Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

I having trouble understanding the return in C++

[b][red]This message was edited by Garrett85 at 2003-10-30 14:5:47[/red][/b][hr]

IM new to programming, Iv been learning for a while now, reading different book and web sources and such and IM still pritty stupid at it. Iv never even gotten into windows programming or graphics at all. The writers of the books and web pages always start out good but then after a chapter or two they tend to think I know and understand things I dont. IM currently reading Game Programming All In One and IM trying to learn and understand Functions. Iv always just put return 0; at the end of all of my simple programs having no real knowledge of why its there or what it does. But now Iv got to return a variable that Iv created in my program (return variable) if I dont the program does not do what it is suppose to do. IM 17 and I want to be a game programmer but I dont know if I can make it, I keep getting stuck in begging level lessons and stuff like return that I just talked about. I dont know how you people learn all this. Anyway, could someone explain return, what is it and how does it work. When and why do I return what. Here is the code IM looking at. THX :-)

#include
using namespace std;

int variable(int number = 5);

main()
{
int number2, number3;

cout<< "Please enter a number";
cin>> number2;

number3 = variable(number2);

cout<< "The number is " << number3;

return 0;
}

int variable(int number)
{

int number4 = number * number;


return number4;
}

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [b][red]This message was edited by AsmGuru62 at 2003-11-3 8:16:17[/red][/b][hr]
    : [b][red]This message was edited by Garrett85 at 2003-10-30 14:5:47[/red][/b][hr]
    :
    : IM new to programming, Iv been learning for a while now, reading different book and web sources and such and IM still pritty stupid at it. Iv never even gotten into windows programming or graphics at all. The writers of the books and web pages always start out good but then after a chapter or two they tend to think I know and understand things I dont. IM currently reading Game Programming All In One and IM trying to learn and understand Functions. Iv always just put return 0; at the end of all of my simple programs having no real knowledge of why its there or what it does. But now Iv got to return a variable that Iv created in my program (return variable) if I dont the program does not do what it is suppose to do. IM 17 and I want to be a game programmer but I dont know if I can make it, I keep getting stuck in begging level lessons and stuff like return that I just talked about. I dont know how you people learn all this. Anyway, could someone explain return, what is it and how does it work. When and why do I return what. Here is the code IM looking at. THX :-)
    :
    : #include
    : using namespace std;
    :
    : int variable(int number = 5);
    :
    : main()
    : {
    : int number2, number3;
    :
    : cout<< "Please enter a number";
    : cin>> number2;
    :
    : number3 = variable(number2);
    :
    : cout<< "The number is " << number3;
    :
    : return 0;
    : }
    :
    : int variable(int number)
    : {
    :
    : int number4 = number * number;
    :
    :
    : return number4;
    : }
    :
    :
    [blue]The return values are used when the function returns and you have code which actually uses the value, some examples:
    [code]
    int DoubleIt (int iValue)
    {
    return (2 * iValue);
    }

    // Use it in a formula:
    int iCalculated = DoubleIt (6) + DoubleIt (7);
    // What is happening up here? ^^^
    // - DoubleIt (6) will RETURN 12
    // - DoubleIt (7) will RETURN 14
    // - 12 added to 14 and we get 26 inside 'iCalculated'

    // Use it in a logical statement:
    if (DoubleIt (22) == 44) {
    // ^^^ The 'DoubleIt (22)' returns 44 and your code
    // gets into this section
    }
    else {
    // But NOT in this section!
    }
    [/code]
    I hope it was clear - in any event, "C" (and so is "C++") language can use a function call anywhere in the code you can use a value of the type which function returns. In our case above the function returns type 'int', so you can use this ANYWHERE in the code where 'int' can be used! Cool! Isn't it?
    [/blue]


Sign In or Register to comment.