Labreport 551
Labreport 551
Lab#6
Computer Programming
Objectives:
To understand the concept of function.
To understand what the function prototype is and how that is different from the
function definition.
Convert the code processing in the main function to a function called from the main
function.
Create functions with multiple parameters.
The mechanisms for passing information between functions and returning results.
Understand the difference between pass by value and reference.
To know about function overloading.
Description
A function is a collection of statements that performs a specific task. So far you have
experienced functions as you have created a function named “main” in every program
you have written. Functionsare commonly used to break a problem down into small
manageable pieces. This approach is sometimes called divide and conquer because a
large problem is divided into several smaller problemsthat are easily solved.
1. Name: You should give each function a descriptive name. In general, the same
Computer programming Department of mechanical Purwa Tariq
1. Parameter list: The program can send data into a function. The parameter
list is a list ofvariables that hold the values being passed to the function.
2. Body: The body of a function is the set of statements that perform the
function’s operation.They are enclosed in a set of braces.
3. Return type: A function can send a value to the part of the program that
executed it. Thereturn type is the data type of the value that is sent from the
function.
Void Functions
It is not necessary for all functions to return a value, however. Some functions simply
perform one or more statements, which follows terminate. These are called void
functions. An example is shown below.
Calling a Function
A function is executed when it is called. The “main” function is called automatically
when a program starts, but all other functions must be executed by function call
statements. When a function is called,the program branches to that function and executes
the statements in its body.
Function prototype
A function prototype eliminates the need to place a function definition before all calls to
the function.You must place either the function definition or / the function prototype
ahead of all calls to the function. Otherwise, the program will not compile.
By using parameters, you can design your own functions that accept data this way. A
parameter is a special variable that holds a value being passed into a function. The values
can be passed to the functionas “value” or as “reference”.
Pass by Value
void displayValue (int);
// main function
int x = 2;
displayValue(x); //function is called and the parameter x is passed to the function definition
Here the value “x” passed by main function is stored in another “x”. Keep in mind this
“x” is differentfrom the one in the main function. Any change we make to this “x” will
not change the value of “x” present in the “main” function. Suppose we update the “x”
value here as, “x” = 3, then “x” has value 3 in this function only but in the main function
“x” will not get updated, it will still stay as “x” = 2, because both are different. We can
give different name to the “x” in the function definition to avoid confusion. In the same
program we can also write the function definition line as,
Now the variable sent by the main function is “x” whose value is copied in the variable
“num”.
Pass by Reference
void displayValue (int&);
// main function
Computer programming Department of mechanical Purwa Tariq
int x = 2;
displayValue(x); //function is called and the parameter “x” is passed to the function
definition
Notice this “&” with the data type in function definition. Here the value “x” passed by
main function is stored in another “x” but by writing “&” gives an address to the “x” in
the main function. So, if I change this “x” value within the function it will also change
the value of “x” in the main function. Even if we had stored it in another variable, it still
has the address of the main function variable “x”, so any change we make into this
variable will update the variable in the main function also.
Now the variable sent by the main function is “x” whose value is copied in the variable
“num”. If wechange the value of “num” here the value of “x” in the main function will
also change as “num” has the address of “x”.
This “&” should be written in both function definition as well as prototype.
A function can also return a Boolean value instead of integer or double or character. Then
we must use“bool” in the function definition instead of “int”
Function Overloading
Overloaded functions have the same name but different parameter lists. They can be used
to create functions that perform the same task but take different parameter types or
different number of parameters. The compiler will determine which version of function
to call by argument and parameterlists.
Example
In the above example, all the functions being called are different. Although their names
are same but the parameters or their data types are change, that makes them a different
function.
LAB TASKS
1. Write a function courseGrade. This function takes as a parameter an int value
specifying the score for a course and returns the grade, a value of type char, for
the course.
2. Write a main function that call a function named countCall. Every time this
function is called you should display a message “I have been called 1 time” etc.
If the function is called 3 timesthe output should be: (Use pass by Reference)
3. Write a function accepts an integer argument and tests it to be even or odd. The
function returns true if the argument is even or false if the argument is odd. The return
value should be bool. In main take a integer input from user and pass it to the function.
4. The program simulates a simple food ordering system. The user can select a food item from
the menu, specify how many of that item they want to order, and then calculate the total bill.
The program also applies a discount if the total bill exceeds a certain amount.
showMenu():
It uses cout (console output) to print the menu.
Each food item is listed with a corresponding number (1 for Burger, 2 for Pizza, etc.).
This helps the user choose an option by entering the corresponding number.
getPrice(int choice):
The function takes an argument choice (an integer).
Depending on the value of choice, the function returns the price of the selected item.
If the user chooses "1" (Burger), the function returns the price $5.99. If the user chooses "2"
(Pizza), it returns $8.99, and so on.
If the user provides an invalid choice (e.g., a number outside the menu options), the function
returns 0, indicating no valid price.
calculateBill(int choice, int quantity):
The function takes two arguments:
1. choice: The number representing the selected food item (e.g., 1 for Burger, 2 for Pizza).
2. quantity: The number of items the user wants to order.
The function first calls getPrice() to get the price of the food item based on the user's
choice.
Computer programming Department of mechanical Purwa Tariq
Then, it multiplies the price by the quantity to get the total cost for that food item and quantity.
The result is returned as the total cost.
applyDiscount(double totalBill):
The function takes totalBill as an argument (the total amount the user needs to pay before
the discount).
It checks if the total bill is greater than $30.
If the total bill is greater than $30, it applies a 10% discount by reducing the total bill by 10%.
If the bill is not above $30, no discount is applied, and the function simply returns the original
total bill.
For Example :