about function

Please discuss:
a.function
b.what is the purpose of "return" in function definition, how does it terminate?
c. my common error is return statement in function definition, can you teach me what are the pointers to prevent these errors?

I'm using a Turbo c program compiler... tanx =)
My focus is in C program only.. pls discuss in basic form

Comments

  • A function (ie, routine) is a block of relevant code that has a well defined purpose and function. Er, rather, that is the best case scenario ;-)

    For example, printf() is a routine that displays a formatted screen to stdout.

    "return" provides a way to return back to the routine or piece of code that called the routine. ie, it returns from the function.

    so:[code]void foo (void) {

    return; //returns from function (here for example; can be omitted)
    }

    int main () {

    foo (); //calls foo()
    return 0; //< the return; statement returns here
    }[/code]Notice main also uses a return statement in this example. This also (on the low level) returns to the code that called it. The way this works is platform specific.

    I do not understand the last question, sorry.

  • for question c:

    example:
    void TEMPERATURE( int T, char C)
    {return (what?);
    }
    int main()
    (

    TEMPERATURE(a,b) /*the form of temperatue stands for (T^C) ex:32deg.Celsius
    return 0;
    }

  • what is the correct form of routine?

    is it from
    function prototype->function call->function definition->after the function call-> return 0?
  • : what is the correct form of routine?

    You cant return nothing from TEMPERATURE because it has a return type void. If you want it to return something (say, a float for example) give it a return type float and return a value:

    [code]float TEMPERATURE( int T, char C) { //return type float

    float ret=0.0f;
    return ret; //returns the value in "ret"
    }

    int main()
    (
    float value = TEMPERATURE(a,b); //now float will contain the return value

    return 0;
    }[/code]

    : is it from
    : function prototype->function call->function definition->after the
    : function call-> return 0?

    A function prototype is only to let the compiler know the return type and parameter types of a routine. Its not needed in all cases (ie, if you define a routine before using it.)

    Anyways, it goes (using the above example code): main() calls function TEMPATURE() -> TEMPATURE() does something and returns a value -> back in main() stores return value in variable -> main() returns 0 to operating envirement to terminate the program.

    In fact, alot of smart compiliers may eliminate the function call entirely as it does nothing at all in my example. This is a different subject though.


  • what the "ret" in "return ret" stands for?
    that's my problem, i have my difficulty in returning from function definition/prototype to main ().. Why is that there is no parenthesis in the return ret--- I mean return (ret)? is it possible?
  • [color=Blue]Pls open , my attachment I attached a tcfile.. can you check it? its all about TEMPERATURE.. I think the problem of the program is in return statment in the function prototype/ definition...[/color]
  • trebla2352,

    Please keep all things related to the same question in the same thread. Otherwise one of the threads might disappear into the flood of new threads. It is also easier to read them this way.

    I had to delete some of your posts since they didn't follow our posting policies here on the C/C++ boards. The posting policies can be found below "forum info" but also [link=http://www.programmersheaven.com/mb/CandCPP/374856/374856/posting-policies/?S=B20000#374856]here[/link]. When posting, please take a moment to formulate a question regarding something you don't understand, or post the code you are having trouble with. Simple copy/paste of your homework assignment will always be deleted.

    ---

    For the convenience of other users reading this thread, the code referred to is this:

    [code]#include
    void TEMPERATURE( float temp, char unit)
    {char b;

    scanf("%c",&b);
    switch (b)
    {case 'C': temp=5/9*(temp+32); break;
    case 'F': temp=9/5*temp -32; break;
    }
    return ;
    }
    int main ()
    {float s; char a;

    printf("Convert the TEMPERATURE(b^d):
    ");
    scanf("%f^%c",&s,&a);

    TEMPERATURE(s,a);
    printf("temperature=%f^%c",s,a);
    return 0;
    }[/code]
  • sOrry, i'm a new member of this forum.. i'm not yet read the forum rules and regulations. I'm apologizing.. thank you for reminding me..

  • : what the "ret" in "return ret" stands for?
    : that's my problem, i have my difficulty in returning from function
    : definition/prototype to main ().. Why is that there is no
    : parenthesis in the return ret--- I mean return (ret)? is it possible?

    ret was the variable that I defined in the example code.

    "return" is a keyword defined by the C++ standard that has the following forms that it may be used:

    return;
    return expression;

    If you use the first form, "return;", the routine just returns to the calling routine. If you use the second version, you can have the routine "return" a value of any type. That is, in the second form, "expression" can be a constant, mathematical expression, variable, pointer, any defined object. However, the routine must be declared to return that type.

    So, any of the below examples are valid:

    //! function returns value integral value 0
    int foo () {return 0;}

    //! function returns float
    float foo() {return 0.0f;}

    //! function returns result of a+b
    float add (float a, float b) {return a+b;}

    For the parenthesis, they are never required in expressions. In "return ret;", ret is an expression, thus the () is not required.

    Kind of like this:

    bool bVarable = varA == varB;

    Here, "varA == varB" is an expression that will either evaluate to true or false. The result is stored in bVarable...notice no () is needed.

    Yet another good example is the sizeof operator. sizeof has the form [b]sizeof expression[/b] thus can be used with or without ()'s.
Sign In or Register to comment.

Howdy, Stranger!

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

Categories