call by reference

This is a discussion on call by reference within the C Programming forums, part of the General Programming Boards category; hi there, an array will be passed by using the argument passing, but the array element will not be changed. ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    22

    Angry call by reference

    hi there,

    an array will be passed by using the argument passing, but the array element will not be changed.

    this is my instant problem.
    thank u buddy.
    Jackie

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    17
    Hello jackie,

    Could you pls put your code here? let me check it.
    b/c i think it should work well.


    Tom

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    void pass_by_value_function( int number);

    void pas_by_reference_function( int * number );



    int main()
    {

    int num = 10;

    printf("1) Num = %i \n\n",num);

    pass_by_value_function( num);

    printf("3) Num = %i \n\n",num);

    pass_by_reference_function( &num);

    printf("5) Num = %i \n\n",num);

    getch();

    return 0;

    }

    void pass_by_value_function( int number)
    {
    number = 200;
    printf("2) Num = %i \n\n",num);
    }


    void pas_by_reference_function( int * number )
    {
    number = 9999;
    printf("4) Num = %i \n\n",num);
    }





    In other words, pass by reference and the number can be altered, pass by value, and it cannot.

    If you wish to pass by reference without changing the value of the variable, copy the initial values into temp variables within the function and change the temp variables.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi, hate to do this sabastiani...check code for mistakes.

    Code:
    void pass_by_value_function( int number); 
    
    void pas_by_reference_function( int * number ); 
    /* here is a s missing from pas ..minor if typed in a hurry*/
    
    
    int main() 
    { 
    
    int num = 10; 
    
    printf("1) Num = %i \n\n",num); 
    
    pass_by_value_function( num); 
    
    printf("3) Num = %i \n\n",num); 
    
    pass_by_reference_function( &num); 
    
    printf("5) Num = %i \n\n",num); 
    
    getch(); 
    
    return 0; 
    
    } 
    
    /*below num is undeclared in function*/
    void pass_by_value_function( int number) 
    { 
    number = 200; 
    printf("2) Num = %i \n\n",num); 
    } 
    
    /* also num undeclared in funct, also misuse of pointers*/
    void pas_by_reference_function( int * number ) 
    { 
    number = 9999; 
    printf("4) Num = %i \n\n",num); 
    }
    
    /*this should be    
    void pass_by_reference(int *number)
    {
      *number = 9999;
      printf("4) Num = %i \n\n", *number); 
    }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21