Return Statement

This is a discussion on Return Statement within the C++ Programming forums, part of the General Programming Boards category; I have my variables stated globally, and at the end of each function, I want them to be updated globally, ...

  1. #1
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50

    Return Statement

    I have my variables stated globally, and at the end of each function, I want them to be updated globally, so that the next function is using the right numbers.

    Does return do this? How do I go about using it?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    what? I'm not 100% sure what you are asking, but return just returns a value from a function

  3. #3
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    So if I go...

    return num;

    it will take the variable that num and return it to where it was declared?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    maybe. perhaps if you explain exactly what it is you are trying to do with a small accompanying code sample we can show you how to get the effect you are after.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    Ok, I was asking the wrong question, sorry.

    If I want to return a variable, does it need to be stated with the function?

    As in, should it look like this?

    Code:
    #include <iostream>
    
    int main(int num);
    int sta();
    
    int num;
    
    using namespace std;
    
    int sta()
    {
         cout<<"Number is "<<num<<endl;
    
         system("pause");
    }
    
    int main(int num)
    {
    
    num = num + 1;
    
    return num;
    }
    More importantly, if I have a variable stated globally, every time I do something to it like change it's value (such as v = 15), wouldn't it be 15 in the next function I use it in? Regardless of returning it or not?
    Last edited by Flakster; 10-13-2005 at 12:32 PM.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes. If a variable is global, you can access it from everywhere and in a single threaded program then your last statement above would be true. Your code is flawed mind you.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    Oops, nevermind, I figured out what I was doing wrong.

    I was using return properly, but I wasn't in the right spot.

    Sorry for horribly vague questions.

  8. #8
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    Yeah, that was kind of pseudo/very crappy code I threw together, then I realized I was pretty much doing it right already, but I was thrown off by a simple mistake elsewhere in my code.

    Thanks everyone!
    Last edited by Flakster; 10-13-2005 at 12:58 PM. Reason: spelling mistake :):):):):):)

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    You really don't have to use the return to access the new data of num it is global so any function that modifies it will actually modify it and the next time it is used it will have those modifications done already I would recommend to you not to use globals they can get you into trouble sometimes.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

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