Professional Documents
Culture Documents
(Lecture-5)
Topic:
• Reference Variables Vs Pointers
• Pass by reference and value
• Different Functions in C++
Example:
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:
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.
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;
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
}