Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

CHAPTER 2:

Functions

Sub-Topics
Function Without a Return Value
FUNCTION WITHOUT A RETURN VALUE

Function without a Return Value:


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
FUNCTION WITHOUT A RETURN VALUE

Function without 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 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 ha o e calc la ed
3. Function Call:
This is where you will call the function that you want to execute
FUNCTION WITHOUT A 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)
FUNCTION WITHOUT A RETURN VALUE
Function Prototype:
You should follow the following format for function declarations:

For example:
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 and 1 integer value
Let say you want to calculate the wage of an employee:

Declare the function CalcWage() that will


receive 1 integer and 1 floating-point value
FUNCTION WITHOUT A RETURN VALUE
Function Call:
Since the function that ou e called will NOT RETURN a value,
you don t ha e to store the value received into a variable
FUNCTION WITHOUT A RETURN VALUE
Function Call:
For example:
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
FUNCTION WITHOUT A 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
FUNCTION WITHOUT A RETURN VALUE
Function Definition:
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
FUNCTION WITHOUT A RETURN VALUE
Function Definition:
You should follow the following format for function definition:

For example:
Let say you want to calculate the total of 2 numbers:
FUNCTION WITHOUT A RETURN VALUE
Function Definition:
Let say you want to calculate the average of total marks of students:

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


QUICK EXERCISE
What would be the output displayed on the screen for the program below if the
input is 3.2?
QUICK EXERCISE

1. Write a program that calculates the wage of an employee. Your program


will read the em lo ee name, the number of hours worked and the
hourly pay rate. Your program should display a greeting message to the
user and the wage of the employee that has been calculated.

2. Write the same program, but use:


Function dispGreeting() that do not receive any value through its
parameters. It will read the em lo ee name and display a
greeting message to the user as follows:
Hello John Mille . Welcome o hi og am.
Function calcWage() that receives the number of hours worked
and the hourly pay rate. It then calculate and returns the wage of
the employee.

You might also like