Professional Documents
Culture Documents
Chapter 1
Chapter 1
Modular Programming
Using Functions in C
DAISY JEAN A. CASTILLO
Instructor
01 Types of Functions
Elements of user-defined
03
Lessons
function
04 Using functions
05 Definition of function
06 Function Call
07 Return Statement
08 Function Declaration
09 Category of functions
12 Recursion
Modularity
Modular Programming Using Functions in C
action
Modular Programming Using Functions in C
Main Function
Concept of Function
Calling Program Data
Called
Calling Program
Program
Return
Value
C program with sumlist function which adds
given 3 numbers.
Standard Library C Functions
Function 4 Function 1
main() main()
function
function
Function 3 Function 2
Advantages of Using Functions
1. Modularity
• With the use of functions, modularity gets automatically incorporated in the
program. Thus a single program gets divided into small modules, obviously
maintaining small modules is much easier than maintaining a single large
program.
Advantages of Using Functions
2. Reusability
• If function is being used then to perform the same function again within the
program, the code need not be written again. Only one line for calling the
function can be written, which would perform the task. This not only saves
effort and time but also reduces the length of the program.
Advantages of Using Functions
2. Reusability
• Since function written for one program, can be used in other programs also
hence programming becomes easy. Time and efforts reduce to a considerable
extent.
D on’t
R epeat
Y ourself
keep your code
D R Y
Elements of User-Defined Function
3. Argument/Parameter list
• Argument list is basically a list of variables along with their data types. These
are the variables in which function receives the data from the calling program.
4. Body of the function
• Body of the function comprises of all those C statements, which are to be
executed, within the function.
Elements of User-Defined Function
Function name
Function data type Argument list
Body of
the function
Using Functions
Now the question is, how do you call a function, which has already been
defined? Well, a function can be called by writing a statement of the
following type, in the calling program.
• Note that it is not necessary that function should always return a value
to the calling program. It may or may not return the value, depending
upon the requirement.
• Limitation with return statement is that it can return only a single
value to the calling program. Multiple values cant be returned through
it. For example “return(a,b)’ is a wrong statement.
C program with sumlist function with return
variable statement which adds given 3 numbers.
C program with sumlist function with return
expression statement which adds given 3 numbers.
C program with max function with return expression statement
which receives two integer values from the main function and
returns back the larger value to it
max function with return expression which returns the larger value
to it
C program with max function with return expression statement
which receives two integer values from the main function and
returns back the larger value to it
Function Declaration
• Now you know that to make use of function, you have to have a
function and a calling program(function).
• Which sequence these two items should be placed?
• Should the definition be placed before the calling function or after
calling function?
• C language supports both!
Placing the Function
Definition Before
Calling Program
Function
Prototype
Main
Placing the Function Function
Definition After the
Calling Program with
Function Prototype Function
Definition
Placing the Function
Definition After the
Calling Program
without function
prototype
CC 103 Chapter 1 Activity 1
• Write a C program that computes students average.
• Create and use a function with 4 parameters(Name, MathGrade,
EnglishGrade and ScienceGrade)
• Call the function 3 times with different values.
Sample Output:
John’s Grade (Math=?, English=?, Science=? and the average is)
Ana’s Grade (Math=?, English=?, Science=? and the average is)
Frank’s Grade (Math=?, English=?, Science=? and the average is)
Calling Function By Value (Function Call
Technique)
Calling Function By Value (Function Call
Technique)
2 Categories of C Variables in Function
LOCAL VS EXTERNAL
remain accessible within the
function, in which they are values are accessible within and
declared outside the function
2^3
= 2*2*2
Recursion Sample 2 - C program which accepts base and exponent from the
user and print its power value using the power function.
End of Chapter 1.
Thank you!