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

    Join Date
    Jul 2013
    Posts
    26
    Rep Power
    0

    Lightbulb Why can't a function return two variables


    I want to separate a number.
    that is if the input is 1234
    then the output should be 12 34
    but my code returns only the first half of the number.
    ie,the output is 12
    my code is
    Code:
    #include <stdio.h>
     int separator(int number_to_separate)
     {      
       int second_half;   
       int first_half;    
        
       second_half=number_to_separate%100;
       number_to_separate=number_to_separate/100;  
       first_half=number_to_separate; 
            
       return first_half;    /* separator returns two variables.but is there any alternatives to fix it */
       return second_half;   
          } 
    int main(int argc, char *argv[])
     {  
       int number;
       scanf("%d",&number);   
       printf("%d",separator(number)); 
       system("PAUSE");	
       return 0; 
    }
    the function separator() returns two variables first_half and second_half;but the output is first_half.
    is this the reason for my problem!! :confused: if so is there any alternative way to separate a number?? :chomp:
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    "Return" the values through the parameter list via pointers. Declare two pointers in the parameter list. When you call the function, pass to it the addresses of the variables you want to have receive those values.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by yashwanth.c.b
    the function separator() returns two variables first_half and second_half.
    No it does not - the language does not support that; return is a program flow control statement, it returns control to the caller, so the second return is unreachable code.

    You have at least two choices:

    1) Return a struct:
    Code:
    struct separated
    {
        int first ;
        int second ;
    } ;
    
    struct separated separator( int number_to_separate )
    {      
        struct separated ;
        
        separated.second = number_to_separate % 100 ;
        separated.first = number_to_separate / 100 ; 
    
        return separated ;
    } 
    
    int main()
    {  
        int number ;
        struct separated sep ;
        scanf( "%d", &number ) ;
        
        sep = separator( number ) ;
        printf("%d, %d", sep.first, sep.second ) ; 
    
        system("PAUSE");	
        return 0; 
    }

    2) Return the values via reference parameters

    Code:
    void separated separator( int number_to_separate, int first, int second )
    {      
        *second = number_to_separate % 100 ;
        *first = number_to_separate / 100 ; 
    } 
    
    int main()
    {  
        int number ;
        int first ;
        int second ;
    
        scanf( "%d", &number ) ;
        
        separator( number, &first, &second ) ;
        printf("%d, %d", first, second ) ; 
    
        system("PAUSE");	
        return 0; 
    }
    Note that your original code modified number_to_separate which is both unnecessary and bad practice. In a long and complex function it is useful to be able to assume that a parameter passed to the function does not change value! In your case having modified it all you did was assign it to another variable - why not assign the variable directly?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Typographical error:
    Originally Posted by clifford
    2) Return the values via reference parameters

    Code:
    void separated separator( int number_to_separate, int first, int second )
    {      
        *second = number_to_separate % 100 ;
        *first = number_to_separate / 100 ; 
    }
    Should be:
    Code:
    void separated separator( int number_to_separate, int *first, int *second )
    Just for OP's sake.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    26
    Rep Power
    0
    thank you all for your kind replies.it were very useful for me

IMN logo majestic logo threadwatch logo seochat tools logo