Professional Documents
Culture Documents
Chapter 5 Function
Objectives
Functions in C enable program segments to be grouped as modules. The introduction
of function simplifies and organizes C programs structure. There are some reasons and
advantages of using functions in C. Functions would enable (i) modularity to be
implemented easily, (ii) increase programs readability by grouping of some similar tasks,
(iii) improve debugability where errors are easily identified and isolated, (iv) allows
repeatability where some segment of codes could be repeated without repeating retyping,
and (v) enables reusability where new programs are written by using as much code as
possible from old programs. At the end of this chapter, students would have learned what
is function and how to include functions in program as follow.
5-1
CHAPTER 5: FUNCTION
main( )
{
..
..
..
..
..
..
..
}
Calling program
main( )
{
func( );
func( )
{
.
.
.
Code written
only once if
function used
func( );
..
..
}
5-2
CHAPTER 5: FUNCTION
Program organization
By using the idea of subroutine, make it easier to organize programs and keep track of
what they were doing. If the operation of the program could be divided into separate
activities and each activity placed in a separate subroutine, each subroutine could be
written and checked out more or less independently. Separating the code into modular
functions also made programs easier to design and understand.
Independence
By making subroutines as independent from the main program and from one another
possible. For instance, subroutines were invented that had their own private that is,
variables that couldnt need to worry about accidentally using the same variable names in
different subroutines; the variables in each subroutine were protected from inadvertent
tampering by other subroutines. Thus is was easier to write large and complex programs.
{
statement;
statement;
..
.
}
5-3
CHAPTER 5: FUNCTION
Explanation:
Take note that the declarator doesnt end with a semicolon. It is not a program statement;
it tells the compiler that a function is being defined. The function definition continues
with the body of the function: the program statements that do the work. The body of the
function is enclosed in braces. In the above example, the func is a user-defined function
name.
5.2.2 Calling the Function
As with the C library functions we have seen, such as printf( ) and getche( ), our userwritten function func( ) is called from main( ) simply by using its name, including the
parentheses following the name. The parentheses let the compiler know that you are
referring to a function and not to a variable or something else. Calling a function like this
is a C statement, so it ends with a semicolon.
Example:
void main( )
{
..
func( );
..
.
}
5-4
CHAPTER 5: FUNCTION
Example:
void func(void );
The key thing to remember about the prototype is that the data type of the return value
must agree with that of the declarator in the function definition, and the number of
arguments and their data types must agree with those in the function definition.
{
int x, y;
5-5
CHAPTER 5: FUNCTION
return (x);
}
The return statement has two purposes. First, executing it immediately transfers control
from the function back to the calling program. Second, whatever is inside the parentheses
following return is returned as a value to the calling program. The return statement need
not be at the end of the function; as soon as its encountered, control will return to the
calling program. The return statement can also be used without a value following it, and
no value would be returned to the calling program. The return statement can only be used
to return a single value.
5-6
CHAPTER 5: FUNCTION
funct(10);
.
.
10
void funct(int x)
{
}
Calling program
Function
5-7
CHAPTER 5: FUNCTION
10
void main(void)
{
funct(10, 20);
.
.
20
}
Calling program
Function
5-8
CHAPTER 5: FUNCTION
Example:
#include <stdio.h>
void func1(void);
void func2(void);
int x, y;
void main(void)
{
..
..
}
5-9