Function Declarations

The function declaration tells the compiler the function name and how to call these function. The body of these function can be defined separately. A function declaration contains these following parts

1 :return_type function_name( parameter list );


For the function max() defined above, the following is the function declaration −

1) int max(int num1, int num2);


Parameter names are not important in function declarations, only their types are required, so the following are also valid declarations −

int max(int, int);

A function declaration is required when a function is defined in one source file and called in another source file. In this case, you should declare the function at the top of the statement that calls the function.

Calling a Function


When creating a C++ function, you define what the function must do. To use a function, you must call or call the function. When a program statement calls a function, program control is transferred to the calls function. The called function performs a defined task and returns program control to the main program when the return statement is executed or when its function closing parenthesis is given in this function statement. To call a function, just pass the required parameter and the function name, and if the function returns a value, then you can store the returned value. For example -

example 1


using namespace std
// function declaration//
int height(int num1, int num2);
int main() { // local variable declaration://
int a = 100;
int b = 200;
int ret;
// calling a function to get height value.//
ret = height(a, b);
cout << "height value is : " << ret << endl;
return 0;
}

example 2

// function returning the height between two numbers//
int height(int num1, int num2) {
// local variable declaration//
int result;
if (num1 > num2)
result = num1;
else
return result;
}

result

It keep the height() function and main() function and compile the source code. When running the final executable statement. it produces the following results −

height value is : 200


example 3

output