A function is a self contained block of code that
performs a particular task. Main is also a function but in order to use a
function we need to inform it that we want to use it this act of informing the compiler
of the functions need is known a function call. The only function that is
automatically called is the main function. Their are some information which
need to be passed to the function at the time of function call these are
nothing but arguments .the result processed by the function is also given by
the return statement (optional).now one by one we will study all the terms
related to functions.
First we study the function prototype
Function prototype is the function declaration i.e.
it gives the compiler the information of the function. Its syntax is:
Return datatype function
name(arguments);
Return type is nothing the datatype
of the value returned by the function .it can be an int,
float, char etc.Every function returns a value except
for the function with void in place of the return type. That means this
function returns nothing i.e. no processed or calculated data is given back to
the main function. Likewise the void main command which we use basically
returns nothing to the operating system.
For example:
int add(int a,intb);
Now we go for
function definition
Function definition is the actual part of the
function which performs the intended task. Function definition has the various
statements which are executed when a function is called .it is over here that
the data provided by the main function is copied into the variables used in the
function. Function definition is usually provided at the end of a program but
there is no error if we directly write the function definition without function
prototype at the starting after including the header files. Take a look at the
syntax:
return type function name(argument list with name)
{
Statements;
Return statement if return type is not void;
}
This is the function definition.
Now the
function call
Function
call is nothing but the signal to the function that yes you are required hare
please come just like we signal our friend .function call is actually the
linking of the various function together via a common channel that is the
main().however it is possible to call a function in another function etc.Syntax
of function call:
Function
name (variable names);
Here
inside the bracket only names are required no need to write the datatype of the variables.
I will
take an example and explain the concepts we have learnt so far.
#include<iostream.h>
int factorial(int a);
void main()
{
int a,fact;
cout<<"enter the number:";
cin>>a;
fact=factorial(a);
cout<<"\nthe
factorial="<<fact;
}
int factorial(int b)
{
int fac=1;
for(int
i=1;i<=b;i++)
fac=fac*i;
return fac;
}
In this
program we find the factorial of the given number using function. Now first we
write the function prototype of the function factorial at the starting then the
main( ) starts. In the main we read the number whose
factorial has to be found. Then we call the function factorial .now after
calling what happens is that first the value of a is
stored in b as the function call has a where we have b in the function
definition. Then the statement in the function definition are executed .finally
the control reaches the return statement were the value of fac
which is the factorial is returned to the main( ).in
main it is caught by the variable fact which simply uses the assignment
operator for catching the value.
Now again
consider the same program with just one change
#include<iostream.h>
int factorial(int a);
void main()
{
int a,fact;
cout<<"enter the number:";
cin>>a;
fact=factorial(a);
cout<<"\nthe
factorial="<<fact;
}
int factorial(int a)
{
int fac=1;
for(int
i=1;i<=a;i++)
fac=fac*i;
return fac;
}
Here we have
put a in place of b in the function prototype and
definition. Here the concept of scope comes into play.
Scope is
nothing but the period up to which the variable is in existence or the area up
to which it can be used.
Now the
variable a is declared in the main so it can only be
used in main but if a function is used outside main then it can be used
anywhere in the program. Similarly the a is out of scope outside main so
therefore it is possible to use it in the function definition of factorial as
it will not cause the same variable to have two value.
If you pass a function
an argument you do not pass it
the actual variable or object but only a copy of it. This means that a function
cannot modify an argument you gave it. The method used above is the same call
by value type. Take it as an example. The other one is call
by reference about which we will learn later.
Recursive Functions
It is possible to call a function from itself; in this case
the function is recursive. For
example, the following function is recursive:
void f() {
f();
}
This function will go in infinite loops or infinite
executions.
Following example finds the factorial of a number using
recursive function.
#include<iostream.h>
int factorial(int a);
void main()
{
int a,fact;
cout<<"enter the number:";
cin>>a;
fact=factorial(a);
cout<<"\nthe
factorial="<<fact;
}
int factorial(int a)
{
int fac;
if(a==1)
{
return 1;
}
else
{
fac=factorial(a-1)*a;
return fac;
}
}
As you can see the function factorial calls itself in its
own body. So when the function is called first it is checked whether the number
is one or not if it is one then 1 is directly returned by the if statement but
if the number is other than 1 then the else statement is executed. In the else
body the factorial function is again called but this time with one less than
the number a. This function call is multiplied with the number a. Now when the
function is called again. If a-1 is not 1 then the function call with one less
than a-1 is executed. So this goes on till the if in the function definition is
executed which returns one and then the pending open functions in the memory
begin to give their return values thus finally giving the result as fac.