lect5_2

lect5_2 - Void Functions 1 Programmer Defined Functions -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Void Functions 1 Programmer Defined Functions - Part 2 Void Functions The functions we discussed so far have returned a value. Some times, we would like to write functions that perform subtasks other than returning a value. In such cases, we can use void functions . A void function does not return a value; it has void as its result type. Let us rewrite our rectangle area program using void functions. #include <iostream> using namespace std; // function prototype void computeArea(); int main() { computeArea(); // function call return 0; } // end function main // computeArea function definition void computeArea() { int length, width, area; cout << "Enter values for length and width"; cin >> length >> width; if ( length <= 0 || width <= 0 ) { cout << "One or both of your inputs is not positive"; } else { area = length * width; cout << "Area of a rectangle with "; cout << "length = " << length << " and width = " << width; cout << " is " << area; } } // end function computeArea Functions can call other functions In C++, the body of a function definition can contain a call to any other function if that function prototype has been declared earlier. Void Functions 2 Indeed, we have seen this before several times; the main (which is a function) calling...
View Full Document

Page1 / 5

lect5_2 - Void Functions 1 Programmer Defined Functions -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online