PROGRAMMING IN C (EST102)
MODULE 4
Working with functions
Introduction to modular programming, writing functions, formal parameters,
actual parameters, Pass by Value, Recursion, Arrays as Function Parameters
structure, union, Storage Classes, Scope and life time of variables, simple
programs using functions
1
, MODULAR PROGRAMMING
Modular programming is the process of subdividing a computer program into
separate sub-programs.
Advantages
Ease of Use: This approach allows simplicity, as rather than focusing on
the entire thousands and millions of lines code, access it in the form of
modules
Programming errors are easy to detect: Minimizes the risks of
programming errors and also makes it easier to identify errors.
Allows re-use of codes: A program module is capable of being re-used
in a program which minimizes the development of redundant codes
FUNCTION
A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and can have additional
functions.
Types of Functions
There are two types of functions in C programming:
•Standard library functions
•User-defined functions
The main difference between these two categories is that library functions are
not required to be written by user whereas a user defined function has to be
developed by the user at the time of writing a program.
STANDARD LIBRARY FUNCTIONS
The standard library functions are built in functions. These functions are
defined in header files.
For example,
•printf () is a standard library function to send formatted output to the screen.
This function is defined in the stdio.h header file.
•sqrt () function calculates the square root of a number. The function is
defined in the math.h header file.
•strlen() function calculates the length of a given string. The function is defined
in the string.h header file.
2
, USER-DEFINED FUNCTIONS
User defined functions are functions that are defined by the user to do certain
specific task.
Advantages
•Program will be easier to understand, maintain and debug.
•Reusable codes that can be used in other programs
•Large program can be divided into smaller modules. Hence, a large project
can be divided among many programmers.
In order to make use of a function, three elements are required
1. Function declaration.
2. Function definition.
3. Function call.
Function Declaration
All functions in a c program must be declared, before they are invoked. A
function declaration (also known as function prototype) consists of four parts.
*Function type (return type).
*Function name.
*Parameter list.
*Terminating semicolon.
The general format of function declaration is:
Function-type function-name (parameter list);
In function declaration:
*The parameter list must be separated by commas.
*The parameter names do not need to be the same in the prototype
declaration and the function definition.
*Use of parameter names in the declaration is optional.
*If the function has no formal parameters, the list is written as void.
*The return type is optional, when the function returns int type data.
*The return type must be void if no value is returned.
Example: int addition (int a, int b);
int – function-type
addition – function-name
(int a, int b) -Parameters (arguments)
A prototype declaration may be placed in two places in a program.
3
MODULE 4
Working with functions
Introduction to modular programming, writing functions, formal parameters,
actual parameters, Pass by Value, Recursion, Arrays as Function Parameters
structure, union, Storage Classes, Scope and life time of variables, simple
programs using functions
1
, MODULAR PROGRAMMING
Modular programming is the process of subdividing a computer program into
separate sub-programs.
Advantages
Ease of Use: This approach allows simplicity, as rather than focusing on
the entire thousands and millions of lines code, access it in the form of
modules
Programming errors are easy to detect: Minimizes the risks of
programming errors and also makes it easier to identify errors.
Allows re-use of codes: A program module is capable of being re-used
in a program which minimizes the development of redundant codes
FUNCTION
A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and can have additional
functions.
Types of Functions
There are two types of functions in C programming:
•Standard library functions
•User-defined functions
The main difference between these two categories is that library functions are
not required to be written by user whereas a user defined function has to be
developed by the user at the time of writing a program.
STANDARD LIBRARY FUNCTIONS
The standard library functions are built in functions. These functions are
defined in header files.
For example,
•printf () is a standard library function to send formatted output to the screen.
This function is defined in the stdio.h header file.
•sqrt () function calculates the square root of a number. The function is
defined in the math.h header file.
•strlen() function calculates the length of a given string. The function is defined
in the string.h header file.
2
, USER-DEFINED FUNCTIONS
User defined functions are functions that are defined by the user to do certain
specific task.
Advantages
•Program will be easier to understand, maintain and debug.
•Reusable codes that can be used in other programs
•Large program can be divided into smaller modules. Hence, a large project
can be divided among many programmers.
In order to make use of a function, three elements are required
1. Function declaration.
2. Function definition.
3. Function call.
Function Declaration
All functions in a c program must be declared, before they are invoked. A
function declaration (also known as function prototype) consists of four parts.
*Function type (return type).
*Function name.
*Parameter list.
*Terminating semicolon.
The general format of function declaration is:
Function-type function-name (parameter list);
In function declaration:
*The parameter list must be separated by commas.
*The parameter names do not need to be the same in the prototype
declaration and the function definition.
*Use of parameter names in the declaration is optional.
*If the function has no formal parameters, the list is written as void.
*The return type is optional, when the function returns int type data.
*The return type must be void if no value is returned.
Example: int addition (int a, int b);
int – function-type
addition – function-name
(int a, int b) -Parameters (arguments)
A prototype declaration may be placed in two places in a program.
3