Passing pointers / address as parameter to functions in C++ basics

Below is the code from the C++ basics course, Unit 4 on arrays and pointers

#include <iostream>
using namespace std;

void increment(int &number) {
    cout << "The address of number is: " << &number << endl;
    cout << "Now the value of number is: " << number << endl;

int main() {
    // Declare the number
    int number = 42;
    // Print the number's value
    cout << " The value of number is: " << number << " and the address is: " << &number << endl; 
    return 0;

In below line, we are passing the integer number to the function as a parameter,.

    // Call the function increment

In below line, the function is defined such that it accepts the address of an integer as a parameter.

void increment(int &number)

But in the program , when we are calling the function, we are passing an integer as a parameter instead of address,like say, increment(&number); can i know why this is?

That is passing by reference, you will alter the value of the variable when gets in the function and will reflect the value outside the function changed.
Check the w3schools example

1 Like

Thanks @issaiass , let me readup on the link you shared

This topic was automatically closed after 20 hours. New replies are no longer allowed.