Discuss about functions in detail

Estudies4you
Q36. Discuss about functions in detail.
Answer:

Functions
Functions can be defined as components of a large program. They obtain values from the main( ) function, performs the operation and returns the result to the calling function, Functions are reusable. They make debugging and modification of a program easier. They reduce the size of the program. They provide modularity of programs.
Structure of a C++ Function
The structure of a C++ function has five components,
  1. Function prototype declaration
  2. Function call
  3. Function definition
  4. Actual and formal arguments
  5. Return statement
1. Function Prototype Declaration
A function prototype declares the return type, function name and its arguments.
Syntax
fun_returntype fun_ name (fun _ arguments);
Example
float total( int, float );
In the above example, the prototype declares fun._ name total( ) containing two arguments float and integer. Its return type is float.

2. Function Call
A function call call a function. A function gets executed whenever it is called.
Function call contains function name and arguments enclosed in parenthesis. It is also possible to call a function without any arguments.
Syntax
function_name (arguments);
Here function_name represents, the name of the function and arguments represent the parameters to be passed to the called function.
Example
add(x, y);
In the above example add is the function name, x and y are the arguments passed to add ( ).

3. Function Definition
Function definition defines a function. It contains the return type, function name and its arguments. Function definition must be similar to function prototype. It is followed by function body. Function body starts and end with curly braces {}. Functions can be defined anywhere in the program. A function which is defined before its caller may omit its prototype declaration.
Syntax
fun _returntype fun _ name (fun _ arguments);
{
Function body;
}
Example
int add (x, y) //function definition
{
return (x + y); //function body
}

4. Actual and Formal Arguments
Actual arguments are the arguments that are declared within calling function and resides in function call. Formal arguments are the arguments declared in function definition.
Syntax
main( )
{
function_name (arguments_list); //actual arguments in function call
}
returntype function_name (arguments list). //formal arguments in function definition
{
function body;
}

Example
void main( )
{
a=sum(x, y); //actual arguments
}
int sum(int p, int q) //formal arguments
{
return (x+y);
}
In the above example, the values of x and y are stored in p and q respectively. The values of actual arguments (i.e., x,y) are passed to formal arguments ( p and q).

5. Return Statement
Return statement returns value to the calling function. When the execution of program reaches a return statement, the compiler transfers the control to the calling function. At any instant, a return statement returns only one value.
Syntax
return variable name;
Example
return (x + y);
The above example returns the result of (x + y).

To Top