Computer Programming web Web programming Tips



Pointers and Functions in C++ - Computer Programming For Beginners

By Sergey Skudaev


Pointers

There is also type of variables that hold not a value it-self, but an address where this value is stored. This type of variables is called references or pointers. What is address? Run this simple code for illustration.

Subscribe to our code examples

responsive website

The complete source code for a responsive website with user registration and authentication.

Dowload for free

#include<iostream>
using namespace std;

int main(void)
{
	int a = 5;    //an integer a

	int *b = &a;   //pointer to address of a

	*b=10;

	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;

    int hold = 1;
    cin >> hold;

	return 0;
}

Command prompt window displays:

a=10
b=0x0065FDF4
Press any key to continue

Why output displays that a=10? In the first line of code we assign 5 to "a" variable. Because in line *b=10 we assigned 10 to "b" pointer. Since "b" holds the address of variable "a", system assign 10 to the variable, which address is "b" (0x0065FDF4)





How to understand function?

In header file in switchnumbers.h type:

void switch_by_reference (int &x, int &y);

void switch_by_value(int x, int y);

In switchnumbers.cpp file type:


#include<iostream>
#include "switchnumber.h"

using namespace std;

void switch_by_reference (int &k, int &n)
{
		int temp=0;
		temp=k;
		k=n;
		n=temp;

	cout<<"Values inside switch_by_reference function:"<<endl;
	cout<<"k="<<k<<endl;
	cout<<"n="<<n<<endl;

}

void switch_by_value(int x, int y)
{
	int temp=0;
	temp=x;
	x=y;
	y=temp;

	cout<<"Values inside switch_by_value function:"<<endl;
	cout<<"x="<<x<<endl;
	cout<<"y="<<y<<endl;

}

In main.cpp file type:

#include<iostream.h>
#include "switchnumber.h"

using namespace std;

int main(void)
{

	int a = 5;

	int b = 9;

	cout<<"Initial values:"<<endl;
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;

	cout<<"Call switch by reference function..."<<endl;

	switch_by_reference(a,b);


	cout<<"after switch by reference:"<<endl;
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;

	cout<< "Call switch by value function... "<<endl;

	switch_by_value(a,b);


	cout<<"after switch by value:"<<endl;
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;

     int hold = 1;
    cin >> hold;

	return 0;

	}

Run this code and you will get output:
Initial values:
a=5
b=9
Call switch by reference function...
Values inside switch_by_reference function:
k=9
n=5
after switch by reference:
a=9
b=5
Call switch by value function...
Values inside switch_by_value function:
x=5
y=9
after switch by value:
a=9
b=5
Press any key to continue





When you pass parameters by reference, you pass parameters (a and b) addresses and system assign new values to addresses. As a result the values of the initial variables (a and b) are switched.

When you pass parameters by value, system copies parameters (a and b) values to function local variables (x and y). As a result local variable values ( x and y ) inside the function are changed, but initial variables (a and b) that were declared outside the function are not changed. Before calling switch by value function "a" was equal 9 and "b" was equal 5. After this call, "a" still equals 9 and "b" still equals 5.

It is very important to understand the difference between passing parameters by reference and passing parameters by value.


 << PART I - Data Type >> PART III - Variable Scope



My eBooks on Amazon.com

US    UK    BR    CA
US    UK    BR    CA
US   UK   BR   CA