0% found this document useful (0 votes)
14 views31 pages

Document 7

This document covers variable scope, including global and local variables, and the differences between pass-by-value and pass-by-reference methods in programming. It provides examples and exercises to illustrate these concepts, emphasizing how variable scope affects function behavior and data manipulation. The document concludes with a summary of the key learnings and references for further reading.

Uploaded by

1khichree1
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)
14 views31 pages

Document 7

This document covers variable scope, including global and local variables, and the differences between pass-by-value and pass-by-reference methods in programming. It provides examples and exercises to illustrate these concepts, emphasizing how variable scope affects function behavior and data manipulation. The document concludes with a summary of the key learnings and references for further reading.

Uploaded by

1khichree1
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/ 31

CSC099 FOUNDATION COMPUTING II

FUNCTION AND PROGRAM


STRUCTURE
PART II
LEARNING OUTCOMES
Upon completion of this chapter, students should be able to:
• Explain variable scope.
• Differentiate between global and local variables.
• Compare pass-by-value and pass-by-reference methods.
• Apply appropriate variable scope and parameter passing
techniques in functions.
VARIABLE SCOPE
• Scope-determines the part of program in which you can use the defined
variable.
• Global scope – any object defined in the global area of the program is
visible from its definition until the end of the program.
- global variables : variables that are declared outside the function,
recognized by any function or program that start after its declaration
• Local scope – variable defined within a block, visible only in the block
where they are declared.
• local variables :variables that are declared in the function body and
can only be used in that particular function.
• do not relate to any variable in other function (can have the same
name as variables in other functions)
GLOBAL VARIABLE
• Defined outside any functions
• Can be used anywhere later in blocks of code:
Example: #include <iostream>
using namespace std;
int g; //Global variable

int main(void)
{
g = 0;
}
• Only one instance of any name can exist at the same level:
int a;
int a;
is illegal.
GLOBAL VARIABLE: EXAMPLE 1
CODING OUTPUT
#include <iostream>
using namespace std;
void f(int, int);
int a = 3;
int main()
{
cout << a << endl;
f(a, 4);

}
void f(int x, int y)
{
x += a;
a += 2*y;
cout << x << " " << a << "\n";
}
GLOBAL VARIABLE: EXAMPLE 2
CODING OUTPUT
#include <iostream>
using namespace std;
void anotherFunction();
int num = 2;

int main() {
cout << "In main, num is " << num << "\n";
anotherFunction();
cout << "Back in main, num is " << num << "\n";
return 0;
}

void anotherFunction() {
cout << "In anotherFunction, num is " << num <<
"\n";
num = 50;
cout << "Now the num is " << num << "\n";
}
GLOBAL VARIABLE: EXAMPLE 3
CODING OUTPUT
#include <iostream>
using namespace std;
void printTwoNumbers(int, int);
int globalVar; //global variable declaration

int main(){
int a=5, b=10;
globalVar=99;
cout << "\nBefore, globalVar in main() is " << globalVar;
printTwoNumbers(a,b);
cout << "\nAfter, globalVar in main() is " << globalVar;
}

void printTwoNumbers(int num1, int num2)


{
globalVar=77;
cout << "\nFirst Number is " << num1;
cout << "\nSecond Number is " << num2;
cout << "\nglobalVar in printTwoNumbers() is " <<
globalVar;
}
EXERCISE 1: FIND THE ERRORS
1. #include <iostream>
2. void printTwoNumbers(int num1, int num2);
3. int main(void)
4. {
5. m = 3, n = 56;
6. printTwoNumbers(c,d);
7. }
8.
9. printTwoNumbers( num1, num2) ;
10.{
11. int num1 = 9;
12. cout << "Num1 is " << num1 << "\n";
13. Cout << "Num2 is " << num2 << "\n";
14.}
EXERCISE 2: FIND THE OUTPUT
CODING OUTPUT
#include <iostream>
using namespace std;
void func(int);
int z = 2;
int main() {
int x = 3;
cout << "main: x = " << x << ", z = " << z << endl;
func(x);
cout << "main: x = " << x << ", z = " << z << endl;
func(z);
cout << "main: x = " << x << ", z = " << z << endl;
return 0;
}
void func(int x) {
x = x + 1;
z = x + z + 1;
cout << "func: x = " << x << ", z = " << z << endl;
}
PASSING VARIABLES TO A FUNCTION
• In memory location, a variable has both a value and
a unique address
• In passing a variable to function, you can pass either
the variable’s value (pass by value) or its address
(pass by reference) to the receiving function
PASSING BY VALUE
• In a pass by value, the computer passes the contents
of the variable to the receiving function
• Receiving function is not given access to the
variable in memory
• It cannot change value stored inside variable
• By default, variables are passed by value in C++
EXAMPLE 1: PASS BY VALUE
int SoftwareEngine(int m); a
-4
int main()
{
int a = -4;

SoftwareEngine(a); value copied


cout << "\n" << a; displays –4
}

int SoftwareEngine(int m) m
{
m = m + 3; –4
return m;
}
EXAMPLE 2: PASS BY VALUE
CODING OUTPUT
#include <iostream>
using namespace std;
void test(int a);

int main()
{
int x = 50;
test(x);
cout << "Value x now is " << x;
return 0;
}
void test(int a)
{
a += 50;
}
EXAMPLE 3: PASS BY VALUE
CODING OUTPUT
#include <iostream>
using namespace std;
int security(int t);
int main()
{
int i = 0;
cout << " before call i = " << i << "\n";
i = security(i);
cout << " after call i = " << i << "\n";
return 0;
}
int security(int t)
{
t = t + 10;
return t;
}
EXAMPLE 4: PASS BY VALUE
CODING OUTPUT
#include <iostream>
using namespace std;
int num = 8;
void changeMe(int num);
int main()
{
num = 12;
cout << " In main: " << num << "\n";
changeMe (num);
cout << " In main:: " << num << "\n";
return 0;
}
void changeMe(int x)
{
num = 0;
x += num;
cout << " output is: " << x << "\n";
}
PASSING BY REFERENCE
• Passing a variable’s address is referred to as passing by reference
• Receiving function has access to memory location of the passed
variable
• Use ONLY when you want receiving function to change the contents
of variable

• To pass a variable by reference in C++


• The actual parameter in the calling function MUST be variable(s)
• The formal parameter(s) MUST include the reference operator, the
ampersand (&) symbol
EXAMPLE 1: PASS BY REFERENCE
void security(int &x); a

int main() After the data type of parameter,


{ include reference operator (&)
int a = 7;
Specify the variable to be passed
to the function
security(a);
Reference
cout << a;
Display 10
return 0;
}

void security(int &x) Address


{ Reference
x = x + 3; Require reference
operator (&) X
}
EXAMPLE 2: PASS BY REFERENCE
CODING OUTPUT
#include<iostream>
using namespace std;
void test(int &a);

int main()
{
int x = 50;
test(x);
cout << "Value x now is " << x;
return 0;
}
void test(int &a)
{
a += 50;
}
EXAMPLE 3: PASS BY REFERENCE
CODING OUTPUT
#include<iostream>
using namespace std;
void passByRef(int &k);

int main() {
int i=0;
cout << "The value of i before call " << i << endl;
passByRef(i);
cout << "The value of i after call " << i << endl;
}

void passByRef(int &k) {


k = k + 10;
}
EXAMPLE 4: PASS BY REFERENCE
CODING OUTPUT
#include<iostream>
using namespace std;
void changeMe(int &aValue);

int main() {
int num = 12;
cout << "In main function, number is " << num;
changeMe(num);
cout << "\nBack in main again, number now is " << num;
return 0;
}

void changeMe(int &myValue) {


myValue = 0;
cout << "\nIn changeMe, the value is " << myValue;
}
PASS BY VALUE VS. PASS BY REFERENCE
• Pass by value
• a copy of data is created and placed in a local variable in the called function
• ensure that regardless of how the data is manipulated and changed in the
called function, the original data in the calling function are safe and
unchanged
• Pass by reference
• sends the address of a variable to the called function
• anytime we refer to the parameter, therefore we actually referring to the
original variable
• if the data is manipulated and changed in the called function, the original
data in the calling function are changed
EXAMPLE: PASS BY VALUE VS. PASS BY REFERENCE
CODING OUTPUT
#include<iostream>
using namespace std;
void passByValue(int x, int y);
void passByReference(int &x, int y);
int main(){
int a=3;
passByValue(a,4);
cout << "\nValue a after passByValue() is " << a;
a=3;
passByReference(a,4);
cout << "\nValue a after passByReference() >> " << a << endl;
return 0;
}
void passByValue(int x, int y){
int t = 6;
x += t;
}
void passByReference(int &x, int y){
int t = 6;
x += t;
EXERCISE 1
CODING OUTPUT
#include<iostream>
using namespace std;
void twin(int &);
int main(){
int value = 4;
cout << "In main, value is " << value << endl;
cout << "Now calling twin...\n";
twin(value);
cout << "Now back in main, the value is " << value;
return 0;
}
void twin(int &refVal)
{
if (refVal >= 2)
refVal *= 2;
else
refVal -= 2;
EXERCISE 2: PASS BY VALUE VS. PASS BY REFERENCE
CODING OUTPUT CODING OUTPUT
void f(int, int); void f(int &, int);
int a = 2; int a = 2;
int main() int main()
{ {
int a = 9; int b = 9;
f(a, 4); f(b, 4);
cout << a << endl; cout << b << endl;
return 0; cout << a;
} return 0;
void f(int x, int y) }
{ void f(int &x, int y)
x += y; {
a += 3 * y; x += y;
} a += 3 * y;
}
EXERCISE 3
CODING OUTPUT
#include<iostream>
using namespace std;
void anotherFunction(int &daisy, int rose);
int main(){
int first, second;
first = 5;
second = 10;
anotherFunction(second, first);
cout<<first<<" "<<second;
return 0;
}
void anotherFunction(int &daisy, int rose){
int flower;
flower = 25;
rose = 5 + flower;
daisy = flower * rose;
}
EXERCISE 4
CODING OUTPUT
void do_something(int &thisp, int that);
int main(void)
ANSWER??
{
int first, second;
first = 1; A. 35 2
second = 2; B. 1 35
do_something(second, first); C. 35 7
cout<<first<<" "<<second; D. 1 2
return 0;
}
void do_something(int &thisp, int that)
{
int the_other = 5;
that = 2 + the_other;
thisp = the_other * that;
}
EXERCISE 5
CODING OUTPUT
void num(int &z);
int x, y;
int main(){
x = 2; y = 3;
cout << x << " " << y << endl;
num(x);
cout << x << " " << y << endl;
num(y);
cout << x << " " << y << endl;
return 0;
}
void num(int &z){
int y;
y = z + 2;
z = y * 3;
}
EXERCISE 6
CODING OUTPUT
void chem(int a, int &b, char c);
int main() {
int num1 = 12, num2 = 15;
char ch = 'H';
cout << num1 << " " << num2 << " " << ch << endl;
chem(num1, num2, ch);
cout << num1 << " " << num2 << " " << ch << endl;
return 0;
}
void chem(int a, int &b, char c) {
int one, x;
one = a;
a++;
x = b * 4;
c = 'W';
cout << a << " " << x << " " << c << " " << one << endl;
}
PROGRAMMING EXERCISE
QUESTION ANSWER
By using pass by reference, write a full
C++ program that updates user’s
personal data (age/height). You must use
the following function in your program
1. void AGE(int &)
Take
2. void Height(float &)
Home
Given the sample output as shown below
Exercise
CONCLUSION
• We have learnt the following:
• variable scope
• global vs local variable
• pass by value vs pass by reference
REFERENCES
1) Aminatul Solehah Idris, Jasrul Nizam Ghazali, Mohamad Norzamani Sahroni, Muhd Eizan
Shafiq Abdul Aziz, Noorazida Mohd Idris, Norzaidah Md Noh, Nurhilyana Anuar, Raudzatul
Fathiyah Mohd Said, Teh Faradilla Abdul Rahman, Zakiah Noh, Zamri Abu Bakar,
Practical C++ for Beginners, 1st, Djuta Dallek Enterprise, 2025, ISBN: 978-629-95317
2) Paul Dietel, Harvey Dietel, C++ How to Program, 10, Pearson, 2017, ISBN: 9780134448237
3) Behrouz A. Forouzan, Richard Gilber, C++ Programming: An Object-Oriented Approach,
2022, ISBN: 9789355321305
4) Bhushan Trivedi, Programming with ANSI C++, 2 Edition, Oxford Higher Education,
Oxford University Press, 2014, ISBN: 9780198083962
5) D. S. Malik, C++ Programming, 5, Cengage Learning, 2011, ISBN: 9780538798136
6) Alan Evans, Kendall Martin, Mary Anne Poasty, Technology in Action, 11, Pearson, 2015,
ISBN: 129208235

You might also like