Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 70

CC 103 – Intermediate Programming

Modular Programming
Using Functions in C
DAISY JEAN A. CASTILLO
Instructor
01 Types of Functions

02 Advantages of using 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

Lessons 10 Scope of visibility

11 Types of storage classes

12 Recursion
Modularity
Modular Programming Using Functions in C

action
Modular Programming Using Functions in C

Functions are basically special type of programs, which perform a


specific task, but don’t work independently, as main function does.
Main Function

Main Function
Concept of Function
Calling Program Data

Call the function Function

Called
Calling Program
Program
Return

Value
C program with sumlist function which adds
given 3 numbers.
Standard Library C Functions

• Standard C library functions, like printf, scanf, getchar, putchar, etc.


remain available in standard libraries like <stdio.h. etc.
• Note that many more functions like pow(a,b), sqrt(n) etc. remain
available in other libraries of C.
Need for User Defined Function
Large Problem’s Program Large Problem’s Program
Not Making Use of Functions Making Use of 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

1. Function Data Type


• Function data type is data type of that value, which function return
2. Function Name
• Function name is the actual name of function, which is assigned for the
purpose of function’s identification and calling.
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

Step 1. Define the function


Step 2. Call the function in some other program.
Function name Argument/parameter name

Function return type


Argument/parameter type
Function
definition

C program with sumlist function which adds


given 3 numbers.
Function Call

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.

function name (argument list);


Function
Call

C program with sumlist function which adds


given 3 numbers.
Return Statement

• The body of the function can terminate with a return statement.


• If function doesn’t return any value to the calling function, either
return statement is omitted or a simple statement, “return” is written.
• On the other hand, if the function returns a value to the calling
function, statement “return(value)” is written. Value could be a
variable or expression, which evaluates to the value that need to be
sent back to the calling program.
Return Statement

• 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

values are inaccessible outside


the function
Categories of Functions in C

1. Functions with no arguments and no return value

2. Functions with arguments and no return value

3. Functions with arguments and one return value

4. Functions with no arguments and one return value

5. Functions that return multiple values


1. Functions with no
arguments and no return
value
As the name indicates, these functions
neither take any input from the main
function, nor they return any value to it.
1. Functions with no
arguments and no return
value
As the name indicates, these functions
neither take any input from the main
function, nor they return any value to it.
C program with header function that has no arguments and return value
C program which accepts roll number, name and age of 5 students. If the age
of any student is found to be less than 21 years, a function named remarks
prints “Not eligible”
2. Functions with
arguments and no return
value
These functions receive values from the
calling function. They do not send back
any value to the calling program.
C program which accepts 5 numbers from the user and print its square and cube
through a function.
3. Functions with
arguments and one
return value
Such functions receive values from the
calling program through arguments.
They return the result to the calling
program.
4. Functions with no
arguments and one
return value
Such functions do not get any input
from the calling program but still
provide some value to it.
5. Functions that return
multiple values
Returning multiple values from a function to
the calling program made possible by making
some additions in the argument list and
sending them back through argument list
itself.
CC 103 Chapter 1 Activity 2
• Create a C Salary Computation App
• Create 3 additional functions for this app
• The first function is grossSalary, that will handle the
computation of gross salary.
• The second function is salaryDeductions, that will
handle the computation of total deduction
• The last function is netSalary will be responsible for
computing the net salary.
• The user will input the following (Name, hour, loan,
health insurance)
• Tax (12% of the gross salary) and rate(500/hr) is
fixed)
Scope of Visibility
• The variables that you declare within the calling function (say main())
cannot be accessed by function.
• The variables that you declare within the function remain inaccessible
in calling function.
• The variables that you declare in function loose their identity and
value as soon as the function ends. Their values cannot be retained for
utilization.
1. Automatic Variables

Automatic variables, are the variables, which


come into existence ,when the function starts
and loose their existence when the function
ends. Their values are inaccessible in other
function.
1. Automatic Variables

If you do not qualify the declaration of a


variable, with any storage class, by default, it
will get declared as automatic.
2. External Variables

External variables come into existence when


they are declared and remain alive till the end
of the program (complete program not only in
the function). Use of extern keyword.
2. External Variables

Without extern keyword as long as declared


outside the functions.
3. Static Variables

Static variables are the variables, which


remain local to the function. Their values
cannot be accessed outside the function. They
come into existence till the end of the
program.
Recursion
Recursion
Recursion
• In recursion, a function calls itself repeatedly until some specified
condition is satisfied.
• This process is used for repetitive computation, in which action of
current iteration is framed on the basis of the result of previous
iteration.
Recursion Sample 1 - C program which we calculate the factorial of any
number with the use of a recursive function.
Power 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!

You might also like