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.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (725) Python Coding Challenge (169) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses