{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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
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.
Background image of page 1

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

View Full Document Right Arrow Icon
Void Functions 2
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}