Coupon Accepted Successfully!


Parts of a Function

Parts of a function are as follows.

  1. Function prototype declaration
  2. Function call
  3. Definition of a function
  4. Actual and formal arguments
  5. Return statement

Function Prototype Declaration

We use many built-in functions. The prototypes of these functions are given in the respective header files, and we include them using #include directive. In C++, while defining user-defined functions, it is unavoidable to declare its prototype. A prototype statement helps the compiler to check the return and argument types of the function.

A function prototype declaration consists of function’s return type, name, and arguments list. It tells the compiler

  1. Name of the function
  2. Type of value returned
  3. The type and number of arguments

When the programmer defines the function, the definition of function must be same like its prototype declaration. If programmer makes mistake, the compiler flags an error message. The function prototype declaration statement is always terminated with semicolon. The statements given below are the examples of function prototypes.

(A) void show (void);

(B) float sum (float, int);

(C) float sum (float x, int y);

In example (A) the return type is void, that is the function does not return any value. The void functions are always without return statement. The void argument, that is the function, does not require any argument. By default, every function returns an integer value. To return a non-integer value, the data type should be mentioned in function prototype and definition. While writing definition of function, the return type must be preceded by the function name and it is optional if return type is default (int).

In statement (B), the prototype of function sum() is declared. Its return type is float and arguments are float and integer type, respectively. It is shown in Figure.

In example (C) with argument type, argument names are also declared. It is optional and also not compulsory to use the same variable name in the program.

Parts of a function

Function Call

A function is a latent body. It gets activated only when a call to a function is invoked. A function must be called by its name followed by argument, or without argument, list enclosed in parenthesis and terminated by semicolon.


Syntax of function call is as follows:


function-name(with/without argument list);

In the above statement, function-name is the name of the function, arguments are within the bracket and arguments are separated by comma. If arguments are absent one can write void within the bracket.

Function Definition

The first line is called function definition and function body follows it. The function definition and function prototype should match with each other. The function body is enclosed within curly braces. The function can be defined anywhere. If the function is defined before its caller, then its prototype declaration is optional.


Syntax of function call is as follows:


Return data type specifies the type of value returned by the function.


The function-name is the name of the function being defined.


The argument/parameter list specifies types and names of the arguments/parameters (also called formal arguments).

Actual and Formal Argument

The arguments declared in caller function and given in the function call are called actual arguments. The arguments declared in the function definition are known as formal arguments.

Actual and formal arguments

As shown in Figure, variables y and z are actual arguments and variables j and k are formal arguments. The values of y and z are stored in j and k, respectively. The values of actual arguments are assigned to formal arguments. The function uses formal arguments for computing.

The return Statement

The return statement is used to return value to the caller function. The return statement returns only one value at a time. When a return statement is encountered, complier transfers the control of the program to caller function. The syntax of return statement is as follows:


return (variable name); or return variable name;

The parenthesis is optional.

Few programs are as follows:

7.1 Write a program to declare prototype of function sum(). Define the function sum() exactly similar to its prototype.

Explanation: In the above program, the prototype of function sum() is declared. The prototype instructs the compiler that the function sum() should return float value. The types of arguments used by function sum() are int and float, respectively. The values of variables a and b are passed to function sum(). These values are assigned to formal arguments x and y. The sum of x and y is calculated and returned. The return value of function is assigned to float variable s.

7.2 Write a program to declare and define void function.

Explanation: In the above program, the prototype of function show() is given preceding void keyword. The statement show() invokes the show() function that displays the message “In show()”.

Test Your Skills Now!
Take a Quiz now
Reviewer Name