Professional Documents
Culture Documents
UNIT-III
Function: A function is a self conditioned program segment that performs specific and well
defined task.There are two types of functions
1.Library function
2.Userdefined function
Library function:
Library functions are pre defined functions.The code of these functions are not
available to the user so they cnnot modify the functions
Ex: sin(),cos(),sqrt(),pow() etc.;
User defined function:
These functions allow users to define their own functions according to the requirement
of program.An user defined function can be defined in the following formats
Format 1: return type functionname(parameterlist)
parameter declaration;
{
local variables;
executable statement1;
esecutable statement2;
……………………
…………………………
return(expression);or return;
}
ex: int add(a,b)
int a,b;
{
int s;
s=a+b;
return(s);
}
format 2:
return type functionname(datatype parameter1,datatype parameter2…..)
{
local variables;
executable statement 1;
execitable statement2;
……….
…………….
return(expression);or return;
}
ex: int add(int a,int b)
{
int s;
s=a+b;
return(s);
}
Local variables: The variables which are used only in a function are declared in its function
body.These variables are known as local variables.That means local variables are accessible only with
in the function in which they are declared.
Return statement: This can be declared in two ways
1) Return(expression)
2) Return;
return(expression): This statement return the value of expression to the calling function.
return: This statement returns the controller to the calling function but does not return any value.
The return statement always return either a single value or none
Ex: return(a)
return(a+b);
Both are valid
return(a,b) is invalid
The user defined function can be explained with three elements.
1)Function declaration
2)Function call
3)Function definition
Function call: The statement which is used to call a function known is called as function call.
Function definition: The actual code of function known as function definition
Function declaration: The statement where you declared a function by specifying the name and
return type is called as declaration.
ex:
void main()
{
void show();/* function declaration*/
show();/*function call */
}
void show() /* function definition*/
{
printf(“\n welcome “);
}
We can declare the function before the main() or after the main().We can also write actual code
of function before main().If you do so there is no need of function declaration.
Calling function and called function:
The function from which another function is called is known as calling function and
another function is known as called function.
ex: int main()
{
int add(int,int);
int a=10,b=20;
add(a,b);
}
void add(int a,int b) /*function code */
{
int s;
s=a+b;
printf(“%d”,s);
}
Here main() is “calling function” and add() is called as “called function”.In the above example
function is written after main().So the prototype (or) function declaration must be written in
main().We can also write the same function in the following way
printf(“%d”,s);
}
void main()
{
int a=10,b=20;
add(a,b);
}
In the above program function add() is declared before main().So the declaration of function is
not there in main()
Arguments used in functions:
Arguments or parameters are of two types
1) Actual parameters
2) Formal parameters
Actual parameters: The parameters which are passed in function call are known as actual
parameters. They are defined in calling function and they can be expressions, constants or
variables
Formal parameters: The parameters which are used in function define are called formal
parameters.
ex:
void main()
{
void mul(int,int);
int i=10,j=5;
mul(i,j);
}
void mul(int x,int y)
{
int s;
s=x*y;
printf(“%d”,s);
}
In the above programe I,j are actual parameters of and x,y are formal parameters.The values of
x,y are same as values of I,j
Categories of functions:
Functions can be classified in to four types
1)Function with no arguments and no return values
2) Function with arguments and no return values
3) Function with no arguments and with return values
4) Function with arguments and with return values
Example programs:
/* The program illustrates the functions with no arguments and no return value*/
#include<stdio.h>
void add(void);
void main()
{
add();
}
void add(void)
{
int x,y,sum;
printf(“Enter any two integers:”);
scanf(“%d%d”,&x,&y);
sum=x+y;
printf(“The sum id %d”,sum);
}
Output:
Enter any two integers: 2 4
The sum is 6
Diagrammatic representation:
Example
/* The program illustrates the functions with arguments and no return value*/
#include<stdio.h>
void add(int a,int b);
void main()
{
int x,y,;
printf(“Enter any two integers:”);
scanf(“%d%d”,&x,&y);
add(x,y);
}
void add(int a,int b)
{
int sum;
sum=a+b;
printf(“The sum id %d”,sum);
}
Output:
Enter any two integers: 2 4
The sum is 6
Example program
/* The program illustrates the functions with no arguments and but a return value*/
#include<stdio.h>
int add(void);
void main()
{
int sum;
sum=add();
printf(“The sum id %d”,sum);
}
void add(void)
{
int x,y,c;
printf(“Enter any two integers:”);
scanf(“%d%d”,&x,&y);
c=x+y;
return c;
}
Output:
Enter any two integers: 2 4
The sum is 6
Example program:
/* The program illustrates the functions with arguments and a return value*/
#include<stdio.h>
int add(int a,int b);
void main()
{
int x,y,sum;
printf(“Enter any two integers:”);
scanf(“%d%d”,&x,&y);
sum=add(x,y);
printf(“The sum id %d”,sum);
}
int add(int a,int b)
{
int c;
c=a+b;
return c;
}
Output:
Enter any two integers: 2 4
The sum is 6
Ex:
void main()
{
int a=10,b=20;
void sum(int,int);
sum(a,b);
printf(“%d%d”,a,b);
getch();
}
void sum(int x,int y)
{
x=x+4;
y=y+5;
printf(“%d%d”,x,y);
}
output:
x=14
y=25
a=10
b=20
In the above program the values of a,b are passed to the function sum(x,y)
A b
X y
25
14
Values of x,y after executing the sum().values of a&b no
change
X y
Call by Reference:
When a function is called by an argument/parameter which is a pointer(address of the argument)
the copy of the address of the argument is passed to the function. Therefore a possible change on
the data at the referenced address change the original value of the argument.
Ex:
void main()
{
int a=10,b=20;
void sum(int *,int *);
sum(&a,&b);
printf(“%d%d”,a,b);
getch();
}
void sum(int *x,int *y)
{
*x=*x+4;
*y=*y+5;
printf(“%d%d”,*x,*y);
}
Output:
X=14
Y=25
A=14
B=25
A B
10 20
2345 1256
X-------- A
10
Y--------- B
20
Y---------B
25
Recursion:
Recursion is a process in which a function calls itself.
Features :
Advantages :
It is easy to use.
It represents compact programming structures.
Disadvantages :
It is slower than that of looping statements because each time function is called
3 is retrun to 2*fact(3)=2*3=6
Storage Classes
A storage class defines the scope (visibility) and life time of variables and/or functions within a C
Program.
Scope: The scope of variable determines over what region of the program a variable is actually
available for use.
Visibility: The program‟s ability to access a variable from the memory.
Lifetime: Life time refers to the period during which a variable retains a given value during the
execution of a program.
Scope rules:
2. The scope of a local variable begins at point of declaration and ends at the end of the block or
function in which it is declared.\
4. The life time of an auto variable declared in main is the entire program execution time,
although its scope is only the main function.
5. The life of an auto variable declared in a function ends when the function is exited.
6. All variables have visibility in their scope , provided they are not declared again.
7. A variable is redeclared within its scope again, it loses its visibility in the scope of the
redeclared variable.
1) auto
2) extern
3) static
4) register
Example :
auto int a;
Program :
/* Program to demonstrate automatic storage class.*/
#include <stdio.h>
#include <conio.h>
void main()
{
auto int i=10;
clrscr();
{
auto int i=20;
printf("\n\t %d",i);
}
printf("\n\n\t %d",i);
getch();
}
Output :
20
10
extern - Storage Class
Variables that are both alive and active throughout the entire program are known as external
variables. extern is used to give a reference of a global variable that is visible to all the program
files. If the value is not assigned to the variable of type static the default value is zero.
Syntax : extern [data_type] [variable_name];
Example :
extern int a;
The variables of this class can be referred to as 'global or external variables.' They are declared
outside the functions and can be invoked at anywhere in a program.
Program :
/* Program to demonstrate external storage class.*/ #include <stdio.h>
#include <conio.h>
extern int i=10;
void main()
{
int i=20;
void show(void);
clrscr();
printf("\n\t %d",i);
show();
getch();
}
void show(void)
{
printf("\n\n\t %d",i);
}
Output :
20
10
Example :
static int a;
Static storage class can be used only if we want the value of a variable to persist between
different function calls.
Program :
/* Program to demonstrate static storage class. */ #include <stdio.h>
#include <conio.h>
void main()
{
int i;
void incre(void);
clrscr();
for (i=0; i<3; i++)
incre();
getch();
}
void incre(void)
{
int avar=1;
static int svar=1;
avar++;
svar++;
printf("\n\n Automatic variable value : %d",avar);
printf("\t Static variable value : %d",svar);
}
Output :
Automatic variable value : 2 Static variable value : 2
Automatic variable value : 2 Static variable value : 3
Automatic variable value : 2 Static variable value : 4
#include <stdio.h>
void display(int a)
{
printf("%d",a);
}
int main(){
int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only.
return 0;
}
Output
4
Single element of an array can be passed in similar manner as passing variable to a function.
#include <stdio.h>
float average(float a[]);
int main(){
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c); /* Only name of array is passed as argument. */
printf("Average age=%.2f",avg);
return 0;
}
float average(float a[]){
int i;
float avg, sum=0.0;
for(i=0;i<6;++i){
sum+=a[i];
}
avg =(sum/6);
return avg;
}
Output
Average age=27.08
}
Function(c); /* passing multi-dimensional array to function */
return 0;
}
void Function(int c[2][2]){
/* Instead to above line, void Function(int c[][2]){ is also valid */
int i,j;
printf("Displaying:\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
printf("%d\n",c[i][j]);
}
Output
Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5