Part 4
Part 4
Introduction
One of the strengths of C-language is functions. A function is a sub-program which performs a specific
task.
In ‘C’, the functions can be classified in to two categories namely: User defined functions and library
functions.
The library functions are also known as “in-built” functions which are used to carry out number of
commonly used operations.
This function doesn’t require any user help. In order to use library function in a program. We have to
include the corresponding header file where the library function is available.
In some situations these built-in functions are not sufficient for a number of applications. Therefore, it
is necessary to develop a function that has been defined by the user. Such functions are known as “user-
defined” functions.
Modular Programming:
Writing to any programming language it is structured principle to divide a larger task in to a number of
sub tasks. Each sub task is referred to as a module. In ‘C’ these modules are known as functions.
Main program
Fun-4 Fun-5
Advantages of a function:
There are number of advantages by using functions. Some of the important advantages of the function
are :-
The use of a function avoids the need for repeat of same program instructions.
It facilitate top down modular programming.
It is easy to read, write and debugging a function.
It is easier to maintain and modify a function.
The length of the source program can be reduced by using functions at appropriate places.
It is easy to locate and isolate functions for further investigation.
Small functions tend to be self documenting and highly retable.
Top-down Design
The concept of top-down is demonstrated by process. When process is called, it calls A, B and C in turn.
Functions B does not start running, how-ever, until A is finished. While A is running, it calls A1 and A2 In
turn. In other words, all functions in a line from process to A2 must be called before Function B can start.
A structure chart shows only the function flow through the program. It is not a block diagram or a
flow- chart. Structure charts show only function flow; they contain no code
Rules:
1. Each rectangle in a structure chart represents a function written by the programmer. Standard C
functions are not included.
2. The name in the rectangle is an intelligent name that communicates the purpose of the function. It is the
name that will be used in the coding of the function.
3. Each rectangle in a structure chart represents a function written by the programmer. Standard C
functions are not included.
4. The name in the rectangle is an intelligent name that communicates the purpose of the function. It is the
name that will be used in the coding of the function.
5. The function chart contains only function flow. No code is indicated.
6. Common function chart contains are indicated by a cross- hatch or shading in the lower right corner of
the function rectangle.
7. Common calls are shown in a structure wherever they will be found in the program. If they contain sub
function calls, the complete structure need be shown only once.
8. Date flows and flags are optional. When used, they should be named.
9. Input flows and flags are shown on the left of the vertical line; output flows and flags are shown on the
right.
Defining a function:
A function is a self-contained block of code that performs specific function. Once a function has been
designed it can be treated as a “Black Box”.
A function definition contains a name a parenthesis pair containing zero (or) more parameters and a body.
For each parameter there should be a corresponding declaration that occurs before the body.
Any parameter not declared is considered to be int by default.
It is a good programming practice to declare all parameters. The general format of function definition is as
follows.
Function name:
The function name can be any name following the rules of the variables. Normally a function name is
made relevant to the function operation as it will be easy to keep track of it whenever a transfer of similar
functions is used in the main program.
Formal Arguments:
Any variable declared in the body of a function is said to be local to that function. Other variables
which are not declared as arguments or in the function body are considered as global to the function and
must be defined externally.
Function Body:
After declaring the type of a function, function name and formal arguments a statement (or) block of
statements is enclosed between beginning and end statements.
Return Statements:
The keyword return is used to terminate function and it also returns a value to its caller. The return
statement may also be used to exit a function without returning any value. The return statement may (or)
may not include any expression.
The general form syntax of Return statement is as follows.
return;
return (expression);
The return in a C programs can appear anywhere within the function body. A function can also have
more than one return statement.
Types of Functions:
The user defined functions may be classified in to three ways based on the formal arguments
passed and usage of return statement. The three types are listed below.
A function is invoked without passing any formal arguments from the calling portion of a program and also
the function does not return back any valued to the called function.
A function is invoked without passing any formal arguments from the calling portion of a program and the
function returns back a value to the called function.
A function is invoked with formal arguments from the calling portion of the program, but the function
doesn’t return back any value to the calling portion.
A function is invoked with formal arguments from the calling portion of a program which returns back a
value to the calling environment.
Actual Arguments:
An actual argument is an expression (or) a variable contained in a function call that replaces the formal
parameter which is a part of function declaration.
Sometimes a function may be called by a portion of program with some parameter and these parameters are
known as actual arguments.
Formal Arguments:
These are the parameters present in a function definition which may also be called as “dummy
arguments” (or) the “parametric variables”. When the function is invoked the formal parameters are
replaced by the actual parameters.
Global Variables:
These variables are declared outside the main function. They are referred by the same data type and by
the same name through out of program ie, both in calling portion of the program and in function definition
block. Hence, the scope of global variables is throughout the program.
Passing Values Between Functions:
int mul(int,int,int); // Function Prototype
void main()
{
int x,y,z,k;
printf(“Enter three values”);
scanf(“%d%d%d”,&x,&y,&z);
k=mul(x,y,z); // Function Calling
printf(“Product=%d”,k);
}
int mul(int a,int b,int c) // Function Definition
{
int t;
t=a+b+c;
return(t);
}
Function prototype gives prior information to the compiler about the function name, the function return
type and also about the type, order and number of arguments of the function. This is also known as forward
declaration of a function (or) simply function declaration.
The variables which are declared in the main program are passed to the function definition by the calling
the function and mentioning these variables within the parenthesis. I.e., k= mul (x, y, z).
In the function definition these values get collected in the three variables a, b, c respectively.
Note that the type, order, number of actual parameters and formal parameters must always be same.
Instead of using different parameter names {a, b, c} in function definition, we can use the same variable
names {x, y, z} in function definition. Even though we are using the same parameter names, compiler would
still treat them as different variables since, they are in different functions.
We have two methods of declaring formal arguments.
int mul (a, b , c) ii) int mul ( int a, int b, int c)
int a, b, c; { }
{ }
The second method of declaration is most commonly used.
In order to sent values from the function to main () i.e., from function definition to function calling
environment, we use return statements.
There is no restriction of number of return statements and the place of return statement.
It is important to note that a function can return only one value at a time.
Call-by-reference:
When we call the sub program, in these techniques the formal parameters are replaced by addresses of
actual parameters.
By using these addresses, we would have access to the actual parameters and hence we would able to
manipulate them.
In this technique, any changes made to the formal parameters will affect the values of actual
parameters. i.e., the changed value from the function is returned to the main ().
Recursion
In ‘C’ it is possible for the function to call themselves.
A function is called “recursive” if a statement with in a body of a function calls the same function.
Sometimes it is also called a circular definition.
Recursion is thus the process of defining something in terms of itself.
If a problem is to solve by using recursion, then it should satisfy the following two criterias:
o The problem solution should be in recursive format. i.e., whatever the process involved in the
first step of the solution should be the same for further steps.
o It should have a termination condition.
Advantages of Recursion
Recursion solves the problem in the most general way as possible
Recursive function is small, simple and more reliable than other coded versions of the programs.
Recursion is used to solve the more complex problems that have repetitive structure.
Recursion is more useful because as most of the problems are naturally recursive in nature.
For some problems, it is easy to code as well as understand the problem using the recursion. Ex:-
Towers of Hanoi.
Disadvantages of Recursion:-
Usage of recursion incurs more over head, since this technique is implemented using function calls.
Whenever a recursive call is made, some of the system’s memory is consumed. As such additional
memory space is required to solve the problem using recursion.