Professional Documents
Culture Documents
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
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
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.
Page 4 of 8
Programming Language
Functions
Unit 7
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
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