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

CSC425 Introduction to Computer Programming

Lab 7: Functions (Part I)

Functions

 A function is a block of code that design to perform a specific task


 Often called modules
 Every C++ program contains at least one function (main)
 Some function are built-in functions and defined in language libraries (standard
predefined function.
 Others, called user-defined functions, are written by programmer; defined in a program
o Value-returning functions
o Void function

Standard predefined function


 C++ provides the predefined:

Mathematical Math functions are contained in the header file <cmath>


functions Example:
 pow (x,y)
 sqrt (x)
 abs (x)
 floor (x): returns the largest whole number that is not greater x
 floor (45.67)= 45.00
 ceil (x): returns the smallest whole that is not less than x
 ceil(56.34) = 57.0
String String functions are contained header file such as:
functions  <cctype> for:-
 tolower : Convert uppercase letter to lowercase
 toupper : Convert lowercase letter to uppercase
 getline :to read strings with blanks

 <cstring> for:-
 strlen : computes the length of the string, but not including
null characters
 strcpy : to copy one string to another
 strcmp : takes two strings as arguments and compare these
two strings
Manipulator Manipulator functions are contained in the header file <iomanip>
functions  setw():sets the width of the filed assigned for the output.
 setprecision():sets the total number of digits to be displayed,
when floating point numbers are printed.
 fixed: sets floating-point numbers in a fixed decimal format
1. User-defined functions (Value Returning Functions)

 All value returning functions perform a task and then precisely return one value
 Example code for value returning functions
 Syntax:

• Function prototype (declaration)


 Tells the compiler about a function's name, return type, and parameters
• Function definition
 Provides the actual body of the function.
• Function call
 A statement that executes a function. 
• Formal parameter
 Is a reference parameter
 These are the variables written or declared in function definition/prototype, and
receive their values when a call to that function is made.
• Actual parameter
 The values/variables passed while calling a function
2. Void Functions

 Void functions are created and used just like value-returning functions except they do
not return a value after the function executes. 
 Use the keyword "void." 
 Example:
 Syntax:
Task 1. Trace the output of following code. (Screenshot the output)

a)

b)

c)
d)

e)

f)
g)

h)
i)

j)

k)
l)

Task 2. Given the following C++ code


a) Identify three parameters mark as * for function double calculation ( * , * , *)

b) Determine the output from the program based on the following input.

a. 10 15 20
b. 4 5 3
Task 3.

As a stockiest of a supplement product GELHALAL, Ahmad wants to know the profit of


selling this product as a wholesale. The cost of the supplement product is RM30 per bottle.
The following table shows the selling price of stockiest to dealer.

No of Bottle Price per Bottle (RM)

10 to 19 50.99

20 to 29 45.59

30 and above 40.99

Dealers are NOT allowed to buy less than 10 bottle.

a. Write the definition of function calcProfit(). This function receives the number of
bottle sold to the dealer through its parameter. This function then calculates and
returns the profit using the following formula:

profit = number of bottle x (price per bottle – cost per bottle)

b. Write a main program to do the following:


 Get the input of dealer’s name and the number of bottle
 If the number of bottle is less than 10,
 Display the message
“Dealer is not allowed to buy less than 10 bottles!”

 Otherwise:
1. Calculate the profit using the function calcProfit()
2. Display the profit
Task 4.

The government of Malaysia assigned you to write a program to advise all staff to determine
what retirement age to choose based on the criteria given by Body Mass Index (BMI) and
Mortality Predictor (MP) .

BMI is calculated by the following formula:

Weight (kg)
𝐵𝑀𝐼 =
𝐻𝑒𝑖𝑔ℎ𝑡 ሺ𝑚ሻ𝑥 𝐻𝑒𝑖𝑔ℎ𝑡 (𝑚)

Meanwhile, MP code is determined by the following condition:

BMI Age Gender MP code

M A
< 40
F B
> 30
M B
> 40
F C

M A
< 40
F A
<= 30
M B
> 40
F B

Only those who achieved ‘A’ or ‘B’ rating in their MP will be advised to choose to retire at
60, while those who achieve ‘C’ must choose to retire at 55.

a. Write the definition of function calcBMI () that receives weight and height. This
function calculates and returns the BMI.
b. Write the definition of function calcMP() that receives BMI, age and gender. This
function determines and returns the MP code
c. Write a main program to do the following:
a. Get the input of weight, height, gender and age.
b. Calculate the BMI using the function calcBMI()
c. Determine the MP code using the function calcMP()
d. Display the message “Retire at 60” or “Retire at 55” based on the MP code.

You might also like