Like Tree1Likes
  • 1 Post By c99tutorial

Arguments and parameters help.

This is a discussion on Arguments and parameters help. within the C Programming forums, part of the General Programming Boards category; Is there anyway we can return 2 values from a called function example Code: float xxxx(float a, float b, float ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Arguments and parameters help.

    Is there anyway we can return 2 values from a called function
    example
    Code:
    float xxxx(float a, float b, float c, float d)
    {///
    ///
    ///
    }
    
    void xxx()
    {
    int e,f,g,h;
    ////
    ////
    xxx(e,f,g,h);
    }
    so if I want for example a+b and c+d, can i return those 2 answer? I don't think its possible since I am new into C programming

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    It's possible. You can pack as many return values as you want in a structure, for example.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,056
    Quote Originally Posted by AFCKING View Post
    Is there anyway we can return 2 values from a called function

    Code:
    float xxxx(float a, float b, float c, float d);
    A simple way is to declare some "out parameters" in your function signature. For example, you would change the above to the following

    Code:
    void xxxx(float a, float b, float c, float d, float *resultA, float *resultB) {
    // ... your code
    *resultA = ...;
    *resultB = ...;
    }
    Then you can call your function like this

    Code:
    float a,b;
    xxxx(1.2f, 3.4f, 5.6f, 7.8f, &a, &b);  // save results in a and b

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Don't use it myself, but couldn't something like below be used or have I forgotten how the mechanism works? I'm curious.

    Code:
    float* xxxx(float a, float b, float c, float d)
    {
        float *tmp = malloc(sizeof(float) * 2);
    
        if(tmp)
        { 
            *tmp = a + b;
            *(tmp + 1) = c + d;
            return tmp;
        }
    
        return NULL;
    }
    main

    Code:
    float *result = xxxx(1, 2, 3, 4);
    
    if(result)
    {
        printf("a+b = %0.0f, c+d = %0.0f", *result, *(result + 1));
        free(result);
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,056
    Quote Originally Posted by ronin View Post
    Don't use it myself, but couldn't something like below be used or have I forgotten how the mechanism works? I'm curious.

    Code:
    float* xxxx(float a, float b, float c, float d)
    {
        float *tmp = malloc(sizeof(float) * 2);
    There's nothing wrong with it, except that you might forget to free. Probably the most failure-tolerant way is the suggestion from whiteflags, to make a struct. This has the advantage that you can name what the parameters are for. For example, say you have function foo that returns the sum and the product. Then you could write the function like this

    Code:
    typedef struct FOO_RESULT FOO_RESULT;
    struct FOO_RESULT {
    	float sum;
    	float product;
    };
    
    FOO_RESULT foo(float a, float b, float c, float d) {
    	float x = a + b;
    	float y = c * d;
    	
    	return (FOO_RESULT) {
    		.sum = x,
    		.product = y,
    	};
    }
    The caller of the function can do so in a way that is "self-documenting" and with no chance of memory allocation violations

    Code:
    FOO_RESULT res = foo(1.2f, 3.4f, 5.6f, 7.8f);
    printf("Results are: %.2f, %.2f\n", res.sum, res.product);

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Thanks c99; it's been a long time since I've tried that method, and wasn't sure if I had it right. I'd go with the struct too, but pass it as a parameter.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command-line arguments or parameters to a program
    By c_lady in forum C Programming
    Replies: 2
    Last Post: 06-21-2010, 10:28 AM
  2. Sending fewer parameters then arguments
    By gar35 in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2010, 09:20 PM
  3. Explanation of arguments/parameters in Functions
    By hellogamesmaste in forum C Programming
    Replies: 8
    Last Post: 08-17-2009, 02:30 PM
  4. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  5. Parameters vs. Arguments
    By Sereby in forum C Programming
    Replies: 2
    Last Post: 07-27-2004, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21