Topic 5 Function
Topic 5 Function
CSC128: TOPIC 5
FUNCTIONS
2
LEARNING
OUTCOMES
✓ Predefined functions and how to use
them in a program
✓ User-defined functions
✓ Function’s data type: void, int, float,
double, char, char[]
✓ Learn how to use function with parameter
(value and reference)
✓ The scope of global and local variables
3
INTRODUCTION
What is a
FUNCTION?
Function is a group of statements that will
perform a particular task.
For example:
✓ To calculate the area of a rectangle
✓ To display the total price of items
purchased
4
Pre-defined User-defined
Functions Functions
9
CATEGORIES PREDEFINED
OF
FUNCTIONS FUNCTIONS
10
PREDEFINED FUNCTIONS
✓ Built-in functions
✓ Allows you to use existing functions located inside the
libraries, so that you don’t have to write any code to
perform a certain task
✓ To use predefined functions in your program, you need to:
1. Use pre-processor directives
2. Include appropriate header file
11
PREDEFINED FUNCTIONS - EXAMPLE
Write a program that will receive a number and by using that
𝟑
number, calculate “ 𝒏𝒖𝒎𝒃𝒆𝒓”
Will CALL the predefined
function named “pow” inside the
“cmath” header file
PREDEFINED FUNCTIONS - cmath
<cmath>
Allow us to use mathematical functions inside a program
• pow(x, y) x to the power of y
• sqrt(x) square root of x
• log(x) natural log of x
• sin(x) sine of x
• cos(x) cosine of x
• tan(x) tangent of x
• abs(x) absolute value of x
Must be in a floating-point expression
PREDEFINED FUNCTIONS - iomanip
13
<iomanip>
Allows you to manipulate how your output is displayed
CATEGORIES
USER
OF DEFINED
FUNCTIONS FUNCTIONS
16
USER-DEFINED FUNCTIONS
There are 3 TYPES of user-defined functions:
✓ Function with a return value
▪ (int, float, double, char, char[])
Let say you want to calculate the average of total marks of students:
Declare the function CalcAverage()that will
receive 1 floating-point value ,1 integer value, and
will return a floating-point value
Let say you want to calculate the wage of an employee:
Declare the function CalcWage()that will receive 1
integer value and 1 floating-point value, and will return a
floating-point value
23
PART A PRACTICE 1
OCT 2016 QUESTION 10
24
FUNCTION WITH A RETURN VALUE
FUNCTION CALL:
Is performed to call a function
that will do intended tasks
• This is placed inside the
main function OR even
inside other functions
• Since the function that you’ve
called will return a value, you
would need to store the
value received into a
variable
25
FUNCTION WITH A RETURN VALUE
FUNCTION CALL:
To call a function, you need these 2 things:
1. Name of the Function
2. List of Parameters:
• Is used to indicate what value the parameters will pass to the
function (The order of the parameters is important)
Let say you want to calculate the average of total marks of students:
The main program will call the function
CalcAverage() and the value inside the
variable totalMarks and numStudent
will be passed to the function
Function Call:
You can call the function to execute the
task that it provide
Function Definition:
This is where you will define what the
function will do
Let say you want to calculate the average of total marks of students:
Declare the function CalcAverage()that will
receive 1 floating-point value and 1 integer value
Let say you want to calculate the average of total marks of students:
The main program will call the function
CalcAverage() and the value inside the
variable totalMarks and numStudent
will be passed to the function
TYPES OF
VARIABLE LOCAL AND
GLOBAL
VARIABLE
56
TYPES OF VARIABLE
• There are 2 types of variables that you can use:
1. LOCAL VARIABLE 2. GLOBAL VARIABLE
✓Is a variable that is ✓Is a variable that is declared outside
declared inside a of all functions
function ✓This type of variable can be used by
✓This type of variable all functions inside the program
can only be used by ✓However, this type of variable is not
the function that recommended to be used: Because
declared it the program readability may be
compromised and the program
maintenance can be tedious
57
PRACTICE 5
a. For each program, determine whether the variable number is a
local variable or a global variable. b. For each
program, what
would be the
output displayed
on the screen if
the user input is
4 (show your
tracing).
58 PART B QUESTION 4
MAR 2016 EXAMPLE 4
59
•
TYPES OF PARAMETER
Parameter is a container that will hold a value that will be passed
and received by the function. 2 types of parameter:
1. FORMAL PARAMETERS 2. ACTUAL PARAMETERS
✓Are parameters that are defined ✓Are parameters that are placed at
at Function Heading Function Call
✓They indicate the values that ✓They indicate the values that will be
will be received by the function passed to the function
✓*Data type for the formal ✓Should not state the data type for
parameter should be stated the actual parameters
61 PART A QUESTION 9
MAR 2016 PRACTICE 6
62
HOW VALUES
ARE PASSED
BETWEEN
FUNCTIONS?
63
HOW VALUES ARE PASSED
BETWEEN FUNCTIONS?
Values can be passed between functions through:
1. Parameter Passing by Value
Values are passed between functions through parameters
2. Return Value
A value can be passed by using a return value inside the
function
3. Parameter Passing By Reference
The location of the value (variable) are passed between
functions through parameters
64
EXAMPLE 5
What is the output of the following program (show your
tracing)?
65
FUNCTION WITH PASS BY
REFERENCE
When should Function with pass by reference be used?
1.Whenyou want to change the value of the “Actual Parameter”
After the function is called, the
variable sum will be updated
2. When you want to return more than one value back to the main
program
66
PART A PRACTICE 7
OCT 2016 QUESTION 9
67
OCT 2016
PART B
QUESTION 5
EXERCISE 4
68
EXAMPLE 6
Function Call: The function called will allow the
variable sum and avg to be updated
TOPIC 5
• FUNCTIONS
✓ Predefined functions
✓ User-defined functions
✓ Function with
parameter (value and
reference)
✓ Global and local
variables
COMPLETE
75
REFERENCES