Lab 08 Function 1 PDF
Lab 08 Function 1 PDF
Objective(s) : Upon completion of this lab session, learners will be able to:
Function
A function is a group of statements that together perform a task. Every C++ program has at least one
function, which is main(), and all the most trivial programs can define additional functions. The
execution of the program always starts with main, but it can call other functions to do some part of
the job.
Like any other object in C++, functions must be declerad and defined. The function declaration is
done first with a prototype decleration. A function can be called with main() function or other user
defined fuction(s). the function definition, which is traditionally coded after the function that makes
the call, contains the code required.
Exercise 1
Write a C++ Program that contains one user defined function month().
In main() function:
o Read an integer input in between (1 to 12) and store it month_of_year.
o Call month(month_of_year)
In month() function:
o Print the corresponding month of year in month().
o Example: Value of parameter is 4… Print “April”.
Exercise 2
Write a C++ Program that contains one user defined function cal_grades().
In main() function:
o Prompt user to enter obtained(0 - 100) marks for one subject.
o Call cal_grades(marks_subject).
o Print the corresponding Grade with respect to Marks.
In user defined function:
o Perform conditioning with else if statement return char value.
o Function must return value.
Exercise 3
Write a C++ Program that contains four user defined function(s) addition(), subtraction(), division(),
multiplication(). Develop a calculator as follows
In main() function:
o A menu with choices addition,subtraction,division and multiplication must be displayed.
o Get two numbers and a choice from user
o Call the respective functions with user given number as parameter using switch statement
o Print the result from addition(), subtraction, division(), multiplication().
In user defined functions:
o Plus and Minus function get two integer values and return integer.
o Multiply and Divide functions get two integer values and return float.