Computer Programming web Web programming Tips



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

By Sergey Skudaev

Happy Amazon Prime Day July 12 - Exclusive Deals for Prime Members - Start Free 30-Day Trial

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.

#include<iostream.h>

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;

	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.h>
#include "switchnumber.h"

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"

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;



	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



Subscribe to our mail list to receive free code examples.

Comments

No comments yet.

Add Comment

* Required information
(never displayed)
 
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
 
1000
Enter the third word of this sentence.
 
Enter answer:
Captcha
Refresh
 
Enter code:
 
Notify me of new comments via email.
 
Remember my form inputs on this computer.
 
I have read and understand the privacy policy. *
 
I have read and agree to the terms and conditions. *
 
 
Powered by Commentics

My eBooks on Amazon.com

Learn C++ Programming By Examples

Learn SQL Programming By Examples

Learn PHP Programming by Examples

Visual Basic Programming By Examples

How to Build Your Own Web Site from Scratch