Sponsored Links


Results 1 to 2 of 2

Thread: How to call by reference ? I have an error.

  1. #1
    sana.pari
    Guest

    Icon7 How to call by reference ? I have an error.

    Sponsored Links1


    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 }

    Sponsored Links

  2. #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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 12-26-2012, 11:41 PM
  2. Replies: 0
    Last Post: 01-04-2012, 10:28 PM
  3. Replies: 0
    Last Post: 06-24-2011, 07:44 AM
  4. Define Call by reference in c++ 2011
    By Vuhelper in forum C++ Programing
    Replies: 0
    Last Post: 06-01-2011, 10:58 PM
  5. I want to sale out reference books and handouts of mcs
    By Xpert in forum Virtual University Handouts
    Replies: 2
    Last Post: 07-16-2010, 09:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
-: Vuhelp Disclaimer :-
None of the files shown here are hosted or transmitted by this server. The links are provided solely by this site's users. The administrator's or staff of Vuhelp.net cannot be held responsible for what its users post, or any other actions of its users. You may not use this site to distribute or download any material when you do not have the legal rights to do so. It is your own responsibility to adhere to these terms. If you have any doubts about legality of content or you have any suspicions, feel free to contact us.
Online Education | JhelumSoft | Jobs