Professional Documents
Culture Documents
POLYTECHNIC
Gobindapur ,plassey,west Bengal,741156
E-CONTENTS:-C PROGRAMMING
UNIT 4:FUNCTIONS
Developed By
4.2.1.storage classes 6
4.2.2.category of function 11
A function is a block of code that performs a specific task. It has a certain name and it is
reusable .All executable code resides within a function. It takes input, does something with it,
then give a specific output. A C program consists of one or more functions indeed.
We pass information to the function called arguments which specified when the function is
called. A function either can return a value or returns nothing. Function is a subprogram that
helps reduce coding.
a. Writing functions avoids rewriting the same code over and over
b. Separating the code into modular functions also makes the program easier to design and
understand.
Types of Function in C:
1. Function Declaration
2. Function Definition
1 . Function declaration
A function prototype is simply the declaration of a function that specifies function’s
name,parameters and return type.it does not contain function body.a function prototype gives
information to the compiler that the function may later be used in the program
1|P ag e
4.1 . Function Definition
The function definition consists of the whole description and code of a function. It tells that
what the function is doing and what are the input outputs for that. A function is called by
simply writing the name of the function followed by the argument list inside the parenthesis.
#include <conio.h>
int r;
r=a + b;
return (r);
int main()
int z;
return 0;
2|P ag e
4.1.2.Scope and lifetime of variables
Scope of variables
A scope in any programming is a region of the program where a defined variable can have its
existence and beyond that variable it cannot be accessed
Local variables
Variables that are declared inside a function or block are called local variables. They can be
used only by statements that are inside that function or block of code, not known to functions
outside their own. In The following example variables x,y and z are local to main() function
#include<stdio.h>
int main()
z=x+y;
printf(“x=%d,y=%d,z=%d”,x,y,z);
return 0;
Output of program
10 20 30
Global variables
Global variables are defined outside a function, usually on top of the program. Global variables
hold their values throughout the lifetime of your program and they can be accessed inside any
of the functions defined for the program.
#include<stdio.h>
int main()
3|P ag e
x=10; y=20; //initialization
g=x+y;
printf(“x=%d,y=%d,g=%d”,x,y,g);
return 0;
Output of program
10 20 30
Example program:
#include<stdio.h>
#include<conio.h>
void fun(int,int);
int main( )
fun(a,b);
getch();
return 0;
4|P ag e
}
x=x+10;
y=y+20;
Example program:
#include<stdio.h>
#include<conio.h>
int main( )
fun(&a,&b);
printf("a=%d,b=%d", a,b);
getch();
return 0;
*x = *x + 10;
5|P ag e
*y = *y + 20;
The result clearly shown that the called function reflect the original values in main function. So
that it changes original values.
4.2.1.storage classes
Storage classes in C
Storage classes are used to describe the features of variables basically the scope ,visibility,and
life time to trace the existence of variables during runtime of a program
a.The variables defined using auto storage class are called local variables
b.By default A variable is in auto storage class if it is not specified explicitly
#include<stdio.h>
#include<conio.h>
void increment();
int main()
increment();
6|P ag e
increment();
increment();
getch();
return 0;
void increment()
int i=1;
printf("%d",i);
i=i+1;
Output of program:
1 1 1
#include<stdio.h>
#include<conio.h>
void increment();
int main()
7|P ag e
{
increment();
increment();
increment();
getch();
return 0;
void increment()
printf("%d",i);
i=i+1;
Output of program:
1 2 3
scope Global
8|P ag e
#include<stdio.h>
#include<conio.h>
int x=10;
int main()
extern int y;
printf("value of x is %d \n",x);
printf("value of y is %d ",y);
getch();
return 0;
int y=20
output of program:
value of x is 10
value of y is 20
a.Same functionality as auto vaiables but accessing register rather than memory is more faster in
program
b.generally, frequently used variables are kept in registers but only a few variables can be placed in
registers
scope Local
9|P ag e
#include<stdio.h>
#include<conio.h>
int main()
register int i ;
for(i=1;i<=5;i++)
printf("%d\t",i) ;
getch();
return 0;
Output of program
1 2 3 4 5
1. We should use static storage class only when we want the value of the variable to
remain same every time we call it using different function call
2. We should use register storage class only for those variables that are used in our
program very oftenly.
3. We should use external storage class only for those variables that are being used by
almost all the functions in the program.
10 | P a g e
4.2.2.category of function
When a function has no arguments, it does not return any data from calling function. When a
function does not return a value, the calling function does not receive any data from the called
function.
#include <stdio.h>
#include <conio.h>
Void fun()
int main()
fun();
return 0;
Output of program :
Hi ! it is a Function
When function has no arguments data cannot be transferred to called function. But the called
function can send some return value to the calling function.
11 | P a g e
#include<stdio.h>
#include <conio.h>
int add( );
main()
int c;
c=add();
int add ()
Int x,y,z;
printf(“enter values”);
scanf(“%d%d”,&x,&y);
z=x+y;
return z;
Output of program:
enter values 2 3
The sum = 5
In this category data is transferred between calling and called function. That means called
function receives data from calling function and called function also sends the return value to
the calling function.
12 | P a g e
#include<stdio.h>
#include <conio.h>
main()
Int a,b,c;
printf(“enter values”);
scanf(“%d%d”,&a,&b);
c=add(a,b);
int z;
z=x+y;
return z;
output of program:
enter values 2 3
The sum = 5
13 | P a g e
4.2.3 Recursion and it’s types
Recursion
The process in which a function calls itself is known as recursion and the corresponding function
is called the recursive function
Recursion rule 1: Every recursive method must have a base case -- a condition under which no
recursive call is made -- to prevent infinite recursion.
Recursion rule 2: Every recursive method must make progress toward the base case to prevent
infinite recursion
Types of recursion
direct recursion
indirect recursion
Tail recursion
direct recursion
when function calls itself directly then it is called direct recursion
int fact(int);
main()
int n,f;
scanf("%d",&n);
f=fact(n);
int fact(int n)
int f;
14 | P a g e
f=1;
else
return f;
Output of program:-
Factorial of 5 is 120
Indirect recursion
When function calls another function and that function calls the calling function
#define N 4
int g=1;
void fun1();
void fun2();
void fun1()
15 | P a g e
if(g<=N)
printf("%d",g);
g++;
fun2();
else
return;
void fun2()
if(g<=N)
printf("%d",g);
g++;
fun1();
else
return;
int main()
fun1();
getch();
return 0;
16 | P a g e
}
Output of program
1 2 3 4
Tail Recursion:
a recursive function is said to be tail recursive if the recursive call is the last thing done by the
function.the function has to perform any operation at the time of calling and it does nothing at
returning time
#include<stdio.h>
#include<conio.h>
int main()
int f;
f=factorial(4);
printf("%d",f);
getch();
return 0;
fact(n,result)
return fact(n-1,n*result);
factorial(n)
return fact(n,1);
17 | P a g e