0% found this document useful (0 votes)
50 views75 pages

Topic 5 Function

The document discusses functions in CSC128. It covers predefined functions, user-defined functions, functions with return values, functions without return values, and functions that pass parameters by reference. The key learning outcomes are functions and their uses, data types for functions, and how to pass parameters to functions.

Uploaded by

Nik Daniel Haziq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views75 pages

Topic 5 Function

The document discusses functions in CSC128. It covers predefined functions, user-defined functions, functions with return values, functions without return values, and functions that pass parameters by reference. The key learning outcomes are functions and their uses, data types for functions, and how to pass parameters to functions.

Uploaded by

Nik Daniel Haziq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

1

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

• When you are solving a


complex problem, your main
program can become long
and complicated WHY DO YOU
NEED
• FUNCTIONS can be used to
simplify your “main” program FUNCTIONS?
5

• By breaking up your long


and complicated main
program into sub-programs
(known as FUNCTIONS)
HOW CAN YOU
SIMPLIFY YOUR
• These functions can be main()
called using function call
statements when needed PROGRAM?
6
MAIN PROGRAM WITHOUT FUNCTIONS
What are the
objectives of
this program?
7
MAIN PROGRAM WITH FUNCTIONS
8
INTRODUCTION TO FUNCTIONS
• Since each function will do a specific task, it is easier for
you to trace errors and debug.

• A function can be called numerous times, anytime you need


it. Therefore, it can prevent redundancy and saves time

There are 2 CATEGORIES of functions:


Functions

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

FUNCTION NAME DESCRIPTION

setw(x) Set field width to x


left Places the characters to the left
instead of the right
setprecision(x) Set the floating point precision to x
fixed Display floating point values in
decimal notation
EXAMPLE
<iomanip> OUTPUT
15

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[])

✓ Function without a return value


▪ (void)

✓ Function with pass by reference


▪ (int&, float&, double&, char&, char*&)
17

TYPES OF USER-DEFINED FUNCTIONS FUNCTION


USER – WITH A
DEFINED RETURN
FUNCTIONS VALUE
(int, float,
double, char,
char[])
18
FUNCTION WITH A RETURN VALUE
• This type of function will return ONE value after all the
statements inside it has been executed.
• For example, inside the function definition:

Will return the value inside the variable “number” to


the main program if it is called
19
FUNCTION WITH A RETURN VALUE
• To use this function, you must do the following:
1. FUNCTION DECLARATION (a.k.a. Function Prototype):
This is where you will declare the function that you want to
use
2. FUNCTION CALL:
This is where you will call the function that you want to
execute
3. FUNCTION DEFINITION:
This is where you will define what your function will do
You will need to include a return VALUE, so that it can be passed back to the main program that calls it
20
FUNCTION WITH A RETURN VALUE
FUNCTION PROTOTYPE:
✓ All functions must be declared before they can be used in a
program
✓ Function declarations/prototype are placed just before the
main program
21
FUNCTION WITH A RETURN VALUE
FUNCTION PROTOTYPE:
To declare a function, you need these 3 things:
1. The Return Type:
• Is used to indicate the type of value that it will return
• The type of value can be of any data type: int, double,
float, char, char[]
2. Name of the Function
3. List of Parameters Type:
•Parameter: Is a container that will hold a value that will be passed to
the function
•Is used to indicate what type of value the parameters will receive
and passed to the function (The order of the parameters is
important)
22
FUNCTION WITH A RETURN VALUE
FUNCTION PROTOTYPE:
Format for function declarations:

Let say you want to calculate the total of 2 numbers:


Declare the function CalcTotal() that will receive 2
integer values and will return an integer value

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)

• You should follow the following format for function call:


26
FUNCTION WITH A RETURN VALUE
FUNCTION CALL:
Let say you want to calculate the total of 2 numbers:
The main program will call the function CalcTotal() and the
value inside the variable num1 and num2 will be passed to
the function

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

Let say you want to calculate the wage of an employee:


The main program will call the function CalcWage()
and the value inside the variable hoursWorked
and hourlyRate will be passed to the function
27
FUNCTION WITH A RETURN VALUE
FUNCTION DEFINITION:
• Is where you will define what your
function will do when it is called by
the main program
• This is placed outside of the main
program
• Your function should include a
RETURN VALUE:
• After all the necessary statements
have been executed to achieve a
value that the program wants, that
value can then be returned to the
main program
28
FUNCTION
FUNCTION DEFINITION:
WITH A RETURN VALUE
To define a function, you need these 4 things:
1. The Return Type:
• Is used to indicate the type of value that it will return
• The type of value can be of any data type: int, double, float,
char, char[]
2. Name of the Function
3. List of Parameters along with its Type:
• Is used to indicate what value the parameters received from the main program (The
order of the parameters is important)
• The type of value must also be included, which can be of any data type: int,
double, float, char, char[]
4. Function Body:
• Are the statements inside the function that needs to be executed to perform certain
tasks, you need to include a RETURN VALUE at the end
29
FUNCTION WITH A RETURN VALUE
FUNCTION DEFINITION:
Format for function definition:

Let say you want to calculate the total of 2 numbers:


Define function CalcTotal() that
receives 2 integer values from main
program. These values will then be
stored inside variable no1 and
no2 respectively. This function will
return the value inside the variable
total back to the main program.
30
FUNCTION WITH A RETURN VALUE
FUNCTION DEFINITION:
Let say you want to the average of total marks of students:
Define function CalcAverage() that
receives 1 floating-point and 1 integer
value from main program.

These values will then be stored inside


variable total and number respectively.

This function will return the value inside


the variable average back to the main
program.
31
FUNCTION WITH A RETURN VALUE
FUNCTION DEFINITION:
Let say you want to Let say you want to calculate the wage of an
employee: Define function CalcWage() that receives
1 integer and 1 floating-point value from
main program.

These values will then be stored inside


variable hours and rate respectively.

This function will return the value inside


the variable wage back to the main
program.
32
PRACTICE 2

What would be the output displayed on the


screen for the program?
33 PRACTICE 3
JAN 2018
PART A
QUESTION 9
34 PRACTICE 4
JAN 2018
PART A
QUESTION 10
35
EXAMPLE 1
1. Write a program that will add up 2 numbers
received from the user.

2. Write the same program, but use:


Function CalcTotal() that will receive 2 numbers
through its parameters. It then calculates the
total of 2 numbers and returns the total to the
user.
36
EXAMPLE 1
Function Declaration (Function Prototype):
You need to declare the function before it can
be used

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

After all the statements have been


executed, it will return a value to the main
program that calls it
37

JAN 2018 EXAMPLE 2


PART B
QUESTION 5
38

JAN 2018 EXERCISE 1


PART B
QUESTION 5
39

TYPES OF USER-DEFINED FUNCTIONS FUNCTION


USER – WITHOUT
DEFINED A RETURN
FUNCTIONS VALUE
void
40
FUNCTION WITHOUT RETURN VALUE
FUNCTION DEFINITION:
This type of function will NOT return any value after all the statements
inside it have been executed
For example, inside the function definition:

It is mainly used when you want the function to display your


calculated value instead of returning it back to the main program
41
FUNCTION WITHOUT RETURN VALUE
• To use this function, you must do the following:
1. FUNCTION DECLARATION (a.k.a. Function Prototype):
This is where you will declare the function that you want to
use
2. FUNCTION CALL:
This is where you will call the function that you want to
execute
3. FUNCTION DEFINITION:
This is where you will define what your function will do
You do not need to include a return value, instead DISPLAY THE VALUES that you’ve calculated.
42
FUNCTION WITHOUT RETURN VALUE
FUNCTION PROTOTYPE:
To declare a function, you need these 3 things:
1. The Return Type:
• Use void as the return type:
• This means that the function will not return any value back to the
main program
2. Name of the Function
3. List of Parameters Type:
•Parameter: Is a container that will hold a value that will be passed to
the function
•Is used to indicate what type of value the parameters will receive
and passed to the function (The order of the parameters is
important)
43
FUNCTION WITHOUT RETURN VALUE
FUNCTION PROTOTYPE:
Format for function declarations:

Let say you want to calculate the total of 2 numbers:


Declare the function CalcTotal() that will receive 2
integer values

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 wage of an employee:


Declare the function CalcWage()that will receive 1
integer value and 1 floating-point value
44
FUNCTION WITHOUT RETURN VALUE
FUNCTION CALL:
• Since the function that you’ve
called will NOT RETURN a
value, you don’t have to store
the value received into a
variable.
45
FUNCTION WITHOUT RETURN VALUE
FUNCTION CALL:
Let say you want to calculate the total of 2 numbers:
The main program will call the function
CalcTotal() and the value inside the variable
num1 and num2 will be passed to the function

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

Let say you want to calculate the wage of an employee:


The main program will call the function CalcWage()
and the value inside the variable hoursWorked and
hourlyRate will be passed to the function
46
FUNCTION WITHOUT RETURN VALUE
FUNCTION DEFINITION:
• Your function should NOT include a
return value.
• After all the necessary statements
have been executed to achieve a
value that the program wants, that
value can then be DISPLAYED to
the main program.
47
FUNCTION
FUNCTION DEFINITION:
WITHOUT RETURN VALUE
To define a function, you need these 4 things:
1. The Return Type:
• Use void as the return type: This means that the function will not return
any value back to the main program
2. Name of the Function
3. List of Parameters along with its Type:
• Is used to indicate what value the parameters received from the main program (The
order of the parameters is important)
• The type of value must also be included, which can be of any data type: int,
double, float, char, char[]
4. Function Body:
• You should NOT include a return value at the end, instead DISPLAY THE VALUE that
needed to be displayed.
48
FUNCTION WITHOUT RETURN VALUE
FUNCTION DEFINITION:
Format for function definition:

Let say you want to calculate the total of 2 numbers:


Define function CalcTotal() that
receives 2 integer values from main
program. These values will then be
stored inside variable no1 and
no2 respectively. This function will
CALCULATE and DISPLAY the
output.
49
FUNCTION WITHOUT RETURN VALUE
FUNCTION DEFINITION:
Let say you want to the average of total marks of students:
Define function CalcAverage() that
receives 1 floating-point and 1 integer
value from main program.

These values will then be stored inside


variable total and number respectively.

This function will calculate the average and


DISPLAY the output, without returning any
value to the main program.
50
FUNCTION WITHOUT RETURN VALUE
FUNCTION DEFINITION:
Let say you want to Let say you want to calculate the wage of an
employee: Define function CalcWage() that
receives 1 integer and 1 floating-
point value from main program.

These values will then be stored


inside variable hours and rate
respectively.

This function will calculate the wage


and DISPLAY the wage. No value
will be returned to the main
program.
51
PRACTICE 5
What would be the output displayed
on the screen for the program if the
input is 3.2?
52 PART B QUESTION 4
MAR 2016 EXAMPLE 3
53 PART B QUESTION 5
MAR 2017 EXERCISE 2
54

MAR 2017 EXERCISE 3


PART B QUESTION 5
55

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 FORMAL AND


PARAMETER ACTUAL
PARAMETERS
60


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

After all the process inside the


function have been executed,
the function will PASS the
updated parameter total
back to the main program

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

OCT 2016 EXERCISE 5


PART B
QUESTION 5
69
EXAMPLE 6
You are asked to write a program that calculates the
total and average of two integers. Your program should
contain the following functions and main program:
• Function calcTotalAndAvg() that receives two
numbers as parameters. It should also receive the
total and average as reference parameters. It will then
calculate the total and average of the two numbers.
• The main program will prompt the user to input two
integers and call the function as needed. It should
display the sum and average of the two numbers.
70
Function Declaration (Function Prototype):
You need to declare the functions before they can be
used

EXAMPLE 6
Function Call: The function called will allow the
variable sum and avg to be updated

Function Definition: This function will update the


parameter total and average and return them back to
the main program
PART C
71
JAN 2018 QUESTION 2 EXAMPLE 7
PART C
72
JAN 2018 QUESTION 2 EXERCISE 6
PART C
73
OCT 2016 QUESTION 2 EXERCISE 7
74

TOPIC 5
• FUNCTIONS
✓ Predefined functions
✓ User-defined functions
✓ Function with
parameter (value and
reference)
✓ Global and local
variables
COMPLETE
75
REFERENCES

• Malik, D.S (2010). “C++ Programming: From Problem


Analysis to Program Design”. Cengage Learning
• Najwa Abd Ghafar. (2018). CSC128: Chap 5 –
Function [PowerPoint slides in PDF].

You might also like