Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++: Return 0

Options
  • 09-04-2005 5:07pm
    #1
    Registered Users Posts: 3,437 ✭✭✭


    Ok, I'm trying to make a simple program using functions for the first time. A friend indicated something about using the return command to return something other than 0 (which I've never done before).
    Both in my course notes, and in the online sources I've checked they jump straight into using this with no explaination.
    So essentially, what does the return command do? So far I've only used it to end a program, but what is it actually doing?

    Thanks.


Comments

  • Closed Accounts Posts: 19,080 ✭✭✭✭Random


    Return basically sends whatever value your specify back to the function that called it. In the case of the main() function it sends that value back to Windows.


  • Registered Users Posts: 19 Propocus


    Crucifix wrote:
    Ok, I'm trying to make a simple program using functions for the first time. A friend indicated something about using the return command to return something other than 0 (which I've never done before).
    Both in my course notes, and in the online sources I've checked they jump straight into using this with no explaination.
    So essentially, what does the return command do? So far I've only used it to end a program, but what is it actually doing?

    Thanks.

    Hi Crucifix,
    Your friend is probably talking about a non-void type function.

    EG
    int isBigger (int x , int y)
    {// return 1 if x > y
    // otherwise return o
    .
    .
    }
    main()
    {

    int a;
    int b;
    // assign values to a & b

    int result;

    result = isBigger( a, b);

    if (result == 1)
    {
    // do something
    }
    else
    {
    // do someting else
    }

    }

    /// Propocus


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Or you could do something like this....


    int main()
    {
    int answer;
    int a=4, b=6;

    answer = addition(a, b);
    printf("The answer is: %d", answer);
    }

    int addition(int a, int b)
    {
    return(a+b);
    }

    Basically, this passes the numbers "a" and "b" to the function called addition. Then, the function adds them and returns that value. Basically, "a" and "b" are added (giving 10), and then the number 10 replaces where i wrote "addition(a, b)" in my main function (if you know what i mean).

    So at the end of the function running, the line that used to be: "answer = addition(a, b);" now reads: "answer = 10;", which then gets printed out.


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    The basic idea of functions is that you want to make a mini program within your big or main program to provide some particular functionality such as adding a & b as Mutant said. Nearly all of the time you'll want to get the result of whatever functionality you've designed in your mini function so hense the return keyword.

    Example:
    Function to see if an integer is above 10. If integer is above 10 returns true else returns false.

    boolean isGreaterTen(int x){
    if(x > 10){
    return true;
    }else{
    return false;
    }

    Oh also you specify what the return type is by putting the variable type infront of the function instead of void. So mine is boolean while mutant's is int.

    Sorry if my syntax is off I've been programming in java for the last 2 years can't remember c++ exactly, but the basic concept is the same.


  • Registered Users Posts: 221 ✭✭Elfman


    Ok im sure i'm reapeating everything thats all ready been said but im just learning this stuff too so i guess that outs me in a good position to explain it simply (hopefully)

    the odea of a function is (as was said) to make a mini function with in main this lessens the clutter amount all else (but it is essectally stuff u could do in main)

    below is a sample program witch dose power by functions

    //Power.cpp
    //E|fmAn

    #include <iostream.h>
    ...what ever esle u want

    //*************Function declaration****************//
    int PowerFun (int x, int y); //you must say what type will be return (int here)
    //********************************************//

    int main () ;
    {
    int Value, PowValue; // it will be Value to the power of PowValue
    int PowerReturn; //this is the int returned from function

    cout<<"Enter value"<<endl;
    cin>>Value;
    cout<<"Enter power"<<endl;

    PowerReturn = PowerFun(Value, PowValue);

    cout<<Value<<" To the power of "<<PowValue<<" = "<<PowerReturn<<endl;

    return 0;
    }


    int PowerFun(int x, int y)
    {
    int cnt = 0, tot = 1;

    for(cnt = 0;cnt < y; ++cnt)
    {
    Tot *= x ;
    }//for loop

    return Tot;

    }


    that should work if u have any questions email me on sean_o_dwyer@hotmail.com or pm me


  • Advertisement
  • Registered Users Posts: 4,287 ✭✭✭NotMe


    Jesus people, put your code in
    [/CODE ] tags so that it is readable  :D 
    [code]
    #include <iostream.h>
    
    int add(int, int);
    
    void main()
    {
          int a = 5, b = 4;
          cout << add(a, b);
    }
    
    int add(int a, int b)
    {
          return a + b;
    }
    
    
    


  • Registered Users Posts: 3,437 ✭✭✭Crucifix


    Wow, great response. Thanks for all the help everyone.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    NotMe wrote:
    Jesus people, put your code in [CODE ][/CODE ] tags so that it is readable :D
    What kind of idiot would put code in [code] tags! Thats just insanity ;)

    Any more questions, and we're here to help.


  • Registered Users Posts: 885 ✭✭✭clearz


    I dont know why ppl reply to posts like this. This type of stuff can be found in google in 2 seconds.


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Not if you are on dialup :eek:


  • Advertisement
  • Registered Users Posts: 221 ✭✭Elfman


    How can u complain bout people replying to "Stuff like this when u wasted ur time reading all thre post and leaving one says that !! genius

    plus often webpages are helpful but sometimes confusing and it helps to have real ppl explain things.

    not to metion it helps me to go over some stuff i havent done in a while


  • Registered Users Posts: 885 ✭✭✭clearz


    Elfman wrote:
    How can u complain bout people replying to "Stuff like this when u wasted ur time reading all thre post and leaving one says that !! genius
    I can complain if I want about this. I am a programmer and read these forums every day. I sometimes learn something new and will sometimes help if I can. But I am sick off posts where the member has made no effort to find the answer themselves before expecting someone else to help them. Pure lazyness.
    Elfman wrote:
    not to metion it helps me to go over some stuff i havent done in a while

    I am interested why you would be going over this stuff. By saying "i havent done in a while" would suggest you aint a programmer. I mean since I first officially learned about return statements way back at the beginning of first year in college they have become something that I would use every day without even thinking about it.
    If you are someone with an amature interest in programming I would suggest you use google to learn the basics instead of a reading a forum
    Elfman wrote:
    plus often webpages are helpful but sometimes confusing
    Absolute rubbish there is gigabytes of usefull texts available free on the net about programming.
    Elfman wrote:
    and it helps to have real ppl explain things.

    What does a robot type the texts and create the 1000's of web-sites got to do with programming

    Oh yea a quick flick through your posts Elf anad I come up with this:
    Urgent help .. Make programme into exe
    "can any one tell me how i make a visuall c++ programme into an exe file ??"
    What a pile of crap question FFS GTFQ. I have to admit but I cant stop laughing at that post. You might not even find the answer for that one on google. A god dam hamster could figure it out. Seriously but with a question like that you should consider not getting into programming which requires good problem solving abilities.


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    Don't be such an a**hole clearz :confused:


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    clearz wrote:
    I can complain if I want about this. I am a programmer and read these forums every day. I sometimes learn something new and will sometimes help if I can. But I am sick off posts where the member has made no effort to find the answer themselves before expecting someone else to help them. Pure lazyness.

    Well, then, don't look at those posts....

    And will people STOP USING iostream.h already? It's obsolete, and has been for a very long time. Use the new forms, or your punishment shall be great if you ever attempt to port to another platform (actually, it shall be great anyway; C++ is oddly non-standardised, compared to other ISO languages).


  • Registered Users Posts: 221 ✭✭Elfman


    Ok dude chill out!!

    I'm a student I’m really glad ur a programmer and fair play for trying to help but ur also being awful harsh

    My main point in the post was that it's much easier to have a person explain something rather than bits and pieces on a web page, also when ur learning and ur used to certain standards it's hard to learn new concept done in a different standard.

    As for ppl not looking things up I kind of agree with u but u don't know he hadn’t look and it's not ur place to judge if u think it's wrong leave it don’t answer it

    But you (in the wisdom of an almighty professional programmer) decide to dedicate a post to giving out to and belittling me and the guy who started this post

    All help appreciated lectures not ok


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    He's right, having someone explain C (or C++) is MUCH easier than trying to do it all by yourself. I'm fairly handy at the ole programming, and dont have much trouble following the handouts we get, whereas some of my friends can't make head nor tail of it.

    So i end up running through the program with them bit by bit making sure they understand the different components. Even though they do pretty much whats on the sheet, when someone is there re-wording everything and giving more examples, it makes it infinitely more understandable.


  • Closed Accounts Posts: 1,061 ✭✭✭dawballz


    clearz is obviously too good a programmer to help anyone.
    I would definately agree it is much better to have an answer to a specific question that can be asked on forums.
    Maybe Crucifix did do a search on Google and he came accross a similiar question that was asked on boards. Would you still tell him to "GTFQ"? This forum is here to help people, you don't need to reply to anything nor read anything if you don't want to.


  • Registered Users Posts: 885 ✭✭✭clearz


    Whatever


  • Registered Users Posts: 221 ✭✭Elfman


    Good one ace stay up all night think of that did u (-;


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    clearz wrote:
    I can complain if I want about this.
    Yeah, but not too much, you could end up irritating a mod.

    The hardest stuff in programming to grok is the very easiest. I can usually find the answers to quite tricky C++ questions because when I go googling I do so armed with the C++ knowledge I already have. When someone who doesn't know such basics as what the return keyword does in non-void functions goes googling they're at a loss. Really, this guy is honestly saying "help, I don't know where to begin". Fair enough.


  • Advertisement
  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    clearz wrote:
    I can complain if I want about this. I am a programmer and read these forums every day.
    ....

    If you are someone with an amature interest in programming I would suggest you use google to learn the basics instead of a reading a forum
    ....
    Absolute rubbish there is gigabytes of usefull texts available free on the net about programming.
    ....
    Oh yea a quick flick through your posts Elf anad I come up with this:
    Urgent help .. Make programme into exe
    "can any one tell me how i make a visuall c++ programme into an exe file ??"
    What a pile of crap question FFS GTFQ. I have to admit but I cant stop laughing at that post. You might not even find the answer for that one on google. A god dam hamster could figure it out. Seriously but with a question like that you should consider not getting into programming which requires good problem solving abilities.

    Of course, clearz is a Real Programmer, and would never, ever, ask a question on a forum that could be answered on Google in ten seconds by an 8yo, by simply typing in the words of a question. No question of it. He'd die first, he'd be so ashamed.

    I mean, this is aWEB DESIGN question. The internet is FLOODED with web design tutorials, documentation and forums, all hoping against hope that someone might click their Google ads.
    http://www.boards.ie/vbulletin/showthread.php?t=214689

    And then there's this:
    http://www.boards.ie/vbulletin/showthread.php?t=191366

    And as he says, he has good problem solving (or possibly causing) abilities:
    http://www.boards.ie/vbulletin/showthread.php?t=188031
    http://www.boards.ie/vbulletin/showthread.php?t=182001

    Sorry, but if you think these questions are any less Google-able than his....


  • Registered Users Posts: 221 ✭✭Elfman


    Let me jiust clear that up ,

    what i was looking for a way to have my programme hidden and just have a icon without all the files around i knew where the exe inn teh debuger was.


    I was handing in a project and i was just looking for the best nad most professtional way to present it to my lectures. not exactly criteria for judging my problem solving abilities,

    Look guys these form are for discusing programming and helping ppl should they ask i have a problem with people making post either declaring the threat as pointless or to say someone posting on it. argue with what i'm saying if it's based on something that's fine but this thread is not about where or not people should be asking for help.

    I've learned alot from these threads and when i saw this thread i was delighted to be able someone else.

    As for my earlier threads i can addmit it was a silly question (-;


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Hello. I'm your local friendly moderator.

    I'm in a very good mood today.

    Please don't do anything to change that, it's not wise.


This discussion has been closed.
Advertisement