Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,674 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 3,054 people online right now. Registration is fast and FREE... Join Now!




Pass Variables By Reference

 
Reply to this topicStart new topic

Pass Variables By Reference

area
post 29 Nov, 2005 - 02:52 PM
Post #1


New D.I.C Head

*
Joined: 22 Nov, 2005
Posts: 22


My Contributions


when you using pass by reference do you have to start every main program and function program with: example : void? all the examples i have seen to help with my next program all start with void? i know i have to use the & sign. any suggestions would be great, cause i gotta get cracking on this next assigment, which i'm sure i'll be asking more questions cool.gif
User is offlineProfile CardPM

Go to the top of the page


Videege
post 29 Nov, 2005 - 03:08 PM
Post #2


rêvant.toujours

Group Icon
Joined: 25 Mar, 2003
Posts: 1,404



Dream Kudos: 150
My Contributions


Quick and short: No, your functions which take pointers as parameters do not have to be of type void, for example:

CODE

bool modifiedVariables(int *number1, int *number2)
{
if (number1++ < 100 && number2++ < 100)
return true;
else
return false;
}


Useless function but it gets the point across. Basically when using pass by reference you allow function parameters to point (hence pointers) to permanent variables (normal reference variables die when the function completes), thus allowing you to modify them through the function. Usually this is used when you want to perform actions on multiple variables and have those actions be permanent (as you can only return one value from a function, or the value type you want to return needs to be different than those of reference, as above in the example).

You would pass some numbers to my function like this:

CODE

void main()
{
int myVar1=2, myVar2 = 5;
while (modifiedVariables(&myVar1, &myVar2))
cout << "Numbers < 100";
}


This post has been edited by Videege: 29 Nov, 2005 - 03:10 PM
User is offlineProfile CardPM

Go to the top of the page

area
post 29 Nov, 2005 - 03:16 PM
Post #3


New D.I.C Head

*
Joined: 22 Nov, 2005
Posts: 22


My Contributions


thanks. we just started this in class today so maybe as the days go on it will make some more sense.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 07:21AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month