0% found this document useful (0 votes)
58 views7 pages

FoP-Lab No 10

The document describes function overloading and passing arguments by reference in C++. It includes examples of declaring reference parameters, calling functions that accept references, and overloading functions with the same name but different parameters. The lab tasks practice these concepts by writing functions that accept references and return modified arguments, overload functions, and analyze code examples.

Uploaded by

umer naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views7 pages

FoP-Lab No 10

The document describes function overloading and passing arguments by reference in C++. It includes examples of declaring reference parameters, calling functions that accept references, and overloading functions with the same name but different parameters. The lab tasks practice these concepts by writing functions that accept references and return modified arguments, overload functions, and analyze code examples.

Uploaded by

umer naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

College of Electrical & Mechanical Engineering, NUST

Department of Electrical Engineering

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.

PASSING AND USING REFERENCE PARAMETERS


When exchanging data between two functions, you must be concerned with both the sending and
receiving sides. From the sending side, calling a function and passing an address as an argument
that’s accepted as a reference parameter on the receiving side is the same as calling a function and
passing a value; the called function is summoned into action by giving its name and a list of
arguments.

For example, the statement


newval (firstnum, secnum);

Fundamentals of Programming DE-42


College of Electrical & Mechanical Engineering, NUST
Department of Electrical Engineering

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.

In C++, a reference parameter is declared with this syntax:

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:

void newval (double& num1, double& num2)

For this function header, the following is a suitable function prototype:

void newval (double& ,double&);

Fundamentals of Programming DE-42


College of Electrical & Mechanical Engineering, NUST
Department of Electrical Engineering

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;

// function with 2 referenced parameters


void addition(int& var1, double& var2) {
var1 = var1 + var1;
var2 = var2 + var2;
}

// function with double type single parameter


void display(double var) {
cout << "Double number: " << var << endl;
}

// function with int type single parameter


void display(int var) {
cout << "Integer number: " << var << endl;
}

int main()

Fundamentals of Programming DE-42


College of Electrical & Mechanical Engineering, NUST
Department of Electrical Engineering

{
int a = 5;
double b = 5.4;

// call function with int type parameter


display(a);

// call function with double type parameter


display(b);

// call function referenced parameters


addition(a, b);
cout << "Added value of a = " << a << endl;
cout << "Added value of b = " << b << endl << endl;

system("pause");
return 0;
}

OUTPUT:

Integer number: 5
Double number: 5.4
Added value of a = 10
Added value of b = 10.8

Fundamentals of Programming DE-42


College of Electrical & Mechanical Engineering, NUST
Department of Electrical Engineering

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.

Fundamentals of Programming DE-42


College of Electrical & Mechanical Engineering, NUST
Department of Electrical Engineering

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.

Fundamentals of Programming DE-42


College of Electrical & Mechanical Engineering, NUST
Department of Electrical Engineering

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.

Fundamentals of Programming DE-42

You might also like