You are on page 1of 8

Programming Language

Functions

Unit 7

Functions
A function is a self- contained program segment that carries out some specific, welldefined tasks. Functions break large computing tasks into smaller ones, and enable people
to build on what others have done instead of starting over from scratch. Appropriate
functions hide details of operation from parts of the program that don't need to know about
them, thus clarifying the whole, and easing the pain of making changes.
C has been designed to make functions efficient and easy to use; C programs generally
consist of many small functions rather than a few big ones. A program may reside in one or
more source files. Source files may be compiled separately and loaded together, along with
previously compiled functions from libraries.
The advantages of functions are as follows:
Can be developed, tested, debugged, and complied independently by different
member of a programming team.
There is no chance of code duplication.
Easily maintainable.
Each function is reusable
Large member of programmer can be involved
Program can be developed in short period of time
Can be developed in different places
Components of function:
Function prototype
Function parameters

. Function definition
. Function return

.Function calling

/*program to multiply any two numbers by using function */


#include <stdio.h>
#include <conio.h>
int product(int, int);
//Function prototype
void main()
{ int a, b, c;
printf(Enter any two numbers);
scanf(%d%d,&a,&b);
c=product(a,b);
//function calling
printf(The product of two number is:%d,c);
getch();
}
int product(int x, int y)
//function definition
{
int p;
p=x*y;
return(p);
}
Function prototype
int product(int, int);
Function prototype provides the following information to the compiler:
The name of the function i.e. product
The type of the value return eg.int , float, void etc
The number and the type of the arguments that must be supplied in a function call
Function definition
int product(int x, int y)
{
int p;
Ashok Pandey

Page 1 of 8

Programming Language

Functions

Unit 7

p=x*y;
return(p);
}
The first line of the function definition is known as function declaratory and is followed by
the function body. Both declarator and function body makeup the function definition. If the
function is defined before the main function then its prototypes declaration is optional.
Function call
c=product(a, b);
A function call is specified by the function name followed by the arguments enclosed in
parentheses and terminated by a semicolon. The return type is not mentioned in the
function call.
Function parameters
The parameters specified in the function call are known as actual parameters and those
specified in the function declarator are known as formal parameters.
Eg a, b in c=product(a,b); are known as actual parameters and
x, y in int product(int x, int y) are known as formal parameters.
Function return
Function can be grouped into two type:
Function that do not have a return type ie. Void function.
Function that do have a return value ie return(p).
Types of function:
Function returning
Function returning
Function returning
Function returning

value and passing arguments


no value but passing arguments
value and passing no arguments
no value and passing no arguments

Function returning value and passing arguments:


#include <stdio.h>
#include <conio.h>
int product(int, int);
//Function prototype
void main()
{ int a, b, c;
printf(Enter any two numbers); scnaf(%d
%d,&a,&b);
c=product(a,b);
//function calling
printf(The product of two number is:%d,c);
getch();
}
int product(int x, int y)
//function definition
{ int p;
p=x*y;
return(p);
}
Function returning no value and passing arguments:
#include <stdio.h>
#include <conio.h>
void product(int, int);
//Function prototype
void main()
{ int a, b;
printf(Enter any two numbers); scnaf(%d
%d,&a,&b);
product(a,b);
//function calling
getch();
Ashok Pandey

Page 2 of 8

Programming Language

Functions

Unit 7

}
void product(int x, int y)
//function definition
{
int p;
p=x*y;
printf(The product of two number is:%d,p);
}
Function returning value and passing no arguments:
#include <stdio.h>
#include <conio.h>
int product();
//Function prototype
void main()
{ int c;
c=product();
//function calling
printf(The product of two number is:%d,c);
getch();
}
int product()
//function definition
{
int a, b, p;
printf(Enter any two numbers); scnaf(%d
%d,&a,&b);
p=a*b;
return(p);
}
Function returning no value and passing no arguments:
#include <stdio.h>
#include <conio.h>
void product();
//Function prototype
void main()
{ product();
//function calling
getch();
}
void product()
//function definition
{
int a, b, p;
printf(Enter any two numbers);
scanf(%d%d,&a,&b);
p=a*b;
printf(the product of two number is:%d, p);
}
Preprocessor directives:
The preprocessor directives are executed at the beginning of the compilation process. They
are not program statements, neither it is a part of function body, and do not end with a
semicolon, as program statements. It begins with the pound sign (#).Preprocessor
directives are instruction to the compiler itself.The preprocessor directive #include tells the
compiler to insert another file into the same source file.
Macros:
Macros are the single identifiers that are equivalent to expression, complete statement or
group of statements. The work of macros is similar to function, however they treated
differently during the compilation process. The preprocessor directive #define can be used
to define macros.
Ashok Pandey

Page 3 of 8

Programming Language

Functions

Unit 7

/* a simple macro */
#include <stdio.h>
#include <conio.h>
#define PI 3.1415
void main()
{float r, area; clrscr();
printf(Enter radius of the circle);
scanf(%f,&r);
area=PI*r*r;
printf(Area of the circle is:%f, area);
getch();
}
/* macro to find the maximum number */
#include <stdio.h>
#include <conio.h>
#define max(a, b) (a>b?a:b)
void main()
{ int x, y, z;
clrscr();
printf(Enter any two integer numbers);
scanf(%d%d,&x,&y);
z=max(x, y);
printf(The greatest number is:%d, z);
getch();
}
Recursion:
It is a process by which a function calls itself repeatedly until some specific condition has
been satisfied. For the problems solve recursively following two conditions must be satisfied:
Each time a function calls itself and it must be closer to a solution.
The problem statement must include a stopping condition.

Basic steps of recursive programs:


Define base and recursive case
Initialize the algorithm. Recursive programs often need a seed value to start with. This is
accomplished either by using a parameter passed to the function or by providing a
gateway function that is non recursive but that sets up the seed values for the recursive
calculation.
Check to see whether the current value being processed match the base case. If so, process
and return the value else if the value being processed does not match the base case, then,
redefine the solution in terms of a smaller or simpler sub-problem or sub-problems,
Run the algorithm on the sub-problem.
Combine the results to find the solution.
Return the result.
Example : program to find the factorial of a given number by using recursion
#include <stdio.h>
#include <conio.h>
long int fact(int);
//Function prototype
void main()
{ int n;
printf(Enter any number);
scanf(%d,&n);
printf(The factorial of given number is:%ld, fact(n));
getch();
}
Ashok Pandey

Page 4 of 8

Programming Language

Functions

Unit 7

long int fact(int num)


{ if(num==0)
return 1;
else
return(num*fact(num-1));
}
Here base case is 0 and it is also called stopping condition.
Lets trace the evaluation of factorial(5):
Factorial(5)=
5*Factorial(4)=
5*(4*Factorial(3))=
5*(4*(3*Factorial(2)))=
5*(4*(3*(2*Factorial(1))))=
5*(4*(3*(2*(1*Factorial(0)))))=
5*(4*(3*(2*(1*1))))=
5*(4*(3*(2*1)))=
5*(4*(3*2))=
5*(4*6)=
5*24=
120
Example: program to find the GCD of given two numbers by using recursion
#include <stdio.h>
#include <conio.h>
int gcd(int, int);
//Function prototype
void main()
{ int a, b, c;
printf(Enter any two number); scanf(%d
%d,&a, &b);
c=gcd(a, b);
printf(The GCD of given two numbers is:%d, c);
getch();
}
int gcd(int x, int y)
{ if(y!=0)
return(gcd(y, x%y));
else
return x;
}
Tower of Hanoi problem:
Initial state:
There are three poles named as origin, intermediate and destination.
n number of different-sized disks having hole at the center is stacked around the
origin pole in decreasing order.
The disks are numbered as 1, 2, 3, 4, .,n.
Objective:
Transfer all disks from origin pole to destination pole using intermediate pole for
temporary storage.
Conditions:
Move only one disk at a time.
Each disk must always be placed around one of the pole.
Never place larger disk on top of smaller disk.
Example: Recursive solution of tower of Hanoi:
Ashok Pandey

Page 5 of 8

Programming Language

Functions

Unit 7

#include <stdio.h>
#include <conio.h>
void TOH(int, char, char, char);
//Function prototype
void main()
{ int n;
printf(Enter number of disks);
scanf(%d,&n); TOH(n,O,D,I);
getch();
}
void TOH(int n, char A, char B, char C)
{
if(n>0)
{
TOH(n-1, A, C, B);
Printf(Move disk %d from %c to%c\n, n, A, B);
TOH(n-1, C, B, A);
}
}
Advantages of Recursion:
The code may be much easier to write.
To solve some problems which are naturally recursive such as tower of Hanoi.
Disadvantages of Recursion:
Recursive functions are generally slower than non-recursive functions.
May require a lot of memory to hold intermediate results on the system stack.
It is difficult to think recursively so one must be very careful when writing
recursive functions.
Storage class:
Every variable and functions in C has two attributes type and storage class. The storage
class of a variable indicates the allocation of space to the variable by the compiler. C
supports the following four storage classes:
Storage class
Respective keyword
Automatic
auto
External
extern
Register
register
Static
static
Automatic storage class:
All the variables declared within functions are auto by default. Variables declared auto can
only be accessed within the function or nested block within which they are declared. They
are created when the control enters to the function and they are destroyed when control
exited form the function. The variable declared inside a function without storage class
specification is auto variable by default.
The auto variable can be declared as:
auto int a;
auto float b;
Static storage class:
The value of static variable persists until the end of the program. The static variables can be
initialized only once. To allow a local variable to retain its previous value when the block is
reentered static storage class is used.
/* for static storage class */
#include<stdio.h>
void
staticdemo();
void main()
{
int i;
for(i=0;i<5;i++)
Ashok Pandey

Page 6 of 8

Programming Language
staticdemo();
}
void staticdemo()
{
static int p=5;
p++;
printf(p=%d\t,p);
}
Output:
6
7
8
9

Functions

Unit 7

10

Register storage class:


The storage class register tells the compiler that the associated variables should be stored in
high speed memory register. The use of the storage class register is an attempt to improve
execution speed. The numbers of variables which can be declared register are limited. The
frequently used variables are declared as register variable.
The register variables can be declared
as: register int i;
register float b;
External storage class:
The variables that are active and alive throughout the entire program are known as external
variable. When a variable is declared outside a function storage is permanently assigned to
it, and its storage class is extern.
Example: program to illustrate the use of external
variable /*file1.c */
#include<stdio.h>
int g;
void
fun1();
void
fun2();
void
fun3();
void main()
{
int i;
printf(Enter value of g);
scanf(%d,&g);
fun1();
fun2();
fun3();
}
void fun1()
{
printf(file1.c and function1 %d, g+1);
}
/* file2.c */
#include<stdio.h>
extern int g;void
fun2()
{
printf(file2.c and function2 %d, g+2);
}
void fun3()
{
printf(file3.c and function3 %d, g+3);
}
Ashok Pandey

Page 7 of 8

Programming Language

Functions

Output:
Enter value of g: 10
File1.c and function1 11
File2.c and function2 12
File2.c and function3 13

Ashok Pandey

Page 8 of 8

Unit 7