Chapter 6
Chapter 6
Chapter 6
Functions
Introduction
A function is a self contained program segment that carries out some specific, well defined task.
Every C program consists of one or more functions. Every C program must have main function.
Execution of program will always begin by carrying out the instructions in main.
Generally, a function will process information that is passed to it from the calling portion of
the function, and return a single value. Information is passed to the function via special
identifiers called arguments.
Function Definition
What a function does is called function definition. The first line of a function definition contains
the type of the value returned by the function, followed by the function name, and (optionally) a
set of arguments, separated by commas and enclosed in parentheses. An empty pair of
parentheses must follow the function name if the function definition does not include any
arguments. Its general form is:
datatype functionname(datatype 1 arg 1,datatype 2 arg 2,……………..,datatype n arg n) {
function body
}
Functions
Chapter 6 Page 2 Nawaraj Paudel
These arguments are called formal arguments, because they represent the names of the data
items that are transferred into the function from the calling portion of the program. They are also
known as parameters or formal parameters. The corresponding arguments in the function call
are called actual arguments, since they define the data items that are actually transferred.
The remainder of the function definition is a compound statement that defines the action to
be taken by the function. This compound statement is sometimes referred to as the body of the
function. It can include one or more return statements, in order to return a value to the calling
portion of the program.
Functions
Chapter 6 Page 3 Nawaraj Paudel
z = a + b;
return z;
}
Information is returned from the function to the calling portion of the program via the return
statement. The return statement also causes the program logic to return to the point from which
the function was accessed. In general terms, the return statement is written as
return expression;
Here, the value of the expression is returned to the calling portion of the program. The
expression is optional. If the expression is omitted, the return statement simply causes control to
revert back to the calling portion of the program, without any transfer of information.
Example: (Program that shows the call of same function more than one time)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,l;
int find_larger(int , int );
clrscr();
printf(“\nEnter two numbers “);
scanf(“%d %d”,&a,&b);
l = find_larger(a,b);
printf(“\nlarger = %d”,l);
printf(“\nEnter two numbers “);
scanf(“%d %d”,&c,&d);
l = find_larger(c,d);
Functions
Chapter 6 Page 4 Nawaraj Paudel
printf(“\nlarger = %d”,l);
getch();
}
int find_larger(int x, int y)
{
if(x > y)
return x;
else
return y;
}
Types of Functions
There are two types of function in C, library function and user defined functions. These
functions are discussed below:
Library Function
These functions are already available in C. These are not written by the programmer. Their
definitions are written in lib file and prototypes are defined in header file. The C language is
accompanied by a number of library functions. Some of them are:
sqrt(a) – returns square root value of a
pow(a,b) – returns a raised to the power b
abs(i) – returns absolute value of i
strlen(s) – returns length of string s
User-defined Function
These are the functions written by the programmer. In brief, user-defined function is self-
contained block of code that performs a particular task. Once a user-defined function has been
written, it can be treated as a block that can be called from other part of the program. We can
write a user-defined function in four different ways depending on its arguments and return type.
1. Functions with no arguments and no return type
2. Functions with arguments and no return value
3. Functions with arguments and return type
4. Functions with no arguments and return type
Functions
Chapter 6 Page 5 Nawaraj Paudel
Functions
Chapter 6 Page 6 Nawaraj Paudel
add(a, b);
getch();
}
void add(int x, int y)
{
int z;
z = x + y;
printf("Sum=%d", z);
}
3. Functions with arguments and return value
Passes values from actual arguments of calling function to the formal arguments of called
function and the computed value by the called function is returned back to the calling function.
For example,
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
int add(int, int);
clrscr();
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
c = add(a, b);
printf("Sum=%d",c);
getch();
}
int add(int x, int y)
{
int z;
z = x + y;
return z;
Functions
Chapter 6 Page 7 Nawaraj Paudel
}
4. Functions with no arguments but return value
These types of functions do not pass values from calling function to called function but the
computed value of the called function is returned back to the calling function. For example,
#include<stdio.h>
#include<conio.h>
void main()
{
int c;
int add();
clrscr();
c = add();
printf("Sum=%d",c);
getch();
}
int add()
{
int x,y,z;
printf("Enter two numbers:\n");
scanf("%d %d",&x,&y);
z = x + y;
return z;
}
Recursive Function
Recursion is a process by which a function calls itself repeatedly, until some specified condition
has been satisfied. The process is used for repetitive computations in which each action is stated
in terms of a previous result.
In order to solve a problem recursively, two conditions must be satisfied. First, the problem
must be written in a recursive form, and second, the problem statement must include a stopping
condition. For example, the program below gives factorial of an integer number using recursive
function.
Functions
Chapter 6 Page 8 Nawaraj Paudel
#include<stdio.h>
#include<conio.h>
void main()
int n;
scanf("%d",&n);
facto=factorial(n);
printf("%d! = %ld",n,facto);
getch();
return 1;
else
Functions
Chapter 6 Page 9 Nawaraj Paudel
Storage Classes
Every variable and function in C has two attributes: type and storage class. The four storage
classes are automatic, external, register, and static, with corresponding keywords auto, extern,
register, and static. The storage class of a variable determines which parts of the program can
access it (scope or visibility) and how long it stays existence (lifetime or duration).
Functions
Chapter 6 Page 10 Nawaraj Paudel
Functions
Chapter 6 Page 11 Nawaraj Paudel
not being executed; that is, between calls to the function. It has the visibility of local variable but
the lifetime of an external variable.
Lifetime: These variables exist for the life of the program. That is, memory space is set aside
for them when the program begins, and continues in existence until the program ends.
Visibility: Static variables are only visible within the function in which they are defined.
Initialization: If an external variable is not initialized explicitly, it is initialized
automatically to 0 when it is created.
Static external variables are only meaningful in multi-file programs. These are scope restricted
external variables. The scope is the remainder of the source file in which they are declared. Thus,
they are unavailable to functions defined earlier in the file or to functions defined in other files.
Example:
static int n; //static external variable
int test()
{
static int count = 0; //static automatic variable
count++;
return count;
}
Functions
Chapter 6 Page 12 Nawaraj Paudel
int sum = 0;
for(i = 1; i < n; i++)
sum = sum + i;
return sum;
}
The Preprocessor
Lines that begin with # are called preprocessor directives. These lines communicate with the
preprocessor. Preprocessor is a part of the compiler that deals with preprocessor directives
before real compilation process begin. Preprocessor directives are written at the beginning of the
program. They must not end with a semicolon. Only one preprocessor directive can appear in
one line. There are two types of preprocessor directives: #include and #define.
#include directive
It is also called file inclusion directive. It tells the compiler to include another file into your
source file. In effect, the #include directive is replaced by the contents of the file instead. We can
write this directive as follows:
#include<filename>
OR
#include filename
For example, #include<stdio.h>
In most cases, #include directive is used to include header files that contains various
declarations describing any library function we use in our programs. For example, for sqrt(n)
function, the header file is math.h and we use it as follows:
#include<math.h>
#define directive
It is called macro definition. Macro is used to generate inline code. Hence, macro name that
appears in a program is replaced by the string that defines the macro. Their use can reduce the
program execution time. This directive occurs in two forms: macros without arguments and
macros with arguments. These two forms have following general forms respectively.
#define identifier text
#define identifier(identifier,…, identifier) text
For example,
Functions
Chapter 6 Page 13 Nawaraj Paudel
Functions