Thread: A fundamental question on 'return' statement

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    A fundamental question on 'return' statement

    I've an extremely n00bish doubt regarding 'return' statement. If I comment out second last code in attoi function; the output is only '6'; instead of 123456 (I figured out the formatting portion to print 123456). Why does 'return' chooses to print only last element from the character array? Why does'nt it is returning a value for every iteration?

    I hope I sound coherent?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int attoi(char inp[]);
    
    int main(void) {
    	int l;
    	char p[5] = {49,50,51,52,53,54};
    	l = attoi (p);
    	printf ("%d",l);
    }
    /* attoi: convert s to integer */
    int attoi(char s[])
    {
       int i, n;
       n = 0;
       for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
       n = /*10 * n + */(s[i] - '0');
       return n;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A function only returns once for each call.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    So it waits for loop to get over & returns the final value?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    No, it will return the first time it reach the "return" statement, breaking out of the for loop. If you want to return several value, you should return a pointer on an int array containing all the converted values.

    Also your "for" loop is not really good, because it will probably access non allocated memory. One fix could be to give to the "attoi" function the number of char to be converted.
    Last edited by Tibo-88; 12-13-2011 at 05:46 AM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Tibo-88 View Post
    No, it will return the first time it reach the "return" statement, breaking out of the for loop. If you want to return several value, you should return a pointer on an int array containing all the converted values.
    Nope, you can't do that... C does not know how to return an array.

    Code:
    int test( void )
     { 
        int array[10];
    
        // other stuff
    
       return array; 
    }
    This fails because the array is destroyed (goes out of scope) when the function returns.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Yeah sorry, I guess I didn't expressed my idea in the good way. What I meant was returning an int* :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 4
    
    int *return_array()
    {
        int *ret = malloc(SIZE*sizeof(int));
        ret[0]=0;
        ret[1]=1;
        ret[2]=2;
        ret[3]=3;
       
        return ret;
    }
    
    
    int main()
    {
    
        int i;
        int *test = return_array();
    
        for( i = 0; i < SIZE; i++ )
        {
            printf("%d\n", test[i]);
        }
        
        free(test);    
    
        return 0;
    }
    I guess that's good right?

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Please slow down. I haven't yet reached till pointers. I thought I understood when Tibo said value will be returned once For loop is broken?

  8. #8
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Quote Originally Posted by alter.ego View Post
    Please slow down. I haven't yet reached till pointers. I thought I understood when Tibo said value will be returned once For loop is broken?
    Sorry if I made things too complicated. Then if you don't know how to use pointer yet, I guess your only solution would be to have an "attoi" function taking one char as paramter, and returning only one int, and call it for every char in you array, maybe storing the returned values in an int array.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Tibo-88 View Post
    Sorry if I made things too complicated. Then if you don't know how to use pointer yet, I guess your only solution would be to have an "attoi" function taking one char as paramter, and returning only one int, and call it for every char in you array, maybe storing the returned values in an int array.
    C functions can return only a single value.

    When they hit the return statement, the program resumes from the next step after the fuction call... so it's no longer running the function... thus only 1 value comes back. If you want multiple return values you need to feed it a pointer to an array or run the function multiple times.

    So tibo is right... if you aren't familiar with dynamic memory and pointers yet, you're reduced to writing a function that converts single values... Of course you could always study ahead to learn how to solve the problem.
    Last edited by CommonTater; 12-13-2011 at 08:43 AM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Tibo-88 View Post
    Yeah sorry, I guess I didn't expressed my idea in the good way. What I meant was returning an int* :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 4
    
    int *return_array()
    {
        int *ret = malloc(SIZE*sizeof(int));
        ret[0]=0;
        ret[1]=1;
        ret[2]=2;
        ret[3]=3;
       
        return ret;
    }
    
    
    int main()
    {
    
        int i;
        int *test = return_array();
    
        for( i = 0; i < SIZE; i++ )
        {
            printf("%d\n", test[i]);
        }
        
        free(test);    
    
        return 0;
    }
    I guess that's good right?
    That's better. Even easier since he's handing it the array pointer in the function call would be to alter the array directly.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks for taking out time for the explanation.

    At this moment I don't want to return multiple value. I am just intrigued why multiple value isn't getting returned. As per my understanding since output is 6 (which is the numerical equivalent of last element of the array) I am making myself understand that last value is only returned after the for is broken? Any flaw in this logic?

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alter.ego View Post
    Thanks for taking out time for the explanation.

    At this moment I don't want to return multiple value. I am just intrigued why multiple value isn't getting returned. As per my understanding since output is 6 (which is the numerical equivalent of last element of the array) I am making myself understand that last value is only returned after the for is broken? Any flaw in this logic?
    Ok you need to understand how functions work...

    Call function .................................................. ........... I'm over here now
    .................................................. ..............................Running the function
    .................................................. ..............................call return
    The function is over
    I'm back here now continuing
    the program.


    Get it? return terminates the function and the program continues from the next step after the function call.
    It's over... function done.
    You will not get multiple returns because the first time it sees return... the function exits.

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks for that detailed explanation on Function. I didn't say multiple values will be returned. Ok...at the risk of sounding absolute pinhead, here's what I understood:

    Main program calls function attoi(...) - attoi invoked. For loop checks condition and n is calculated accordingly, which is 1 (first iteration). For loop is checked again and second time n is calculated as 2 & likewise.

    I am still within attoi function. At the 6th iteration the For loop is completed its value is 6. This value is then passed to main() through return. The returned value is collected via a variable which is subsequently printed.
    Last edited by alter.ego; 12-13-2011 at 11:28 PM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Close enough.

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner question about the Return statement
    By lucidrave in forum C Programming
    Replies: 3
    Last Post: 08-07-2009, 05:19 PM
  2. Help with using the return statement
    By mkdl750 in forum C Programming
    Replies: 4
    Last Post: 07-23-2008, 10:14 AM
  3. A question about return statement
    By babu in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 05:18 AM
  4. Return Statement
    By Flakster in forum C++ Programming
    Replies: 8
    Last Post: 10-13-2005, 12:59 PM
  5. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM