You are on page 1of 10

EE 355 A: Computer Programming in C++ (E)

(Lecture-5)

Topic:
• Reference Variables Vs Pointers
• Pass by reference and value
• Different Functions in C++

Department of Electrical engineering


MBM Engineering College, Jodhpur
2
Reference Variable
A reference variable is a "reference" to an existing variable, and it is created with the & operator:

Example:

string class = “C++";


string &subject = class;

cout << class << endl;


cout << subject << endl;

Memory Address
The & operator was used to create a reference variable. But it
can also be used to get the memory address of a variable; which string class = “C++";
is the location of where the variable is stored on the computer.
cout << &class; // Outputs 0x6dfed4
When a variable is created in C++, a memory address is
assigned to the variable. And when we assign a value to the
variable, it is stored in this memory address. 3
Pointer
It is a variable that stores the memory address as its value.

A pointer variable points to a data type (like int or string) of the same type, and is created with the * operator. The
address of the variable you're working with is assigned to the pointer:

string class = “C++"; // A variable of type string


string* ptr = &class; // A pointer variable, with the name ptr, that stores the address of class

// Output the value of class (C++)


cout << class << "\n";

// Output the memory address of class (0x6dfed4)


cout << &class << "\n";

// Output the memory address of class with the pointer (0x6dfed4)


cout << ptr << "\n";

4
Reference Variables Vs Pointers

Both references and pointers can be used to change local variables of one function inside another function. Both of
them can also be used to save copying of big objects when passed as arguments to functions or returned from
functions, to get efficiency gain. Despite the above similarities, there are the following differences between
references and pointers.

1. A pointer can be declared as void but a reference can never be void


2. The pointer variable has n-levels/multiple levels of indirection i.e. single-pointer, double-pointer, triple-pointer.
Whereas, the reference variable has only one/single level of indirection.
3. Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is
often done with pointers.
4. References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid
thing.
5. A reference must be initialized when declared. There is no such restriction with pointers

5
Reference Variables Vs Pointers
main()
{
a := 1 // define int
b := 2 // define int

ap = &a
// set ap to address of a (&a)
// ap address: 0x2101f1018
// ap value : 1

*ap = 3
// change the value at address &a to 3
// ap address: 0x2101f1018
// ap value : 3 ap = &b
// set ap to the address of b (&b)
a=4 // ap address: 0x2101f1020
// change the value of a to 4 // ap value : 2
// ap address: 0x2101f1018 }
// ap value : 4
6
Function
void prime(int n)
{
1. Function with no argument and no return value int i, flag = 0;
2. Function with no argument but return value for (i = 2; i <= n/2; ++i)
3. Function with argument but no return value {
4. Function with argument and return value if (n%i == 0)
{
flag = 1;
break;
}
}

if (flag == 1)
{
cout << n << " is not a prime number.";
}
else {
cout << n << " is a prime number.";
}
}
7
Return Values
The void keyword, used in the previous examples, indicates that the function should not return a value. If you
want the function to return a value, you can use a data type (such as int, string, etc.) instead of void, and use the
return keyword inside the function:

Example
int myFunction(int x) {
return 5 + x;
}

int main()
{
cout << myFunction(3);
return 0;
}

// Outputs 8 (5 + 3)

Multiple Parameter passing: the function call must have the same number of arguments as there are parameters,
and the arguments must be passed in the same order.
8
Pass By Reference void swapNums(int &x, int &y)
{
int z = x;
x = y;
y = z;
}

int main() {
int firstNum = 10;
int secondNum = 20;

cout << "Before swap: " << "\n";


cout << firstNum << secondNum << "\n";

// Call the function, which will change the values of firstNum


and secondNum
swapNums(firstNum, secondNum);

cout << "After swap: " << "\n";


cout << firstNum << secondNum << "\n";

return 0; 9
}
Return by Reference
#include <iostream> Ordinary function returns value but this function doesn't. Hence,
using namespace std; you cannot return a constant from the function.
int& test()
// Global variable {
int num; return 2;
}
// Function declaration
int& test(); You cannot return a local variable from this function.
int& test()
int main() {
{ int n = 2;
test() = 5; return n;
}
cout << num;

return 0;
}

int& test()
{
return num;
10
}

You might also like