Lesson Plan - Functions and scope of variables
Lesson Plan - Functions and scope of variables
Note : No two(or more) variables can be declared with the same name within the same scope. However, it is
possible for two(or more) variables to have the same name if they are (all) declared in different scopes.
Example 1 :
#include <iostream>
void func(){
cout<<a;
int main(){
// cout<<a; // line 1
Here, ‘a’ is a local variable for function func but ‘a’ has no identity outside the function func.
Uncommenting line 1 would produce an error at the time of compilation, because we are trying to access
variable ‘a’ whose scope is limited to the function func().
Example 2 :
#include <iostream>
//global variable
int p=20;
void func(){
cout<<p;
int main(){
cout<<p; // line 1
Here, p is a global variable and therefore, is accessible from anywhere in the program, Hence, the code will
compile without any error.
Example 3:
#include <iostream>
int main()
int p = 50;
double p = 60.70;
cout<<(p)<<endl;
Here, as you might have already observed, there are two variables with the same name p that are declared in
the same scope of main () function.
#include<iostream>
int main()
int p = 5;
int p = 7, q = 9;
Here, p has been declared both as a local and a global variable. Inside the block, the value of p is 7 (as per
local declaration); however, outside the block, the value of p is fetched from the global declaration and the
value of p is 5.
As for q, inside the block, the value of q is 9 (as per local declaration). However, outside the block, q is an
unknown entity, hence the print statement for q, outside the block will simply throw an error.
Common Doubt :
What if there exists a local variable with the same name as that of the global variable inside a function?
If we declare a global variable and a local variable with the same name, then precedence will be given to the
local variable by the compiler i.e. in a region where both local and global variables of the same name exist, all
the operation done on the variable will be done on the local variable only.
If we deliberately want to access the global variable, we may do so using a scope resolution operator. Look at
the example below to have a clear understanding of using a scope resolution operator.
#include <iostream>
//global variable
int p=23;
int main()
//local variable
int p=32;
p++;
return 0;
Before proceeding to the next topic of parameter passing (in functions), let us first learn a bit about them.
A parameter is a named variable passed into a function. Yes, we can pass variables in functions too ! This will
help us to use a function to do mathematical calculations, logical operations etc rather than just printing
statements which we were doing till now.
Actual parameters: parameters that are passed during the function call in another function.
Confused?! We have got you covered. Let us look at the example below :
#include <iostream>
return p-q;
int main ()
int x=89;
int y=9;
Output: 80
Here, you may also use the same variable name p and q in place of x and y. Note that, in that case, p and q
inside the main function would be different from p and q inside the diff function because both would have
different local scopes limited to the respective functions only.
To perform any operation with the help of functions through parameter passing, we must also be aware that
this can be done in two different ways which brings us to the most interesting topic of functions !
Pass by Value: here, the function parameter values (i.e. value from actual parameter) are copied to another
variable (formal parameter).
Pass by Reference: here, actual copy of variable reference is passed to the function. This is also known as
‘call by reference’.
Example:
1. Pass by value
void changeValue(int z) {
int main() {
int a = 40;
changeValue(a);
return ans;
int main()
int a, b, ans;
cin>>a;
cin>>b;
cout<<("The sum of two numbers a and b is: ")<< ans << endl;
We are now going to discuss parameter passing by reference. It is being discussed separately here because it
is a little different from whatever we have learnt so far.
To access the address/reference of any variable, we use the "&" operator. Look at the example below to
understand the use of ‘&’ operator and its use in passing by reference.
#include <iostream>
int c = a;
a = b;
b = c;
int main()
// Call the function swap, which will change the values of num1
// and num2
swap(num1, num2);
return 0;
Output:
Before swap:
20 32
After swap:
32 20
After the swapping operation in the ‘swap’ function, the values are changed in the reference/address of the
variables which are printed in the main function.
If it appears confusing to you, you are not the only one! Don't worry, we will cover the reference concept more in
the forthcoming lecture on pointers.
The next topic is extremely interesting and equally important. Let us understand it with a little attention.
If a function with default arguments is called without passing arguments, then the default parameters are
used. However, if arguments are passed while calling the function, the default arguments are ignored.
#include <iostream>
return (a + b + c + d);
// Driver Code
int main()
// Statement 1
// Statement 2
// Statement 3
return 0;
Output:
60
70
110
When this function is called, it reaches out to the definition of the ‘add’. There it initializes a to 30, b to 20 and
the c to 0 , d to 10 by default as no value is passed. After addition of all these values gives 60 as output.
Sum(10, 20, 30)
When this function is called, a is assigned as 10, b is assigned as 20, the third parameter c is initialized to 30
instead of zero. The last value d remains 10. The sum of a,b,c,d is 70 which is returned as output.
Sum(5, 20, 35, 50)
In this function call, there are four parameter values passed into the function with a as 5, b as 20, c is 35, and
d as 50. All the values are then summed up to give 110 as the output.
We hope that the concept of default argument is pretty clear with all these combination of scenarios discussed
above.
Note: Once you have used the default value for an argument in the function definition, all subsequent
arguments must have a default value as well. This can also be stated that the default arguments are assigned
from right to left.
For example, the following function definition is invalid as the subsequent argument of the default variable d is
not default.
// Invalid because c has default value, but d after it doesn't have a default value
n1--;
n2 = n2 - 2;
int main()
int p = 26;
int q = 13;
decrease(p,q);
Ans:
25: 11
26: 13
In this program snippet, changes in the values of n1 and n2 are not reflected to p and q because n1 and n2 are
formal parameters and are local to function decrease so any changes in their values here won’t affect
variables p and q inside main function.
void makeTwice(int p )
p = p * 2;
int main()
int p = 24;
makeTwice(p);
cout<<(p);
Ans : 24
Explanation:
In the code snippet, changes in the value of p is not reflected in the main function
Here we are calling the function makeTwice using the concept of ‘pass by value’.
void temp(int p)
int q = p;
q = q -100;
int main()
int p = 890;
temp(p);
cout<<(q)<<endl;
Ans: Yes, it will generate an error, because, in the main function there is no variable having name q,variable q
has scope in temp function block only.
b) whole program
c) header section
That is all ! See you in the next lesson !! Till then, keep learning ! Keep exploring !!