Function Prototyping In C++

Prototyping Definition and Overview

Function Prototyping is the process of declaring a function for the compiler to understand the function name, arguments, and return type.

Parts Of Functions

  • Function Declaration
  • Function Call
  • Function Definition

Function Declaration

return_type function_name( param_1,param_2 ... param_n );

For example,

int add(int x,int y)

return_type

A function can return a value using the return statement to the main program.  The function does not return value if the return_type is void.

function_name

A function_name is a user-defined name (identifier) for calling/using the function. The function name and the parameter list together constitute the function signature.

Parameters (param_1,param_2 ... param_n)

A parameter is variable which holds data. The Argument transfers the data from the main program to the function definition.

  • Actual arguments: The main program variables(arguments) are known as the actual arguments during the function call.
  • Formal arguments: The Function definition variables(arguments) are known as the formal arguments.

Function Call

When the main program is run, the program reaches the function call statement and invokes the function and passes the control to it.

int main()
{
   ....
   add(a,b); // Function Call
   ....
}

Function Definition

The actual function code block or function body is called as the function definition.
When the function call is triggered by the compiler, the program control is passed to the function definition. Then the compiler executes statements in the body of the function and the program control returns when the return statement or closing braces(}) is reached.

#include<iostream>

using namespace std;

int add(int x,int y); // Function Declaration

int main() {
   .....
   c = add(a,b); // Function Call
   .....
}

int add(int x,int y) // Function Definition
{
   .....
}

Function Declaration Diagram Representation

Function Declaration Diagram

Simple Function Example Program with Return Statement

#include<iostream>

using namespace std;

int add(int x,int y); // Function Declaration

int main() {
   int a=10,b=20,c;
   c = add(a,b); // Function Call
   cout<<"Addition : "<<c;
}

int add(int x,int y) // Function Definition
{
   int z;
   z = x+y;
   return z;
}

Sample Output:

Addition : 30

Line by Line Explanation and Execution Steps

Execution Step 1: Function Declaration

int add(int x,int y); 

Function Declaration is for compiler understanding about function. Here compiler understands function name is "add", which has two number of arguments with int and return type is int.

Execution Step 2: Variable Declaration

int a=10,b=20,c;

we declare two three variables for pass values a(10) and b(20), Third variable c is for receive return values.

Execution Step 3: Function Call

int a=10,b=20,c;

While the program is running, the program reaches to the add(a,b) function call statement, it passes to control the definition of the add(int x, int y). Here a and b are actual parameters, that values pass to int x and int y. We cannot access a and b values in the function definition. z and y are formal parameters. now x and y have the same value of the a and b. x and y values are 10 and 20. 

Execution Step 4: Function Definition

int add(int x,int y)

This line receives control from the main program, x and y contain a and b values respectively. x and y are the local variables We cannot use x and y values outside the function definition.

Execution Step 5: Calculation

int z;
z = x+y;

We have declared new local variable z for assign value of a+b; now z value is 30.z is a local variable We cannot use the x value outside the function definition.

Execution Step 6: return Statement

return z;

now z value is 30. that value returns to the main program. Which is received by c. 

c = add(a,b);

After return statement, all local variables(x,y, and z) scope ended.

Execution Step 7: Print Output

cout<<"Addition : "<<c;

here just printing value of c. c value is 30.