Ravager Posted October 11, 2004 Share Posted October 11, 2004 I have this program: #include <iostream> using namespace std; #include <cmath> void windchill (double airtemp, double windspeed, double chillindex) { if (0 >= chillindex && chillindex > -25) { cout << "Discomfort." << endl; return; } if (-25 >= chillindex && chillindex > -45) { cout << "A risk of skin freezing (frostbite)." << endl; return; } if (-45 >= chillindex && chillindex > -60) { cout << "Exposed skin may freeze within minutes." << endl; return; } if (-60 >= chillindex) { cout << "Exposed skin may freeze in under 2 minutes." << endl; return; } } int main() { double airtemp, windspeed, chillindex; airtemp = 0.0; windspeed = 0.0; chillindex = 0.0; cout << "Welcome to the wind chill index program." << endl; cout << "Please enter air temperature in degrees Celcius." << endl; cin >> airtemp; cout << "Now enter the wind speed in kilometers per hour." << endl; cin >> windspeed; if (windspeed < 4.8) { cout << "Wind chill only applies to wind speeds of over 4.8km/h." << endl; return 0; } chillindex = 13.12 + (0.6215 * airtemp) - (11.37 * (pow(windspeed, 0.16))) + (0.3965 * airtemp * (pow(windspeed, 0.16))); cout << "If the temperature is " << airtemp << " degrees Celcius, and the wind speed is " << windspeed << " kilometers per hour...\n \n" << endl; cout << "The wind chill index will be " << chillindex << " degrees Celcius.\n \n" << endl; cout << "This could mean:" << endl; windchill(airtemp, windspeed, chillindex); cout << endl; return 0; } and, as you can see, I'm using only pass-by-value here. Can someone explain what pass-by-reference is, and use a snippet of my code to explain its usage? Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 12, 2004 Share Posted October 12, 2004 Say you have a function called swap: void swap(int a, int b) { int temp = a; a = b; b = temp; } int main(int argc, char** argv) { int x = 7; int y = 5; swap(x,y); printf("x: %d y: %d\n",x,y); } Swap should swap them, but in this case, what is printed is: x: 7 y: 5 not x: 5 y: 7 To do the swap function we need to pass by reference: void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } int main(int argc, char** argv) { int x = 7; int y = 5; swap(&x,&y); printf("x: %d y: %d\n",x,y); } This now prints out the correct output. Passing by value gives two completely new variables, while passing by reference allows you to change variables from another function. This is useful for passing back multiple return values (i.e. the return values are stored in the pass by reference variables), since most languages only allow one return value. The standard library functions make good use of this. Link to comment Share on other sites More sharing options...
0 Mouton Posted October 12, 2004 Share Posted October 12, 2004 (edited) There's no reason for u to use pass-by-ref here. An example of that would be: int doSomething(int num1) { int num2 = 5; return num1 + num2; } main () { int num1 = 2; num1 = doSomething(num1); // here, num1=7 } In this case, u have a function which takes an int, and return that int + 5. The result is put back in the variable passed as an argument. This argument is passed by value here. void doSomething(int &num1) { int num2 = 5; num1 = num1 + num2; } main () { int num1 = 2; doSomething(num1); // here, num1=7 } This example does the same as the 1st one, except the variable is passed by reference. So here, the function doesn't have to return anything. It just modify the variable it received in argument. *edit: As kjordan2001 showed, u can also pass by reference using another notation: void doSomething(int *num1) { ... doSomething(&num1); ... It does the same thing. The difference is that if u use this notation, u'll have to use *num1 instead of num1. Link to comment Share on other sites More sharing options...
0 Ravager Posted October 12, 2004 Author Share Posted October 12, 2004 So in what ways would my code differ if I used pass-by-reference instead of pass-by-value? Link to comment Share on other sites More sharing options...
Question
Ravager
I have this program:
#include <iostream> using namespace std; #include <cmath> void windchill (double airtemp, double windspeed, double chillindex) { if (0 >= chillindex && chillindex > -25) { cout << "Discomfort." << endl; return; } if (-25 >= chillindex && chillindex > -45) { cout << "A risk of skin freezing (frostbite)." << endl; return; } if (-45 >= chillindex && chillindex > -60) { cout << "Exposed skin may freeze within minutes." << endl; return; } if (-60 >= chillindex) { cout << "Exposed skin may freeze in under 2 minutes." << endl; return; } } int main() { double airtemp, windspeed, chillindex; airtemp = 0.0; windspeed = 0.0; chillindex = 0.0; cout << "Welcome to the wind chill index program." << endl; cout << "Please enter air temperature in degrees Celcius." << endl; cin >> airtemp; cout << "Now enter the wind speed in kilometers per hour." << endl; cin >> windspeed; if (windspeed < 4.8) { cout << "Wind chill only applies to wind speeds of over 4.8km/h." << endl; return 0; } chillindex = 13.12 + (0.6215 * airtemp) - (11.37 * (pow(windspeed, 0.16))) + (0.3965 * airtemp * (pow(windspeed, 0.16))); cout << "If the temperature is " << airtemp << " degrees Celcius, and the wind speed is " << windspeed << " kilometers per hour...\n \n" << endl; cout << "The wind chill index will be " << chillindex << " degrees Celcius.\n \n" << endl; cout << "This could mean:" << endl; windchill(airtemp, windspeed, chillindex); cout << endl; return 0; }and, as you can see, I'm using only pass-by-value here.
Can someone explain what pass-by-reference is, and use a snippet of my code to explain its usage?
Link to comment
Share on other sites
3 answers to this question
Recommended Posts