You are on page 1of 19

MIRMADAN MOHANLAL GOVT.

POLYTECHNIC
Gobindapur ,plassey,west Bengal,741156

DEPARTMENT OF COMPUTER SCIENCE AND TECHNOLOGY

2nd YEAR-3rd SEMESTER

E-CONTENTS:-C PROGRAMMING

UNIT 4:FUNCTIONS

Developed By

SAHNOAJ AHMED ,LECTURER IN CST

COMPUTER SCIENCE & TECHNOLOGY

MIRMADAN MOHANLAL GOVT. POLYTECHNIC

Gobindapur ,plassey,west Bengal,741156


contents

4.1.1. Function declaration and definition: 1


4.1.2.Scope and lifetime of variables 3

4.1.3. Types of passing Parameter 4

4.2.1.storage classes 6
4.2.2.category of function 11

4.2.3 Recursion and it’s types 14


4.1.1. Function declaration and definition:

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.

Why use function:


Basically there are two reasons because of which we use functions

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:

(a). Library Functions in C


C provides library functions for performing some operations which are present in the c library
and they are predefined.For example sqrt() is a mathematical library function .The function
scanf and printf() are input and output library function . we Have strcmp() and strlen() for string
manipulations. To use a library function we have to include some header file using the
preprocessor directive #include.For example to use input and output function like printf() and
scanf() we have to include stdio.h,for math library function we have to include math.h for string
library string.h

(b)User Defined Functions in C


A user can create their own functions for performing any specific task of program are called
userdefined functions.

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.

Simple Example of Function in C


#include<stdio.h>

#include <conio.h>

int addition (int, int); //function declaration

addition (int a, int b) //function definition

int r;

r=a + b;

return (r);

int main()

int z;

z= addion(10,3); //function call

printf ("the result is %d", z);

return 0;

Output of program: The Result is 13

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()

int x,y,z; //local variable declaration

x=10; y=20; //initialization

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 g; //global variable declaration

int main()

int x,y; //local variable declaration

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

4.1.3. Types of passing Parameter

Most programming languages have 2 strategies to pass parameters. They are


(i) pass by value
(ii) pass by reference

(i) Pass by value (or) call by value :-


In this method calling function sends a copy of actual values to called function, but the changes
in called function does not reflect the original values of calling function

Example program:
#include<stdio.h>

#include<conio.h>

void fun(int,int);

int main( )

int a=10, b=15;

fun(a,b);

printf("a=%d b=%d", a,b);

getch();

return 0;

4|P ag e
}

void fun(int x, int y)

x=x+10;

y=y+20;

Output of program: a=10 b=15


The result shows that the called function does not reflect the original values in main
function.

(ii) Pass by reference (or) call by address :-


the changes in called function reflect the original values of calling function. Calling function
needs to pass “&”operator along with actual arguments and called function need to use “*”
operator along with formal arguments.

Example program:
#include<stdio.h>

#include<conio.h>

void fun(int *,int *);

int main( )

int a=10, b=15;

fun(&a,&b);

printf("a=%d,b=%d", a,b);

getch();

return 0;

void fun(int *x, int *y)

*x = *x + 10;

5|P ag e
*y = *y + 20;

Output of program: a=20 b=35

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

Types of storage class


1.Auto storage class
2.Static storage class
3.Extern storage class
4.Register storage class

1.Auto storage class

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

Storage specifier description


auto Storage place Cpu memory
Initial/default value Garbage value
scope local
life Within the function only

#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

2.Static storage class

a.Static variables retain the latest value used in their scope

b.Static variables initialized only once in it’s lifetime

Storage specifier description


static Storage place Cpu memory
Initial/default value Zero
scope Local
life Retains the value of the variable
between function calls

#include<stdio.h>

#include<conio.h>

void increment();

int main()

7|P ag e
{

increment();

increment();

increment();

getch();

return 0;

void increment()

static int i=1;

printf("%d",i);

i=i+1;

Output of program:

1 2 3

3.Extern storage class

a.Extern variable is nothing but a global variable .

b initialized with a legal value where it is declared in order to be used elsewhere

Storage specifier description

extern Storage place Cpu memory

Initial/default value Zero

scope Global

life Till the end of the main program.variable


definition might be anywhere in c
program

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

4.register storage class

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

Storage specifier description

register Storage place Register memory

Initial/default value Garbage value

scope Local

life Within the function only

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

Which storage class should we use and when

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.

4. Otherwise we should use the automatic storage class.

10 | P a g e
4.2.2.category of function

1.No argument No return value

2. No argument with return value

3.Argument with return value

1.No argument No return value

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()

printf ("Hi ! it is a Function .");

int main()

fun();

return 0;

Output of program :

Hi ! it is a Function

2. No argument with return value

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

printf ("The sum =%d",c);

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

3.Argument with return value

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>

int add(int, int);

main()

Int a,b,c;

printf(“enter values”);

scanf(“%d%d”,&a,&b);

c=add(a,b);

printf ("The sum =%d",c);

int add (int x, int y)

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;

printf("\n Enter any number:");

scanf("%d",&n);

f=fact(n);

printf("\n Factorial of %d is %d",n,f);

int fact(int n)

int f;

if(n==0||n==1) //base case

14 | P a g e
f=1;

else

f=n*fact(n-1); //recursive case

return f;

Output of program:-

Enter any number: 5

Factorial of 5 is 120

For explanation we are taking arbitrary function f(n) as recursive function

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)

if(n==1) return result;

return fact(n-1,n*result);

factorial(n)

return fact(n,1);

17 | P a g e

You might also like