Professional Documents
Culture Documents
CSC404 Chapter2 Part2
CSC404 Chapter2 Part2
Functions
Sub-Topics
Function Without a Return Value
FUNCTION WITHOUT A RETURN VALUE
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:
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
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: