Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    cpp
  » Passing values by reference
      by mochman
 Page 1 of 1 
   

(Login to remove green text ads)
Code:
#include<iostream.h>

// this function will modify the passed in values without having to return anything
void refPassFunc( int &, char &);

int main(void) {
        int userInt;
        char userChar;

        cout << "Please enter a integer : ";
        cin >> userInt;
        cout << "Please enter a character : ";
        cin >> userChar;
        cout << "You have inputted " << userInt << " and " << userChar << endl;
        refPassFunc( userInt, userChar );
        cout << "The new values are " << userInt << " and " << userChar << endl;
        return 0;
}

void refPassFunc( int &intIn, char &charIn ) {
        // will add 6 to the number being passed in
        intIn += 6;
        // will set the character to 'H'
        charIn = 'H';
}
Passing by reference will pass the location of the variable instead of the variable's value. This means that when the function modifes the value of its variable, it is actually modifing the initial passed variable. Phew.
A quick note, all arrays are automatically passed by reference.




 
 Page 1 of 1 
   

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting