Professional Documents
Culture Documents
Functions-Modular programming
INTRODUCTION
C functions can be classified into two categories, namely,
1) Library functions and
2) User-defined functions.
main() is an example of user-defined functions. Printf() and scanf() belong to the category of
library functions.
Main program
Function A
Function B
Function B1
Function C
Function B2
A MULTI-FUNCTION PROGRAM
A function is a self-contained block of code that performs a particular task. Once a function has
been designed and packed, it can be treated as a black box that takes some data from the
main program and returns a value. The inner details of operation are invisible to the rest of the
program. All that the program knows about functions is: what goes in and what comes out.
Every C program can be designed using a collection of these black boxes known as functions.
DEFINITION OF FUNCTIONS
A function definition, also known as function implementation shall include the following
elements:
1. Function name;
2. Function type;
3. List of parameters;
4. Local variable declarations;
5. Function statements; and
6. A return statement.
All the six elements are grouped into two parts, namely,
Function header (First three elements); and
Function body (Second three elements).
A general format of a function definition to implement these two parts is given below:
Function return_type function name(parameter list)
{
Local variable declaration;
Executable satatement1;
Exectable statement2;
..............
. . . . . . .. . .. . . . .
Return statement;
}
The first line function return_type function_name(parameter list) is known as the
function header and the statements within the opening and closing braces constitute the functon
body, which is a compound statement.
Function Header
2
The function header consists of three parts: the function type (also known as return type), the
function name and the formal parameter list. Note that a semicolon is not used at the end of the
function header.
Function Body
The function body contains the declarations and statements necessary for performaing the
required task. The body enclosed in braces, contains three parts, in the order given below:
1. Local declarations that specify the variables needed by the function.
2. Function statements that perform the task of the function.
3. A return statement that returns the value evaluated by the function .
If a function does not return any value (like the printline function , we can omit the return
statement.
FUNCTION DECLARATION
Like variables, all functions in a C program must be declared, before they are invoked. A
function declaration also known as function prototype) consists of foure parts.
Function type (return type).
Function name.
Parameter list.
Terminating semicolon.
They are coded in the following format:
Function-type function-name (parameter list );
3
This is very similar to the function header line except the terminating semicolon. For example,
mul function defined in the previous section will be declared as:
Int mul (int m, int n); /* Function prototype */
Points to note:
1. The parameter list must e separated by commas.
2. The parameter names do not need to be the same in the prototype declaration and the
function definition.
3. The types must match the types of parameters in the function definition, in number and
order.
4. Use of parameter names in the declaration is optional.
5. If the function has no formal parameters, the list is written as (void).
6. The return type is optional, when the function returns int type date
7. The retype must be void if no value is returned.
8. When the declared types do not match with the types in the function definition, compiler
will produce an error.
Calling programs pass information to called functions in "actual arguments." The called
functions access the information using corresponding "formal arguments."
When a function is called, the following tasks are performed:
All actual arguments (those supplied by the caller) are evaluated. There is no implied
order in which these arguments are evaluated, but all arguments are evaluated and all
side effects completed prior to entry to the function.
Each formal argument is initialized with its corresponding actual argument in the
expression list. (A formal argument is an argument that is declared in the function header and
used in the body of a function.) Conversions are done as if by initialization both standard
and user-defined conversions are performed in converting an actual argument to the correct
type. The initialization performed is illustrated conceptually by the following code:
CATEGORY OF FUNCTIONS
A function, depending on whether arguments are present or not and whether a value is returned
or not, may belong to one of the following categories:
Category 1:
Category 2:
Category 3:
Category 4:
Category 5:
In the sections to follow, we shall discuss these categories with examples. Note that,
from now on, we shall use the term arguments (rather than parameters) more frequently:
4
Two-Dimensional Arrays
Like simple arrays, we can also pass multi-dimensional arrays to functions. The approach is
similar to the one we did with one-dimensional arras. The rules are simple.
1. The function must be called by passing only the array name.
2. In the function definition, we must indicate that the array has two-dimensions by
including two sets of brackets.
3. The size of the second dimension must be specified.
4. The prototype declaration should be similar to the function header.
utility of a given variable. The visibility refers to the accessibility of a variable from the
memory.
The variables may also be broadly categorized, depending on the place of their
declaration, as internal (local) or external (global). Internal variables are those which are
declared within a particular function, while external variables are declared outside of any
function.
Automatic Variables
Automatic variables are declared inside a function in which they are to be utilized. They are
created when the function is called and destroyed automatically when the function is exited,
hence the name automatic. Automatic variables are therefore private (or local ) to the
function in which they are declared. Because of this property, automatic variables are also
referred to as local or internal variables.
A variable declared inside a function without storage class specification is, by default, an
automatic variable. For instance, the storage class of the variable number in the example
below is automatic.
main ()
{
Int number;
--------------------------}
We may also use the keyword auto to declare automatic variables explicitly.
main ()
{
Auto int number;
--------------------------------}
One important feature of automatic variables is that their value cannot be changed
accidentally by what happens in some other function in the program. This assures that we
may declare and use the same variable name in different functions in the same program
without causing any confusion to the compiler.
There are two consequences of the scope and longevity of auto variables worth
remembering. First, any variable local to main will be normally alive throughout the whole
program, although it is active only in main. Secondly , during recursion, the nested
variables are unique auto variables, a situation similar to function-nested auto variables
with identical names.
External Variables
Variables that are both alive and active throughout the entire program are known as
external variables. They are also known as global variables. Unlike local variables, global
variables can be accessed by any function in the program. External variables are declared
7
outside a function. For example, the external declaration of integer number and float
length might appear as:
int number;
float length = 7.5;
main ()
{
------------------------------}
function1()
{
------------------------------}
function2 ()
------------------------------}
The variables number and length are available for use in all the three functions. In case
a local variable and a global variable have the same name, the loval variable will have
precedence over the global one in the function where it is declared. Consider the following
example:
Int count;
main ()
{
count = 10;
------------------------------}
function()
{
int count = 0;
------------------------------count = count+1;
}
When the function references the variable count, it will be referencing only its local
variable, not the global one. The value of count in main will not be affected.
Static Variables
As the name suggests, the value of static variables persists until the end of the program. A
variable can be declared static using the keyword static like
static int x;
static float y;
A static variable may be either an internal type or an external type depending on the
place of declaration.
Internal static variables are those which are declared inside a function. The scope of
internal static variables extend up to the end of the function in which they are defined.
Therefore, internal static variables are similar to auto variables, except that they remain in
existence (alive) throughout the remainder of the program. Therefore, internal static variables
can be used to retain values between function calls. For example, it can be used to count the
number of calls made to a function.
Program
main()
{
int I;
for (i=1; i<=3; i++)
stat();
}
void stat (void)
{
static int x = =0;
X = x+1;
printf (x = %d\n, x);
}
Output
X=1
X=2
X=3
A static variable is initialized only once, when the program is compiled. It is never initialized
again. During the first call to stat, x is incremented to 1.Because x is static, this value
persists and therefore, the next call adds another 1 to x as an auto variable, the output
would have been:
X=1
X =1
X=1
This is because each time stat is called; the auto variable x is initialized to zero. When the
function terminates, its value of 1 is lost.
An external static variable is declared outside of all functions and is available to all the
functions in that program. The difference between a static external variable and a simple
external variable is that the static external variable is available only within the file where it
is defined while the simple external variable can be accessed by other files
9
Register Variables
We can tell the compiler that a variable should be kept in one of the machines registers,
instead of keeping in the memory (where normal variables are stored). Since a register
access is much faster than a memory access, keeping the frequently accessed variables
(e.g. loop control variables) in the register will lead to faster execution of programs. This is
done as follows:
Register in count:
Although, ANSI standard does not restrict its application to any particular data type, most
compilers allow only int or char variables to be placed in the register.
Since only a few variables can be placed in the register, it is important to carefully select
the variables for this purpose. However, C will automatically convert register variables into
non-register variables once the limit is reached.
Nested Blocks
A set of statements enclosed in a set of braces is known a block or a compound statement.
Note that all functions including the main use compound statement. A block can have its
own declarations and other statements. It is also possible to have a block of such
statements inside the body of a function or another block, thus creating what is known as
nested blocks as shown below:
main()
{
Int a = 20;
Int b = 10;
. . . . .
{
Int a = 0;
Int c = a + b;
. . . . .
}
B = a;
}
When this program is executed, the value c will be 10, not 30. The statement b = a;
assigns a value of 20 to b and not zero. Although the scope of a extends up to the end of
main it is not visible inside the inner block where the variable a has been declared again.
The inner a hides the visibility of the outer a in the inner block. However when we leave the
inner block, the inner a becomes visible again.
Scope Rules
Scope
The region of a program in which a variable is available for use.
Visibility
The programs ability to access a variable from the memory.
Lifetime
The lifetime of a variable is the duration of time in which a variable exists in the memory
during execution
Rules of use
1. The scope of a global variable is the entire program file.
10
2. The scope of a local variable begins at point of declaration and ends at the end of
the block or function in which it is declared.
3. The scope of a formal function argument is its own function.
4. The lifetime (or longevity) of an auto variable declared in main is the entire program
execution time, although its scope is only the main function.
5. The life of an auto variable declared in a function ends when the function is exited.
6. A static local variable, although its scope is limited to its function, its lifetime extends
till the end of program execution.
7. All variables have visibility in their scope, provided they are not declared again.
8. If a variable is redeclared within its scope again, it loses its visibility in the scope of
the redeclared variable.
Example:
int Fact(int);
main()
{
printf(%d,Fact(3));
}
int Fact(int N)
{
if( N <= 1) return 1;
else
return N*Fact(N-1);
}
Factorial Example
The recursive definition of the factorial function is:
0! = 1
n! = n * (n-1)!
Note that:
(n-1)! = (n-1) * (n-2)!
so for:
n=3
n=2
n=1
n=0
3!
2!
1!
0!
=
=
=
=
3*2!
2*1!
1*0!
1 by definition
11
Features :
Advantages :
It is easy to use.
Disadvantages :
Note :
TOWER OF HANOI
The tower of Hanoi problem is a game, which comprises of three towers with discs stacked
around the first tower initially. The game involves shifting the disks across the adjoining towers.
There are some conditions that need to be adhered during the shifting:
1. A larger disk can not be placed on a smaller disk
2. Only one disc can be shifted at a time.
The end objective is to replicate the initial stack of discs into another tower.
PREPROCESSOR COMMANDS
Preprocessor commands, as the name suggests, are the instructions that are executed before
the source code passes through the compiler. The program that processes the preprocessor
command lines or directives is called a preprocessor.
Preprocessor directives are placed in the source program before the main line. Before
the source code passes through the compiler, it is examined by the preprocessor for any
preprocessor directives. If there are any, appropriate actions (as per the directives) are taken
and then the source program is handed over to the compiler.
12
Preprocessor directives follow special syntax rules that are different from the normal C
syntax. They all begin with the symbol # in column one and do not require a semicolon at the
end.
Directive
#define Defines a macro substitution
#undef Undefines a macro
#include Specifies the files to be included
#ifdef
Test for a macro definition
#endif
Specifies the end of #if.
#ifndef Tests whether a macro is not defined.
#if
Test a compile-time condition
#else Specifies alternatives when #if test fails.
Function
COUNT
100
#define
FALSE
#define
SUBJECTS
#define
PI
3.1415926
#define
CAPITAL
DELHI
Notice that we have written all macros (identifiers) in capitals. It is a convention to write all
macros in capitals to identify them as symbolic constants. A definition, such as
#define
5
13
Will replace all occurrences of M with 5, starting from the line of definition to the end of the
program. However, a macro inside a string does not get replaced. Consider the following two
lines:
Total = M * value;
Printf (M = %d\n, M);
These two lines would be changed during preprocessing as follows:
Total = 5 * value;
Printf(M = %d\n, 5);
Notice tat the string M=%d\n is left unchanged.
A macro definition can include more than a simple constant value. It can include expressions as
well.
Following are valid definitions:
#define
AREA
5 * 12.46
#define
SIZE
sizeof(int) * 4
#define
TWO-PI
2.0 * 3.1415926
#define
string
identifier(f1,f2,fn)
Notice that there is no space between the macro identifier and the left parentheses. The
identifiers f1,f2,,fn are the formal macro arguments that are analogous to the
formal arguments in a function definition. There is a basic difference between the simple
replacement discussed abve and the replacement of macros with arfuments. Subsequent
occurrence of a macro with arguments is known as a macro call (similar to a function call).when
a macro is called, the preprocessor substitutes the string, replacing the formal parameters with
the actual parameters. Hence, the string behaves like a template.
A simple example of a macro with arguments is
#define
CUBE(x)
#define
M+1
#define
SQUARE(x)
((x) * (x))
14
#define
CUBE(x)
#define
SIXTH(x)
(CUBE(x) * CUBE(x))
The preprocessor expands each #define macro, until no more macros appear in the text. For
example, the last definition is first expanded into
((SQUARE(x) * (x)) * (SQUARE(x) * (x)))
Since SQUARE (x) is still a macro, it is further expanded into
((((x)*(x)) * (x)) * (((x) * (x)) * (x)))
An external file containing functions or macro definitions can be included as a part of a program
so that we need not rewrite those functions or macro definitions. This is achieved by the
preprocessor directive
#include filename
Where filename is the name of the file containing the required definitions or functions. At this
point, the preprocessor inserts the entire contents of filename into the source code of the
program. When the filename is included within the double quotation marks, the search for the
file is made first in the current directory and them in the standard directories.
Alternatively this directive can take the form
#include<filename>
Without double quotation marks. In this case, the file is searched only in the standard
directories. Nesting of included files is allowed. That is, an included file can include other files.
However, a file cannon include itself.
If an included file is not found, an error is reported and compilation is terminated.
Let use assume that we have created the following three files:
SYNTAX.C
STAT.C
TEST.C
We can make use of definition or function contained in any of these files by includeing them in
the program as:
#include
<stdio.h>
#include
SYNTAX.C
#include
STAT.C
#include
TEST.C
#define
main ()
{
..
..
..
15
100
16