Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.

Similar presentations


Presentation on theme: "Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the."— Presentation transcript:

1 Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the main function. Function prototype tells the C++ compiler the data type of the function and the arguments, the correct number of arguments, and the correct order of arguments. The data type of a function is determined by the type of value returned by the function. It does not specify the function operation. You need to provide a definition for each function.

2 Function definition and function prototype // A programmer-defined square function #include int square (int); // Function prototype int main() { int x; for (x = 1; x <= 10; x++) cout << square(x) << endl; return 0; } int square (int y) // Function definition { return y * y; }

3 Example square function gets the value of x. square function calculates x * x. The result is passed back to the cout function. cout function displays the result. This process is repeated ten times using the for repetition structure. 1 4 9 16 25 36 49 64 91 100 Output

4 Function without argument Example: void display_information(void); The function display_information is a void function, returns nothing. Second void indicates that display_information expects no arguments.

5 Function with argument Example: int square(int); // prototype The int in parenthesis informs the compiler that function square expects to receive an integer value from the caller. The int to the left of square informs the compiler that it returns an integer result to the caller.

6 Void function with input argument Example: void print_num(int); // prototype The int in parenthesis informs the compiler that function print_num expects to receive an integer value from the caller. The void to the left of the function print_num informs the compiler that it returns nothing to the caller.


Download ppt "Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the."

Similar presentations


Ads by Google