1

Why this code is returning 500 and some garbage value? Why isnt the divide function working?

#include <stdio.h>

int divmul(int v1, int v2)
{
    int div,mul;
    div =v1/v2;
    mul=v1*v2;
    return div,mul;
}
main()
{
    int val1=50, val2=10;
    printf("%d %d\n", divmul(val1,val2));
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Akash
  • 54
  • 1
  • 3
  • 4
    _function divmul_ will only return a single `int`value and the garbadge value is coming out due to second `%d`. – stud3nt Feb 12 '20 at 10:56
  • 1
    Better question: Why this even compile? How can be this valid? `return div,mul;` ..and this do compile. Weird. – Eraklon Feb 12 '20 at 10:58
  • Function return a single value in your case , please check the link https://en.wikipedia.org/wiki/Return_statement – Nadeem Qasmi Feb 12 '20 at 11:01
  • 1
    @Eraklon It's not weird at all. `div,mul` is just [the C comma operator](https://port70.net/~nsz/c/c11/n1570.html#6.5.17). – Andrew Henle Feb 12 '20 at 11:02
  • I guess you came from python world!! – milevyo Feb 12 '20 at 11:03
  • 1
    I would consider a function like that to do just one operation at all, in particular: `int div(int v1, int v2)` and `int mul(int v1, int v2)` For changing more variables at once by the function check out [call by reference](https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm) – Odysseus Feb 12 '20 at 11:04
  • 1
    @Andrew Henle I started programing in C 6 years ago and never seen code like this. Good to know this can happen, but saying its not weird. I wonder how often people encounter this kind of code. Does this returning syntax can be useful at any scenario by the way? – Eraklon Feb 12 '20 at 11:05
  • 1
    @Eraklon there is only one syntax. `return` keyword is followed by 1 expression or not. And whatever matches the syntax for "expressions" can be used for `return` as well. Syntax definition does not care about "usefullness". – Gerhardh Feb 12 '20 at 11:35
  • The compiler should complain about missing parameter for format string. If not, the warning level should be increased. – Gerhardh Feb 12 '20 at 11:36
  • regarding: `main()` This will cause the compiler to output a warning message about the returned type not being `int`. When compiling, always enable the warnings, then fix those warnngs. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same results – user3629249 Feb 13 '20 at 06:04
  • regarding this syntax error: `return div,mul;` This results in the compiler outputting the message: *untitled2.c:8:15: warning: left-hand operand of comma expression has no effect [-Wunused-value]* – user3629249 Feb 13 '20 at 06:06
  • A called function can only return a single value (which can be a struct) so this statement: `printf("%d %d\n", divmul(val1,val2));` does not compiler as it has one too many output format conversion specifiers. – user3629249 Feb 13 '20 at 06:09

1 Answers1

7

A function can return only one object using the return statement.

In this return statement

return div,mul;

there is used an expression with the comma operator. Its value is the value of the right operand. So in fact as the expression div has no side effect then the return statement is equivalent to

return mul;

From the C Standard (6.5.17 Comma operator)

2 The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value. And the compiler will issue an error for the printf call because there are no enough arguments.

Either declare as the return type a structure as for example

struct Result
{
    int div;
    int mul;
};

struct Result divmul( int v1, int v2 )
{
    struct Result result = { v1 / v2, v1 * v2 };

    return result;
}

and then in main

int main( void )
{
    int val1 = 50, val2 = 10;

    struct Result result = divmul( val1, val2 );

    printf( "%d %d\n", result.div, result.mul );
}

Or return the result from the function parameters (the so-called output parameters)

void divmul( int *v1, int *v2 )
{
    int div = *v1 / *v2;
    int mul = *v1 * *v2;

    *v1 = div;
    *v2 = mul;
}

And in main

int main( void )
{
    int val1 = 50, val2 = 10;

    divmul( &val1, &val2 );

    printf( "%d %d\n", val1, val2 );
}

Pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335