Tuesday, 27 March 2018

Elements of user-defined Functions

There are some similarities exists between functions and variable in C++.
 1. Both function name and variable names are considered as identifiers and therefore they must follow the rules of creating variable's name.
 2. Like variable, function have data types associated with them.
 3. Like variable, function names and their types must be declared and defined before they are used in the program.
   In order to make use of user defined functions, we need to establish three elements that are related to functions.
  1. Function definition
  2. Function call
  3. Function declaration

Definition of functions
  The function definition is an independent program module that is specially written to implement the requirements of the definition. So it is also called as function implementation. It includes following topics:
Function Header
Function Body

The general form of function definition is as given below:
Return_type function_name(parameters list)
{
local variable declaration;
executable statement1;
executable statement2;
------------------;
------------------;
return statement;
}
The first line Return_type function_name(parameter list) is know as function header and the statement enclosing the curly braces are known as function body.

Function Header
 It includes three parts:
   1. Return type
      It specifies type of the value that the function is expected to return to the program calling the function.It is also called as return type. If function is not returning any values we have to specify it as void.
   2. Function name
      It is any valid C++ identifier name and therefore must follow the rules for creation of variable names in C++
   3. Parameters list
      It declares variable that will receive the data sent by the calling program. They serve as input data to the function to carry out specific task. Since, they represent the actual input values they are referred as formal parameters or formal arguments.
 For example:
 int findmax(int x, int y, int x)
{
---------------
}
double power(double x, int n)
{
--------------
}
double quad(int a, int b, int c)
{
---------------
}
void print_line( )
{
------------------------
}

Function Body
 It contains the declarations and statements necessary for performing required task. The body enclosed in braces, contains thee parts:
  1. Local variables declaration
       It specifies the variables needed by the function locally.
  2. Function Statements
      That actually performs task of the function.
  3. The return statement
      It returns the value specified by the function.

For example:

float mul(float x, float y)
{
float result; /* local variable */
result = x * y; /* find the result */
return(result);  /* return the result */
}
void display(void)
{
cout<<"Hello World!";  /* only print the value */
}
void sub(int a, int b)
{
cout<<"Subtraction:"<<(a-b);  /* no variable */
}

Return values
 A function may or may not send back any value to the calling function.If it does, it is done by return statement. The return statement also sends the program control back to the calling function. It is possible to pass a calling function any number of values but called function can only return one value per call, at most. The return statement can take one of the following forms:
return;
return(value);
return(variable)
return(expression);
The first plain return does not return any value; it acts much as closing brace of the function. The remaining three statements can eliminates the brackets also. When the return is executed, the program control immediately transfer back to the calling function. None of the statements written after return are executed afterwards.

For example:
return(x);
cout<<"Bye.....Bye";
  In this case the cout statement will not be executed in any case.Because the return statement will transfer program control immediately back to the calling function.Some examples of return are:
int div(int x, int y)
{
int z;
z = x/y;
return z;
}
  Here, instead of writing statement inside the function div( ), we can write a single statement as,
return (x * );
OR
return x * y;
 A function may have more than one return statement when it is associated with any condition such as,
if(x>y)
return x;
else
return y;
In this code, in any condition, only one return statement will be executed.

1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (83) R (68) Selenium Webdriver (2) Software (13) SQL (27)