lect5_1

lect5_1 - Value Returning Functions 1 Programmer Defined...

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: Value Returning Functions 1 Programmer Defined Functions - Part 1 Value Returning Functions The C++ library provides a number of useful predefined functions. However, in many situations we may have to write our own functions. Writing a function involves two things: a declaration and a definition . Consider the following example program. The program defines and uses a function rectangleArea which computes the area of a rectangle. #include <iostream> using namespace std; int rectangleArea(int, int); // function declaration // returns area of rectangle given length and width int main() { int length, width, area; cout << "Enter numbers for length and width:"; cin >> length >> width; if ( length > 0 && width > 0 ) { area = rectangleArea(length, width); // function call cout << "Area of a rectangle with "; cout << "length = " << length << " and width = " << width; cout << " is " << area; } else { cout << "One or both of your inputs is not positive"; } return 0; } // end of main // function definition int rectangleArea(int len, int wid) // function header { return (len * wid); // function body } // end of function rectangleArea Value Returning Functions 2 Declaration of functions A function declaration (also referred to as function prototype ) is placed in the declaration section, before the main function. By this declaration, the compiler knows that rectangleArea is a function and the function has to be supplied with two arguments and it will return a value of type int . Moreover, the first argument is of type int and the second arguments is of type int . The general form for the function declaration has exactly four components as shown below. return_value_type function_name (list of parameter types) ;---------------------------------------------------------- int rectangleArea (int, int); WE CAN ALSO DECLARE THE FUNCTION AS int rectangleArea (int length, int width); The first component of the function declaration specifies what type of value the function returns. In our example case, the function returns a value of type int . The second component specifies the function name. In our example case, the function name is rectangleArea . Note that the same rules for creating identifiers (variable names), discussed before, also apply for function names. The third component specifies how many, and what type of arguments that the function takes. In our example case, the function takes two arguments of type int....
View Full Document

Page1 / 7

lect5_1 - Value Returning Functions 1 Programmer Defined...

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