22-11-2012, 03:38 PM
FUNCTIONS IN ‘C’
FUNCTIONS IN ‘C’.pdf (Size: 72.72 KB / Downloads: 214)
INTRODUCTION
In the earlier lessons we have already seen that C supports the use
of library functions, which are used to carry out a number of commonly
used operations or calculations. C also allows programmers
to define their own functions for carrying out various individual tasks.
In this lesson we will cover the creation and utilization of such userdefined
functions.
OBJECTIVES
After going through this lesson you will be able to
l explain of function
l describe access to function
l define parameters data types specification
l explain function prototype and recursion
l define storage classes – automatic, external, static variables
MODULAR APPROACH
The use of user-defined functions allows a large program to be broken
234 :: Computer Applications
down into a number of smaller, self-contained components, each of
which has some unique, identifiable purpose. Thus a C program
can be modularized through the intelligent use of such functions.
There are several advantages to this modular approach to program
development. For example many programs require a particular group
of instructions to be accessed repeatedly from several different places
within a program. The repeated instruction can be placed within a
single function, which can then be accessed whenever it is needed.
Moreover, a different set of data can be transferred to the function
each time it is accessed. Thus, the use of a function avoids the need
for redundant (repeating) programming of the same instructions.
The decomposition of a program into individual program modules is
generally considered to be an important part of good programming.
15.4 DEFINING A FUNCTION
The question arises what is a function? So, function is a self-contained
program segment that carries out some specific well-defined
task. Every C program consists of one or more functions. The most
important function is main. Program execution will always begin by
carrying out the instruction in main. The definitions of functions
may appear in any order in a program file because they are independent
of one another. A function can be executed from anywhere
within a program. Once the function has been executed, control will
be returned to the point from which the function was accessed.
Functions contains special identifiers called parameters or arguments
through which information is passed to the function and
from functions information is returned via the return statement. It
is not necessary that every function must return information, there
are some functions also which do not return any information for
example the system defined function printf.
Before using any function it must be defined in the program. Function
definition has three principal components: the first line, the
parameter declarations and the body of the functions.
The first line of a function definition contains the data type of the
information return by the function, followed by function name, and
a set of arguments or parameters, separated by commas and enclosed
in parentheses. The set of arguments may be skipped over. The data
type can be omitted if the function returns an integer or a character.
An empty pair of parentheses must follow the function name if
the function definition does not include any argument or parameters.
Functions in ‘C’ :: 235
The general term of first line of functions can be written as:
data-type function-name (formal argument 1, formal argument
2…formal argument n)
The formal arguments allow information to be transferred from the
calling portion of the program to the function. They are also known
as parameters or formal parameters. These formal arguments are
called actual parameters when they are used in function reference.
The names of actual parameters and formal parameters may be either
same or different but their data type should be same. All formal
arguments must be declared after the definition of function. The
remaining portion 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.
This compound statement can contain expression statements,
other compound statements, control statements etc. Information is
returned from the function to the calling portion of the program via
the return statement. The return statement also causes control to
be returned to the point from which the function was accessed.
In general terms, the return statement is written as
return expression;
The value of the expression is returned to the calling portion of the
program. The return statement can be written without the expression.
Without the expression, return statement simply causes control
to revert back to the calling portion of the program without any
information transfer. The point to be noted here is that only one
expression can be included in the return statement. Thus, a function
can return only one value to the calling portion of the program via
return. But a function definition can include multiple return statements,
each containing a different expression. Functions that include
multiple branches often require multiple returns.
It is not necessary to include a return statement altogether in a
program. If a function reaches the end of the block without encountering
a return statement, control simply reverts back to the calling
portion of the program without returning any information.