1. LAB # 11 HANDOUT
Objectives:
Overloaded Functions:
Overloaded functions help us to have multiple functions with the same name but with different
arguments/parameter list. This allows for better readability of the code: multiple functions having self-
evident names can be defined which take input arguments of different data types. It can also help if we
mistakenly pass wrong/different arguments to the function.
While defining overloaded functions, the thing that needs to be different is the data type of the parameter
list. The return data type may or may not be changed accordingly.
Practice Questions:
1. Write three averaging functions (for 4 numbers) with the same name but with int, float and double data
type of input argument respectively.
2. Write 3 overloaded calculator functions (for int, float and double) that take character as one of the
parameters and depending upon this parameter performs the basic 4 mathematical functions (+, -, *, /).
3. Write two overloaded functions that convert float into int and vice versa. (Hint: If the user enters 5.6, the
function should return 5 and if the user enters 4, the function should return 4.0.)
Pass By Value vs Pass By Reference:
Up until now, we have been passing the arguments to a function by value. What that means is that a copy is
created of the variables passed to the function and all the manipulation is done on that copy. This means that
there is no effect on the original value of the variable.
There is another way of passing the value of a variable to a function. That method is called pass by reference.
The syntax for doing so is as follows:
void my_function (int &my_variable);
The ampersand (&) makes it possible to pass the function by reference. It sends the address of the variable
to the function. As the changes are happening to the original value, therefore there is no need for a return value
hence the use of void.
In the main, there aren’t any changes when calling a function that takes a reference to a variable as an input
argument.
Practice Questions:
Repeat the tasks listed above but this time, pass all the values by reference.
2. Scope Rules and Variables:
In C++, a scope is defined by { }. Just like nested loops and nested if/else, scopes can exist inside other scopes.
The variables defined in one scope may or may not be visible/accessible inside other scopes. Scope allow us
to have same named variables even in different scopes.
{ //Outer Scope
{ //Inner Scope}
}
{ int x = 0;
{ int y = 1;
cout<<x<<endl<<y;}
}
As soon as we exit a scope, the values of the variables in that scope are destroyed. What this means is that
whenever we return from a function call, the values of the variables declared and initialized in that function
(or temporary variables) aren’t saved anymore.
Global and Static Variables:
Global variables are such variables which are visible to all the scopes in a program. These variables can be
used/updated from anywhere and can be used when there needs to be a single value visible everywhere. Any
variable that is defined and initialized above main ( ) becomes a global variable.
Variables can be made static by using the keyword static. This ensures that the value of the variable is kept
safe even after we have exited the scope. In other words, when we want to have static value between function
calls, we can make a variable static. So, no matter when that particular function is called, the value of the
particular static variable will remain the same.
Practice Questions:
1. Write a function that “remembers” how many times it has been called.
2. Declare two variables both name X. One of these variables should be accessible from anywhere. While
the other one should only be accessible from main.
3. In the above task, display the value of X declared above main in main. (Hint: Use the Unary operator :: ).
4. Re-write an averaging function for 4 numbers that only uses global variables.
5. Re-write the calculator function using global variables.