#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3

    Return int value of a function ?


    What is the general proper way to use an INTEGER value returned by a function?

    For example:
    Code:
    int function1(...)
    {
    ...
    }
    
    
    int main()
    {
        int ret=function1(....);  <----calling the function
    
    
        ret=ret+1;
    ....
    ..
    
    ....
    Will "ret" CORRECTLY eavaluate to the integer i want ?

    Because for example, i know that you can't use the raw name of the function immediately- you have to declare an int which will be assigned to the name of function1.

    Just to know if i should dig inside the function for my mistake or is it the wrong syntax above ?? because he messes up something with a pointer/address.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,251
    Rep Power
    2222
    int ret=function1;

    You know that is wrong, but I still don't understand what you want to do. As written, that is taking the address of function1 and assigning it to ret, but since ret is an int and not a function pointer that will throw a warning. That should be written thus (assuming that the function takes no arguments):
    int ret=function1();
    {ABE: OK, you edited that before I finished my reply.}

    As for how to interpret and use a return value, that all depends on what the return value means. Go to the man pages of various library functions for examples. On each man page, there is a section called Return Value which describes what return values you should expect and what they mean. scanf returns the number of conversions that it performed. printf returns the number of characters printed. scanf will also return EOF if either end-of-file is reached or an error happens, while printf will return a negative value to indicate an error. Similarly, recv, a function used with TCP sockets, will return either the number of bytes received, a -1 if it encountered an error, or zero if the other host is shutting down the connection. strcmp will return a zero if the strings match, a negative number if the first string is less than the second, or a positive value if it's greater than the second. fopen will return a pointer to a FILE struct for the file that just opened or NULL if the open failed.

    What the return value means can be completely different for different functions. Even though you may start to see patterns forming (NULL or a negative number in case of error), you need to look up what each function's return values mean. And what those return values mean will determine how you should use them.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    My function receives a few arguments, one of them being a large array of characters. It's supposed to return a certain number (like 5,6,7). Instead i get some address: 18944777222 or something of the sort.

    INSIDE the function, JUST BEFORE the return line, i print the value to be returned and it's the RIGHT value. But RIGHT AFTER it gets to main it becomes an ADDRESS no matter what i try to do.

    How to turn it into that same int that was on its way from the function to main but never made it ? I tried the following:
    Code:
    create_list(text,i);
    int ret=create_list;
    printf("%d", ret);
    and still got an address

    I didn't want to shovel about the rest of the code, just the syntax of the return in general-- does it still look wrong ? If no, i'll just keep digging.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,251
    Rep Power
    2222
    Code:
    create_list(text,i);
    int ret=create_list;
    printf("%d", ret);
    As I already told you, that create_list is the address of the function, not a function call. You even corrected it, but now you're making the same mistake again.

    First you make an actual function call, but you throw the return value away. Then you set ret, which is an int, to the address of the function. Your compiler should be having fits over that, throwing warnings at you! You keep getting an address, because that is what you keep setting ret to!

    Why don't you simply assign the return value of the function call to ret?
    int ret = create_list(text,i);
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    Yeah, probably something else is messed up in my entire program.

    Because you know when you use the function power(), you can do this: printf("2^3=%d", power(2,3)); WITHOUT storing it in a ret variable first...Just using its raw name.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,251
    Rep Power
    2222
    No, that's not "just using its raw name". You're using a function call as an expression. An expression evaluates to a value. The value of a function call is its return value.

    Similarly, an assignment statement is basically: <lvalue> = <rvalue>;
    An lvalue (from "left value") is an expression that must evaluate to a location where the value being assigned is to be stored; that can be a variable name, a dereferenced pointer, or the like. An rvalue (from "right value") is an expression that evaluates to the value that is to be stored. Therefore, in

    double num = power(2,3);

    double num is the lvalue and power(2,3) is the rvalue. num is the name of the variable where the value of the rvalue will be stored. power(2,3) is the expression that will be evaluated to obtain the value to be stored, with in this case is the return value of the function call.

    It has nothing directly to do with a function name, "raw" or otherwise. It has everything to do with lvalue and rvalue expressions.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    It suddenly seems to work, even like this: printf("Return value is: %d", function1(text));

    The Lvalue and Rvalue probably solved it.

    I just put it in a completely new source file, wrote a smaller code. Trying to add layer by layer to see where it falls.

    Thanks a lot.

IMN logo majestic logo threadwatch logo seochat tools logo