Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call by reference ? I have an error.
#1
Dear sir, Salaam, Kindly check my this code. I have an error. Please point out my mistake. I want to send the reference of variable x to the function square. bye. --------------------------------------------------------------------- #include using namespace std; void square(double); int main() { double x; x = 123.456; cout << "\n" << "In main(), before calling square(), x = " << x; square(&x); //Passing address of the variable x cout << "\n" << " In main(), after calling square(), x = " << x; } void square(double *x) //read as: x is a pointer of type double { *x = *x * *x; //Notice that there is no space in *x }
Reply
#2
Below is the correct modified code of your program. Compare this code with your previous code and try to point out your mistakes by yourself.

Code:
#include<iostream.h> using namespace std; void square(double *); int main() { double x; x = 123.456; cout << "\n" << "In main(), before calling square(), x = " << x; square(&x); //Passing address of the variable x cout << "\n" << " In main(), after calling square(), x = " << x; system("pause"); } void square(double *x) //read as: x is a pointer of type double { *x = *x * *x; } //Notice that there is no space in *x
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  call by value, call by reference,call by pointer in data structure 2012 Vuhelper 0 4,333 01-04-2012, 10:28 PM
Last Post: Vuhelper
  What is meant by "pass by value and pass by reference" in c++? Vuhelper 0 2,549 11-05-2011, 05:11 PM
Last Post: Vuhelper
  stack during function call in C++ 2011 Vuhelper 0 2,027 11-05-2011, 05:05 PM
Last Post: Vuhelper
  A simple pthread program runtime error in c++ 2011 Vuhelper 0 898 07-23-2011, 05:39 PM
Last Post: Vuhelper
  floor conversion error in c++ 2011 Vuhelper 0 856 07-22-2011, 08:49 PM
Last Post: Vuhelper
  Passing 2d array by reference June 2011 Vuhelper 0 920 06-19-2011, 06:11 PM
Last Post: Vuhelper
  Define Call by reference in c++ 2011 Vuhelper 0 3,246 06-01-2011, 10:58 PM
Last Post: Vuhelper
  Call by Value in c++ 2011 Vuhelper 0 2,363 06-01-2011, 10:47 PM
Last Post: Vuhelper
  C++ have Virtual Function vs Function Pointer call 2011 Xpert 0 5,918 05-12-2011, 09:03 PM
Last Post: Xpert
  explain error handling in c++ 2011 saneha 0 2,358 05-02-2011, 06:52 PM
Last Post: saneha

Forum Jump:


Users browsing this thread: 1 Guest(s)