Thread: Function not returning value

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    12

    Function not returning value

    I think I'm missing something about passing variables to functions. This small program doesn't do what I want it to do. Its suppose to print a = 0 then call the function then print a=5, but it prints a=0 both times... what am I missing?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int blabla(int b);
    
    int main()
    {
       int a=0;
       cout<<"a = "<<a<<" before function call."<<endl;
       blabla(a);
       cout<<"a = "<<a<<" after function call."<<endl;
    }
    
    int blabla(int b);
    {
        b=5;
        return b;  
    }

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    i don't exactly know what it is your doing but if your declaring that a = o then it will always = 0

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    You need to assign the function call to something, else, the returned result is not useable.
    Code:
     a = blabla(a)

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    here
    Code:
    #include <iostream>
    
    using namespace std;
    
    int blabla(int b);
    
    int main()
    {
       int a=0;
       cout<<"a = "<<a<<" before function call."<<endl;
       cin.get();
       a = blabla(a);
       cout<<"a = "<<a<<" after function call."<<endl;
       cin.get();
    }
    
    int blabla(int b)
    {
        b=5;
        return b;  
    }

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    This wont modify var a in main() coz b is not pointing at a.
    if you want to modify the content of a then you need to pass it by reference.

    e.g
    Code:
    void test( int * );
    
    int main()
    {
       int y = 9; // cur value is 9
       test( &y ); //& for passing the memory address of variable y.
       return 0;
    }
    
    void test( int *i ) // i points to variable y in main()
    {
        *i = 8; // i is pointing to y so modifying the value of *i also modifies the value of y.
    
    }
    *edited

    used PHP tag

  6. #6
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    in c++ it is easier to use references rather than pointers which may be messy.
    Code:
    void blah(int &b){
        b = 5;
    }
    int main(){
    a =0;
    blah(a);
    cout<<a;
    return 0;
    }
    might do the work...

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Ok, I think I got it. Thanks for the help ^_^

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by wart101
    i don't exactly know what it is your doing but if your declaring that a = o then it will always = 0
    just to clarify in case anyone else reads this, that is not correct.

    declaring
    int a = 0;
    makes an integer a with the value 0. There is nothing to stop you changing the value later.

    a = 1;
    a = 2;
    a = 666;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in returning value from the dll exported function
    By dattaforit in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2006, 04:30 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM