Code About Us Tutorial


  Functions
    What are Functions?
    Function Definition
    Functions as Parameters to Functions
    Return Values
    Separating Functions
    Passing By Value
    Passing By Reference
    Passing By Pointer
    Inline Functions
    Overloading Functions


   
 

Home > Functions > Passing By Value

November 30, 2009 9:48 am

   

Passing By Value

Passing by value is easy, infact you have already passed arguments to functions by value many times.

Here is what you do:

 

 
//pass by value
#include <iostream.h>

// an example function void printage(int num) { cout << num << endl; }

int main(void) { int myage = 18;

printage(myage); // pass "age" by value

return 0; }

What passing by value does is that it copies your variable to a temporary variable and uses that copy instead of the original variable, thus assuring that your original variable will not get altared.

This is very usefull when sending small types of variables to a function, like most of the basic types: int, char, long, float, etc... But when you are trying to send large amounts of data, passing by value will not be a good idea since it produces an internal copy of the data.

For those purposes, the next section describes how you can pass your variable by reference.Please Rate this Code:


    Comments for: Passing By Value
Annonymouse User says:
It would be better to understand if you go on and explain in deeper detail of what you mean.

Annonymouse User says:
It would be better to understand if you go on and explain in deeper detail of what you mean. says:
what are you talking about... it speaks for itself




Add Comment:
Name:
Email:


 «1» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.