Functions
Functions
INTRODUCTION
It is not necessary that the main() can call only one function, it can call as many functions
as it wants and as many times as it wants.
For example, a function call placed within a for loop, while loop or do-while loop may call
the same function multiple times until the condition holds true.
It is not that only the main() can call another functions. Any function can call any other
function.
In the fig. one function calls another, and the other function in turn calls some other
function.
WHY DO WE NEED FUNCTIONS?
Dividing the program into separate well defined functions facilitates each function to be
written and tested separately. This simplifies the process of getting the total program to
work.
Understanding, coding and testing multiple separate functions are far easier than doing
the same for one huge function.
If a big program has to be developed without the use of any function (except main()), then
there will be countless lines in the main() and maintaining this program will be very
difficult.
All the libraries in C contain a set of functions that the programmers are free to use in their
programs. These functions have been prewritten and pre-tested, so the programmers use
them without worrying about their code details. This speeds up program development.
When a big program is broken into comparatively smaller functions, then different
programmers working on that project can divide the workload by writing different
functions.
A function, f that uses another function g, is known as the calling function and g is known
as the called function.
The inputs that the function takes are known as arguments
When a called function returns some result back to the calling function, it is said to return
that result.
The calling function may or may not pass parameters to the called function. If the called
function accepts arguments, the calling function will pass parameters, else not.
main() is the function that is called by the operating system and therefore, it is supposed to
return the result of its processing to the operating system.
FUNCTION DECLARATION
Function declaration is a declaration statement that identifies a function with its name, a
list of arguments that it accepts and the type of data it returns.
The general Syntax:
return_data_type function_name(data_type variable1, data_type variable2,..);
Where:
return_data_type specifies the data type of value that will be returned to the calling
function.
Function name is the name given to the function
data_type variable1, data_type variable2,… is a list of variables of specified data
type. These variables are passed from the calling function to the called function.
They are also known as arguments or parameters
Example: float avg ( int a, int b);
Things to remember about function declaration:
Function declaration should be end with semicolon.
The function declaration is global.
Use of argument names in the declaration is optional.
int func(int , char, float);
A function cannot be declared within the body of another function.
A function having void as its return type cannot return any value.
A function having void as its parameter list cannot accept any value.
If the declaration does not specify any return type, then by default, the function
returns an integer value.
sum(int a,int b);
FUNCTION DEFINITION
FUNCTION CALL
The return statement is used to terminate the execution of a function and return control to
the calling function.
When the return statement is encountered, the program execution resumes in the calling
function at the point immediately following the function call.
Programming Tip: It is an error to use a return statement in a function that has void as its
return type.
A return statement may or may not return a value to the calling function.
The syntax of return statement can be given as
return <expression> ;
Here expression is placed in between angular brackets because specifying an expression is
optional. The value of expression, if present, is returned to the calling function. However,
in case expression is omitted, the return value of the function is undefined.
Programmer may or may not place the expression within parentheses.
By default, the return type of a function is int.
For functions that has no return statement, the control automatically returns to the calling
function after the last statement of the called function is executed.
FUNCTION PARAMETERS:
1. Formal Parameter:
The variables defined in the function header of the function definition are called
as formalparameter.
All the variables declared should be separated by comma.
Formal parameters receive data (values) from actual parameters.
2. Actual Parameters:
The variables that are used when a function is called in main function is known
as actual parameters.
Using the actual parameters, the data (values) can be transferred to the formal
parameterswhich is used in function definition.
The corresponding formal parameters in the function definition will receive them.
The actual parameters and formal parameters must match in numbers and the type
ofdata (datatype).
#include<stdio.h>
int add( inta,int b) //a,b are formal parameters
{
return(a+b);
}
void main( )
{
int m,n,p;
printf(“enter any two numbers\n”);
scanf(“%d%D”,&m,&n);
p=add(m,n); //m,n are actual parameters
}
Difference between Actual parameters and Formal Parameters
#include<stdio.h>
int large( inta,int b)
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
int main( )
{
int m,n,j;
printf(“enter two numbers”);
scanf(“%d%d”,&m,&n);
j=large(m, n);
printf(“large num=%d”,j);
return 0;
}
//WAP to compare 3 numbers and return large of three numbers using function.
#include<stdio.h>
int large(int a, int b, int c)
{
if(a>b && a>c)
{
return a;
}
else if(b>a && b>c)
{
return b;
}
else
{
return c;
}
}
int main( )
{
int m,n,p,j;
printf(ënter any 3 numbers”);
scanf(“%d%d%d”,&m,&n,&p);
j=large(m,n,p);
printf(“greater num=%d\n”,j);
return 0;
}
There are two ways in which arguments or parameters can be passed to the called function.
1. Call by value in which values of the variables are passed by the calling function to the
called function.
2. Call by reference in which address of the variables are passed by the calling function to
the called function.
CALL BY VALUE
In the Call by Value method, the called function creates new variables to store the value
of the arguments passed to it. Therefore, the called function uses a copy of the actual
arguments to perform its intended task.
If the called function is supposed to modify the value of the parameters passed to it, then
the change will be reflected only in the called function. In the calling function no
change will be made to the value of the variables.
#include<stdio.h>
void add( int n);
int main()
{
int num = 2;
printf("\n The value of num before calling the function = %d", num);
add(num);
printf("\n The value of num after calling the function = %d", num);
return 0;
}
void add(int n)
{
n = n + 10;
printf("\n The value of num in the called function = %d", n);
}
The output of this program is:
The value of num before calling the function = 2
The value of num in the called function = 12
The value of num after calling the function = 2
The Call-by-value method of passing arguments to a function must be used only in two cases:
I. When a called function does not need to modify the value of the actual parameter. It
simply uses the value of the parameter to perform its task.
II. When you want that the called function should only temporarily modify the value of the
variables and not permanently.
When the calling function passes arguments to the called function using call by value
method, the only way to return the modified value of the argument to the caller is
explicitly using the return statement.
The better option when a function can modify the value of the argument is to pass
arguments using call by reference technique.
In call by reference, we declare the function parameters as references rather than normal
variables.
When this is done any changes made by the function to the arguments it received are
visible by the calling program.
To indicate that an argument is passed using call by reference, an asterisk (*) is placed
after the type in the parameter list. This way, changes made to that parameter in the called
function body will then be reflected in its value in the calling program.
#include<stdio.h>
void add( int &n);
int main()
{
int num = 2;
printf("\n The value of num before calling the function = %d", num);
add(&num);
printf("\n The value of num after calling the function = %d", num);
return 0;
}
void add( int *n)
{
*n = *n + 10;
printf("\n The value of num in the called function = %d", n);
}
The output of this program is:
The value of num before calling the function = 2
The value of num in the called function = 12
The value of num after calling the function = 12
Advantages
I. Since, arguments are not copied into new variables, it provides greater time and space
efficiency.
II. The called function can change the value of argument and change is reflected in the calling
function.
III. A return statement can return only one value. In case we need to return multiple values,
pass those arguments by reference.
Disadvantages
I. When argument is passed using call-by-reference it’s difficult to tell whether that
argument is meant for input, output or both.
CATEGORIES OF FUNCTION
#include<stdio.h>
void add( )
{
int s,i=10,j=20;
s=i+j;
printf(“ sum=%d\n”, s);
}
void main( )
{
add( );
}
Here main() does not pass the parameter to add() and add() does not return the value.
Parameters are passed from calling function to the called function and based on the received
parameters values, called function performs required action and returns a value.
#include<stdio.h>
int add(int i,int j); //function declaration
int main( )
{
int sum, a=10, b=20;
sum=add(a, b); / /fun call
printf(“sum=%d”,sum);
return 0;
}
int add(int I, int j)
{
int s;
s= i+j; //fun def return s;
}
In the above program main() function passes the parameter a and b to add() function
and add() function returns value to main() function.
#include<stdio.h>
int mul( ) //function definition
{
int p, i=10, j=20;p=i*j;
return p;
}
int main( )
{
int prod;
rod=mul( ); //function call
printf(“prod=%d\n”, prod);
return 0;
In the above program no parameter are passed from main() to mul(). The input are given in
mul( ) function and result is returned to main().
5. Functions that return multiple values.