FoP-Lab No 10
FoP-Lab No 10
LAB # 10
Referenced Arguments in Functions, Function Overloading and
Storage Categories
Lab Objectives:
To understand and learn how to use referenced parameters, function overloading and
storage categories.
Lab Description:
RETURNING MULTIPLE VALUES:
In a typical function invocation, the called function receives values from its calling function, stores
and manipulates the passed values, and directly returns at most one value. When data is passed in
this manner, it’s referred to as a pass by value.
At times, however, you need to modify this approach by giving a called function direct access to
its calling function’s variables. This approach allows one function—the called function—to use
and change the value of variables that have been defined in the calling function. Doing so requires
passing the variable’s address to the called function. After the called function has the variable’s
address, it “knows where the variable lives,” so to speak, and can access and change the value
stored there.
Passing addresses is referred to as a function pass by reference because the called function can
reference, or access, the variable whose address has been passed.
calls the function named newval() and passes two arguments to it. Whether a value or an address
is actually passed depends on the parameter types declared for newval(). Now take a look at writing
the newval() function and prototype so that it receives the addresses rather than the values of the
variables firstnum and secnum, which are assumed to be doubleprecision variables. One of the
first requirements in writing newval() is to declare two reference parameters for accepting passed
addresses.
datatype& referenceName
For example, the reference declaration double& num1; declares that num1 is a reference parameter
used to store the address of a double. Similarly, int& secnum; declares that secnum is a reference
to an integer, and char& key; declares that key is a reference to a character.
The ampersand, &, in C++ means “the address of.” Additionally, when & is used in a declaration,
it refers to “the address of” the preceding data type. Using this information, declarations such as
double& num1 and int& secnum are sometimes more clearly understood if they’re read backward.
Reading the declaration double& num1 in this manner yields the information “num1 is the address
of a double-precision value.” Because you need to accept two addresses in the parameter list for
newval(), the declarations double& num1 and double& num2 can be used. Including these
declarations in the parameter list for newval(), and assuming the function returns no value (void),
the function header for newval() becomes the following:
FUNCTION OVERLOADING:
Function overloading is a feature in C++ where two or more functions can have the same name but
different parameters. These functions having the same name but different arguments are known as
overloaded functions. For example:
// same name different arguments
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
Here, all 4 functions are overloaded functions. Notice that the return types of all these 4 functions
are not the same. Overloaded functions may or may not have different return types but they must
have different arguments. For example,
// Error code
int test(int a) { }
double test(int b){ }
Here, both functions have the same name, the same type, and the same number of arguments.
Hence, the compiler will throw an error.
EXAMPLE CODE:
The following example code shows the use of referenced parameters and function overloading.
// Lab 10.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
int main()
{
int a = 5;
double b = 5.4;
system("pause");
return 0;
}
OUTPUT:
Integer number: 5
Double number: 5.4
Added value of a = 10
Added value of b = 10.8
LAB TASKS
Task # 1:
a. For the following section of code, determine the data type and scope of all declared variables
on a separate sheet of paper, using the column headings shown in the following chart. (The
entries for the first variable have been filled in.)
b. Draw a box around the appropriate section of the preceding code to enclose the scope of each
variable or constant.
c. Determine the data type of the arguments that the func1() and func2() functions expect and the
data type of the value these functions return.
Task # 2:
Three integer arguments are to be used in a call to a function named time(). Write a suitable
function header for time(), assuming that time() accepts these variables as the reference parameters
sec, min, and hours and returns no value to its calling function.
Task # 3:
a. Write a function named change() that has an integer parameter and six integer reference
parameters named hundreds, fifties, twenties, tens, fives, and ones. The function is to consider
the passed integer value as a dollar amount and convert the value into the fewest number of
equivalent bills. Using the reference parameters, the function should alter the arguments in the
calling function.
b. Include the function written in Task 3a in a working program. Make sure your function is
called from main() and returns a value to main() correctly. Have main() use a cout statement
to display the returned value. Test the function by passing various data to it and verify the
returned value.
Task # 4:
Write a function named yearCalc() that has an integer parameter representing the total number of
days from the date 1/1/2000 and reference parameters named year, month, and day. The function
is to calculate the current year, month, and day given the number of days passed to it. Using the
reference parameters, the function should alter the arguments in the calling function. For this
problem, assume each year has 365 days, and each month has 30 days.
Task # 5:
Write a function named liquid() that has an integer number parameter and reference parameters
named gallons, quarts, pints, and cups. The passed integer represents the total number of cups, and
the function is to determine the numbers of gallons, quarts, pints, and cups in the passed value.
Using the reference parameters, the function should alter the arguments in the calling function.
Use these relationships: 2 cups = 1 pint, 4 cups = 1 quart, and 16 cups = 1 gallon.
Task # 6:
The following program uses the same argument and parameter names in both the calling and called
functions. Determine whether doing so causes any problem for the compiler.
Task # 7:
Using the concept of function overloading, write a C++ program that has 2 functions named
absolute() for both int and float types. The functions should takes some value and return absolute
value to the calling function.