Вы находитесь на странице: 1из 7

Programming in CFunction 2012

Introduction
C functions can be classified into two categories namely, library functions and
user defined function. main is an example of user defined functions. printf and
scanf belong to the category of library functions. The main distinction between
these two categories is that library functions are not required to be written by us
whereas a user defined function has to be developed by the user at the time of
writing a program.
The use of programmerdefined functions allows a large program to be broen
down into a number of smaller, self contained components, each of which has
same unique, identifiable purpose. Thus a C program can be modulari!ed through
the intelligent use of such functions.
Library Function
C has the facility provide library functions for performing some operations. These
functions are present in C library and they are predefined. For example sqrt"# is a
mathematical library function which is used for finding out the square root of any
number. The functions scanf"# and printf"# are input output library functions.
$imilarly we have function lie strlen"#, strcmp"# for string manipulations.
To use a library function we have to include corresponding header file using the
preprocessor directive %include. For example to use input output functions lie
printf"#, scanf"# we have to include stdio.h, for mathematical library functions we
have to include math.h, for string library string.h should be included. The
following program illustrates the use of library function sqrt"#.
/*Program to find the square root of any number.*/
%include&stdio.h'
%include&conio.h'
%include&math.h'
void main"#
(
double n,s)
printf"*enter the number + ,#)
scanf"*-lf,,.n#)
1 Created By
Mr. Deependra Rastogi, Lecturer
Department of Computer Science,TMU
Programming in CFunction 2012
s/sqrt"n#)
printf"*the square root of -.0lf is + -.0lf1n,, n,s#)
getc"#)
2
3utput+
enter the number + 45
the square root of 45.66 is + 7.66
User defined function
8 function is a self contained program segment that carries out some specific, well
defined tas. 9very C program consists of one or more functions. 3ne of these
functions must be called main. 9xecution of the program will always begin by
carrying out the instructions in main. 8dditional function will be subordinate to
main, and perhaps to one another.
$o
A function is a self contained block of code that performs a particular task.!
:f a program contained a multiple functions, their definition may appear in any
order, though they must be independent of one another. That is one function
definition cannot be embedded within another.
8 function will carry out its intended action whenever it is accessed from some
other portion of the program. The same function can be accessed from several
different places within a program. 3nce the function has carried out its intended
action, control will be returned to the point from which the function was accessed.
3nce a function has been designed and paced, it can be treated as a ;blac box<
that taes the some data from the main program and return a value. The inner
details of operation are invisible to the rest of the program. 9very C program can
be designed using a collection of these blac boxes nown as functions.
"onsider a set of statements as sho#n be$o#.
%include&stdio.h'
%include&conio.h'
2 Created By
Mr. Deependra Rastogi, Lecturer
Department of Computer Science,TMU
Programming in CFunction 2012
void main"#
(
printfline"#)
printf"*the use of c functions,#)
printline"#)
getch"#)
2
void printline"void#
(
int i)
for"i/4)i&76)i==#
printf"*>>,#)
printf"*1n,#)
2
%utput&
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
the use of C program
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
8 called function can be placed either before or after the calling function.
?owever it is usual practice to put all the called functions at the end. This is called
the modular programming. @odular programming is a strategy applied to the
design and development of software system. :t is designed as organi!ing a large
program into small, independent program segment called modules that are
separately name and individually callable program units.
@odules are identified and designed such that they can be organi!ed into a top
down hierarchical structure.
3 Created By
Mr. Deependra Rastogi, Lecturer
Department of Computer Science,TMU
Main!
"
##################
##################
$unction 1!%
##################
$unction 2!%
##################
$unction 1!%
##################
&
$unction 1!
"
####################
####################
####################
&
$unction 2!
"
####################
$unction 3!
####################
####################
&
$unction 3!
"
####################
####################
####################
&
Programming in CFunction 2012
call
Aeturn
call
Aeturn
call
Aeturn
'(amp$e for this ca$$ing
%include&stdio.h'
%include&conio.h'
void main" #
(
clrscr"#)
printf " B1ni am in mainB # )
italy" # )
' Created By
Mr. Deependra Rastogi, Lecturer
Department of Computer Science,TMU
Programming in CFunction 2012
printf " B1ni am finally bac in mainB # )
getch"#)
2
void italy" #
(
printf " B1ni am in italyB # )
bra!il" # )
printf " B1ni am bac in italyB # )
2
void bra!il" #
(
printf " B1ni am in bra!ilB # )
argentina" # )
2
void argentina" #
(
printf " B1ni am in argentinaB # )
2
%utput
i am in main
i am in italy
i am in bra!il
i am in argentina
i am bac in italy
i am finally bac in main
)eed for user defined functions
8s pointed out earlier, main is a specially recogni!ed function in C. 9very
program must have a main function to indicate where the program has to begin its
execution. Chile it is possible to code any program utili!ing only main function, it
leads to a number of problems. The program may become too large and complex
and a result a tas of debugging, testing, and maintaining becomes difficult. :f a
program is divided into functional parts then each part may be independently
coded and later combined into a single unit. These independently coded programs
are called subprograms that are much easier to understand, debug, and test. :n C,
such subprograms are referred to as ;functions<.
( Created By
Mr. Deependra Rastogi, Lecturer
Department of Computer Science,TMU
Programming in CFunction 2012
There are times when certain types of operations or calculations are repeated at
many points throughout a program. For instance we might use the fractional of a
number at several points in the program. :n such situations, we may repeat the
program statements whenever they are needed. 8nother approach is to design a
function that can be called and used when ever required. This saves both time and
space.
This division approach clearly results in a number of disadvantages.
4. :t facilitates topdown modular programming as shown in fig. in this
programming style, the high level logic of the overall problem is solved first
while the details of each lowerlevel function are addressed later.
0. The length of source program can be reduced by using functions at
appropriate places. This factor is particularly critical with microcomputers
where memory space is limited.
D. :t is easy to locate and isolate a faulty function for further investigations.
7. 8 function may be used by many other programs. This means that a C
programmer can build on what other have already done, instead of starting
all over again from scratch.
'$ements of User*defined Functions
:n order to mae use of a userdefined function, we need to establish three
elements that are related to functions.
4. Function definition
0. Function call
D. Function declaration
Function +efinition
The function definition is an independent program module that is specially written
to implement the requirements of the function. :n order to use this function we
need to invoe it at a required place in the program. This is nown as the function
) Created By
Mr. Deependra Rastogi, Lecturer
Department of Computer Science,TMU
Main
*rogram
$unction
+
$unction
B
$unction
C
B1 B2
Programming in CFunction 2012
call. The program that calls the function is referred to as the calling program or
calling function.
8 function definition, also nown as function implementation shall include the
following elements)
4. Function name)
0. Function type)
D. Eist of parameter)
7. Eocal variable declaration)
F. Function statements)
5. 8 return statement
8ll the six elements are grouped into two parts, namely,
Function header"First three elements#)
Function body"$econd three elements#
8 general format of a function definition
, Created By
Mr. Deependra Rastogi, Lecturer
Department of Computer Science,TMU
$unction-type function-name parameter .ist!
"
&

Вам также может понравиться