You are on page 1of 16

PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

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);
}

In these two formats


Return type: This specifies the data type of values returned to the above examples.The value of s can
be returned to main() which is integer value.Here return type of add is int.
If no value is returned to the calling function then retun type is declared as void by default
return type of any function will be of type int
Function name: each function is recognized by its name .The function name must follow the rules of
identifiers
Parameters or arguments: The arguments in the function definition are always variables.They cab
be declared in two ways.Shown in the above format1 and format2.

Dept of CSE & IT Page 1


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

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

ex: void add(int a,int b)


{
int s;
s=a+b;

Dept of CSE & IT Page 2


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

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

Dept of CSE & IT Page 3


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

1)Function with no arguments and no return values:


These are the functions in which no parameters are passed from calling function to
called function and no values are returned from called function to calling function
Diagrammatic representation:

Void main() No Void show() arguments


{ Passed to { show
………..
…………………….. ………………..
………………………
Show(); No return value
} To main }

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

Dept of CSE & IT Page 4


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

2) Function with arguments and no return value:


These are the functions in which parameters passing from calling function but no values
returned from called function to calling function.

Diagrammatic representation:

Void main() arguments void show(int x,inty)


{ Passed to { show
………..
…………………….. ………………..
………………………
Show(I,j); No return value
} To main }

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

Dept of CSE & IT Page 5


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

3)Function with no arguments and with return value:


In this no arguments are passed from calling function to called function but values are returned from
called function to calling function.
Diagrammatic representation:

void main() No int show() rguments


{ Passed to { show
………..
…………………….. ………………..
………………………
x=show(); return value return(s)
} To main }

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

Dept of CSE & IT Page 6


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

4)Function with arguments and with return value:


These are the functions in which parameters are passed from calling function to the called
function and values are returned from called function to calling function.
Diagrammatic representation:

void main() a rguments int show(int x,int y)


{ Passed to { show
………..
…………………….. ………………..
………………………
x=show(I,j); return value return(s)
} To main }

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

Passing values between functions (or) parameter passing mechanism:


There are two ways of parameter passing mechanisms
1)call by value
2)call by reference
Call by Value:
When a function is called by an argument/parameter the copy of the argument is passed to the
function. If the argument is a normal (non-pointer) value a possible change on the copy of the
formal argument in the function does not change the actual argument i.e formal parameters will
not be reflected back to actual parameters.

Dept of CSE & IT Page 7


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

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)

10 20 These are local values of function main

A b

10 20 These are formal values for the function sum()

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)

Dept of CSE & IT Page 8


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

{
*x=*x+4;
*y=*y+5;
printf(“%d%d”,*x,*y);
}
Output:
X=14
Y=25
A=14
B=25

In this program the addres of a,b are passed to x,y

A B
10 20

2345 1256

Here x,y are also referering the same location

X-------- A
10

Y--------- B
20

Now do the modification for *x and *y then a and b becomes 14 and 25


X-------- A
14

Y---------B
25

Recursion:
Recursion is a process in which a function calls itself.
Features :

There should be at least one if statement used to terminate recursion.


It does not contain any looping statements.

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

Dept of CSE & IT Page 9


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

/* Program to demonstrate recursion */.


#include <stdio.h>
int fact(int x);
void main()
{
int a,f;
clrscr();
printf(“Enter any integer:”);
scanf(“%d”,&a);
f=fact(a);
printf(“The factorial of %d is %d”,a,f);
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}
Output:
Enter any integer:5
The factorial of 5 is 120
The above program will execute in the following ways.For example if we take the value of a is 3
then the calling function(main) send 3 for the function fact i.e fact(3)
int fact(3)
{
if x==0
return 1;
else
return(3*fact(2));

again call fact(2)


int fact(2)
{
if x==0
return 1
else
return (2*fact(1))
again call fact(1)
int fact(1)
{
if x==0
return 1;
else
return(1*fact(0));
again call fact(0)
int fact(0)
{
if (x==0)
return 1;

Now the value 1 is return to 1*fact(0)=1*1=1


1 is return to 1*fact(1)=1*1=1
2 is return to 1*fact(2)=1*2=2

Dept of CSE & IT Page 10


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

3 is retrun to 2*fact(3)=2*3=6

Out put is factorial of 3! Is 6


example2:
write a program to find the gcd of two numbers using recursion
#include<stdio.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int x,y,g;
clrscr();
printf(“\nenter x,y values:”);
scanf(“%d%d”,&x,&y);
g=gcd(x,y);
printf(“gcd=%d”,g);
getch();
}
int gcd(int a,int b)
{
int g;
if(b<=a&&a%b==0)
g=b;
else if(a<b)
g=gcd(b,a)
else
g=gcd(b,a%b);
retrun(g);
}

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:

1. The scope of a global variable is the entire program file.

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.\

3. The scope of a formal function argument is its own function.

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.

Dept of CSE & IT Page 11


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

7. A variable is redeclared within its scope again, it loses its visibility in the scope of the
redeclared variable.

These are following storage classes which can be used in a C Program:

1) auto
2) extern
3) static
4) register

auto - Storage Class


auto is the default storage class for all local variables and the local variable is known only to the
function in which it is declared. If the value is not assigned to the variable of type auto the default
value is garbage value.
Syntax :
auto [data_type] [variable_name];

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.

Dept of CSE & IT Page 12


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

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

static - Storage Class


static is the default storage class for global variables. As the name suggests the value of static
variables persists until the end of the program.Static can also be defined within a function. If this
is done the variable is initalised at run time but is not reinitalized when the function is called. This
inside a function static variable retains its value during vairous calls. If the value is not assigned
to the variable of type static the default value is zero.
Syntax :
static [data_type] [variable_name];

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++;

Dept of CSE & IT Page 13


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

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

register - Storage Class :


Register is used to define local variables that should be stored in a register instead of RAM. This
means that the variable has a maximum size equal to the register size (usually one word) and cant
have the unary '&' operator applied to it (as it does not have a memory location). If the value is
not assigned to the variable of type register the default value is garbage value.
Syntax :
register [data_type] [variable_name];
Example :
register int a;
When the calculations are done in CPU, the value of variables are transferred from main memory
to CPU. Calculations are done and the final result is sent back to main memory. This leads to
slowing down of processes. Register variables occur in CPU and value of that register variable is
stored in a register within that CPU. Thus, it increases the resultant speed of operations. There is
no waste of time, getting variables from memory and sending it to back again.It is not applicable
for arrays, structures or pointers.It cannot be used with static or external storage class.
Program :
/* Program to demonstrate register storage class.*/
#include <stdio.h>
#include <conio.h>
void main()
{
register int i=10;
clrscr();
{
register int i=20;
printf("\n\t %d",i);
}
printf("\n\n\t %d",i);
getch();
}
Output :
20
10

Passing Array In Function


In C programming, a single array element or an entire array can be passed to a function. Also,
both one-dimensional and multi-dimensional array can be passed to function as argument.
Passing One-dimensional Array In Function
C program to pass a single element of an array to function

#include <stdio.h>
void display(int a)
{

Dept of CSE & IT Page 14


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

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.

Passing entire one-dimensional array to a function


While passing arrays to the argument, the name of the array is passed as an argument(,i.e,
starting address of memory area is passed as argument).

Write a C program to pass an array containing age of person to a function. This


function should find average age and display the average age in main 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

Passing Two Dimensional Arrays to Function


To pass two-dimensional array to a function as an argument, starting address of memory area
reserved is passed as in one dimensional array
Example to pass two-dimensional arrays to function
#include<stdio.h>
void Function(int c[2][2]);
int main(){
int c[2][2],i,j;
printf("Enter 4 numbers:\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
scanf("%d",&c[i][j]);

Dept of CSE & IT Page 15


PACE INSTITUTE OF TECHNOLOGY AND SCIENCES FUNCTIONS

}
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

Dept of CSE & IT Page 16