Module-3 Functions
Module-3 Functions
Module-3 Functions
MODULE 3
FUNCTIONS
1. Function Definition:
A program Module written to achieve a specific task is called as function definition.
i. Function header
Syntax
datatype functionname(parameters)
It consists of three parts
a) Datatype:
The data type can be int,float,char,double,void.
This is the data type of the value that the function is expected to return tocalling function.
b) Function name:
The name of the function.
It should be a valid identifier.
c) parameters
The parameters are list of variables enclosed within parenthesis.
The list of variables should be separated by comma.
3. Function Call:
The method of calling a function to achieve a specific task is called as functioncall.
A function call is defined as function name followed by semicolon.
A function call is nothing but invoking a function at the required place in theprogram to achieve
a specific task.
Ex:
void main()
{
add( ); // function call without parameter
}
Formal Parameters and Actual Parameters
Formal Parameters:
The variables defined in the function header of function definition are called
formal parameters.
All the variables should be separately declared and each declaration must beseparated by commas.
The formal parameters receive the data from actual parameters.
Actual Parameters:
The variables that are used when a function is invoked)in function call) are called
actual parameters.
Prepared by Sumitha Udaikumar (Dept of ISE) SEACET
Principles of Programming using C(22POP13) Module -3 (Functions)
Using actual parameters, the data can be transferred from calling function.to the called
function.
The corresponding formal parameters in the function definition receive them.
The actual parameters and formal parameters must match in number and type ofdata.
The variables used in function call arecalled as The variables defined in functionheader
actual parameters are called formal
parameters
Actual parameters are used in calling function Formal parameters are used in the
when a function is called or invoked function header of a calledfunction.
Ex: add(m,n) Example:
Here, m and n are called actual parameters int add(int a, int b)
{ ………..
}
parameters.
Actual parameters sends data to theformal Formal parameters receive datafrom the
parameters actual parameters.
In this category no data is transferred from calling function to called function,hence called function cannot
receive any values.
In the above example, no arguments are passed to user defined function add( ).
Hence no parameters are defined in function header.
When the control is transferred from calling function to called function a and bvalues are read, they are
added, the result is printed on monitor.
When return statement is executed ,control is transferred from called function/add
to calling function/main.
In this category, there is data transfer from the calling function to the called function using parameters.
But there is no data transfer from called function to the calling function.
The values of actual parameters m and n are copied into formal parameters a and b.
The value of a and b are added and result stored in sum is displayed on the screenin called function itself.
In this category there is no data transfer from the calling function to thecalled function.
But, there is data transfer from called function to the calling function.
No arguments are passed to the function add( ). So, no parameters are defined inthe function header
When the function returns a value, the calling function receives one value from the called function
and assigns to variable result.
The result value is printed in calling function.
result=add(m,n); printf(“sum }
is:%d”,result);
}
In this category, there is data transfer between the calling function and calledfunction.
When Actual parameters values are passed, the formal parameters in calledfunction can receive the
values from the calling function.
When the add function returns a value, the calling function receives a value fromthe called function.
The values of actual parameters m and n are copied into formal parameters a andb.
Sum is computed and returned back to calling function which is assigned to
variable result.
Passing parameters to functions or Types of argument passing
The different ways of passing parameters to the function are:
int m,n;
printf("enter values for a and b:"); scanf("%d
%d",&m,&n);
printf("the values before swapping are m=%d n=%d \n",m,n); swap(m,n);
printf("the values after swapping are m=%d n=%d \n",m,n);
}
Execution starts from function main( ) and we will read the values for variablesm and n, assume we
are reading 10 and 20 respectively.
We will print the values before swapping it will print 10 and 20.
The function swap( ) is called with actual parameters m=10 and n=20.
In the function header of function swap( ), the formal parameters a and breceive the values 10 and
20.
In the function swap( ), the values of a and b are exchanged.
But, the values of actual parameters m and n in function main( ) have not beenexchanged.
The change is not reflected back to calling function.
2. Call by Address
In Call by Address, when a function is called, the addresses of actualparameters are sent.
In the called function, the formal parameters should be declared as pointerswith the same type
as the actual parameters.
The addresses of actual parameters are copied into formal parameters.
Using these addresses the values of the actual parameters can be changed.
This way of changing the actual parameters indirectly using the addresses ofactual parameters is
known as pass by address.
Example: #include<stdio.h>
void swap(int a,int b); void
main()
{
int m,n;
printf("enter values for a and b:"); scanf("%d
%d",&m,&n);
Pointer: A pointer is a variable that is used to store the address of another variable.
NOTE: Syntax: datatype *variablename;
Example: int *p;
#include<stdio.h>
void main()
{
int a ,*p;
p=&a;
}
In the above program p is a pointer variable, which is storing the address of variable a.
The type of formal parameters shouldbe same The type of formal parameters should be same as type
as type of actual parameters of actual parameters, but they have to be declared as
pointers.
Formal parameters contains the Formal parameters contain the addressesof actual
values of actual parameters parameters.
If a variable declared in a function that has same as that of a global variable, then the function will
use the local variable declared within it and ignore the global variable.
Note: it is good for programmers not to use same name for local and global variables as it may
cause confusion.
Example :
#include <stdio.h>
int x =10;
void print();
int main()
{
printf (“\n the value of x in the main() = %d”, x);
int x = 2;
printf (“\n the value of local variable x in the main() = %d”, x);
print ();
return 0;
}
void print ()
{
printf (“\n the value of x in print() = %d”, x);
}
Output:
the value of x in the main() = 10
the value of local variable x in the main() = 2
the value of x in print() = 10
11
Module 3
a. The variables that are defined inside a block { } have local scope and are called as local variable or
automatic variables.
b. They exist only from the point of their declaration until the end of the block.
c. They are not visible outside the block.
Example:
#include <stdio.h>
void print();
int main()
{
int x = 2;
printf (“\n the value of x in the main() = %d”, x);
print ();
return 0;
}
void print ()
{
int x = 1;
printf (“\n the value of x in print() = %d”, x);
}
Output:
the value of x in the main() = 2
the value of x in print() = 1
iii. Function scope
Function scope indicates that a variable is active and visible from the beginning to the end of the function.
In C, only the goto label has function scope. This means that the programmer cannot have the same label
names inside a function.
Example:
int main()
{
……
loop : …….. /*a goto label has function scope*/
……
…….
goto loop; /* the goto statement*/
….
return 0;
}
In the above example, the label loop is visible from the beginning to the end of the main() function.
Therefore, there should not be more than one label having the same name within the main () function.
12
Module 3
Storage Classes
Storage class defines the scope and lifetime of variables and functions declared
within a C program
There are following storage classes which can be used in a C Program:
i. Auto storage class
ii. Register storage class
iii. Static storage class
iv. extern storage class
The variables defined using auto storage class are called as local variables. Auto stands for automatic storage class. A variable
is in auto storage class by default if it is not explicitly specified.
The scope of an auto variable is limited with the particular block only. Once the control goes out of the block, the access
is destroyed. This means only the block in which the auto variable is declared can access it.
A keyword auto is used to define an auto storage class. By default, an uninitialized auto variable contains a garbage value.
Every time the block in which automatic variable is declared is entered, the variable is initialized with the value is declared.
Example: auto int age;
Example program
#include <stdio.h>
void main()
{
int a = 20; //auto integer local to main ()
printf (“\n a= %d”, a);
func1();
}
13
Module 3
void func1()
{
int a = 30; //auto integer local to func1 ()
printf (“\n a= %d”, a);
}
Output:
a = 20
a = 30
The static variables can be declared outside the function (static global variable) and inside the
function (static global variable). They have the characteristics of both local and global variables.
Extern stands for external storage class. Extern storage class is used when we have global functions or variables which
are shared between two or more files.
Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable
or function which have been already defined in the original file.
The variables defined using an extern keyword are called as global variables. These variables are accessible
throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the original
file.
Examples:
extern int x;
14
Module 3
Recursion
Recursion is a method of solving the problem where the solution to a problem depends on solutions
to smaller instances of the same problem.
Recursive function is a function that calls itself during the execution.
i. Base case
The statement that solves the problem is called base case.
Every recursive function must have at least one base case.
It is a special case whose solution can be obtained without using recursion. A base case serves
two purposes:
i). It act as a terminating condition.
ii). the recursive function obtains the solution from the base case itreaches.
ii. General case:
The statement that reduces the size of the problem is called general case.
This is done by calling the same function with reduced size.
In general recursive function of factorial problem can be written as
15
Module 3
n! = 1 , if n = 0 //base case
5!=5*4!
4!=4*3!
3!=3*2!
2!=2*1!
1!=1*0!
1!=1*0!=1*1=1
2!=2*1!=2*1=2
3!=3*2!, 3!=3*2=6
4!=4*3! , 4!=4*6=24
5!=5*4!, 5*=24,
120(answer)
16
Module 3
Example 1.
/******* Factorial of a given number using Recursion ******/
#include<stdio.h>
int fact(int n);
void main( )
{
int num,result;
printf("enter number:");
scanf("%d",&num); result=fact(num);
printf("The factorial of a number is: %d",result);
}
int fact(int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}
out put: enter number : 5
#include<stdio.h>
int fibonacci(int);
int main(){
int n, i;
printf("Enter the number of element you want in series :n");
scanf("%d",&n);
printf("fibonacci series is : n");
for(i=0;i<n;i++) {
printf("%d ",fibonacci(i));
}
}
int fibonacci(int i){
if(i==0)
return 0;
else if(i==1)
return 1;
else
return (fibonacci(i-1)+fibonacci(i-2));
}
Output: Enter the number of terms 5
Fibonacci series
0 1 1 2 3
17