C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 12)
12.
What will be the output of the program?
#include<stdio.h>

int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}

int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d %d\n", k, l);
    return 0;
}
12 12
No error, No output
Error: Compile error
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
91 comments Page 2 of 10.

Yash said:   1 decade ago
@ Preethi

We can write multiple return statements in a function but it is not useful without conditional statements because at first return statement it returns next to where it is called. So in your program return(kk) always executes then it return the value of kk, the return(ll) statement never execute. So the output is 7, 7.

PRASHANT o_0 said:   1 decade ago
return (kk,ll);
here "," has left to right
so returning steps are
STEP 1:
return kk //here after getting a comma
STEP 2:
//discard kk
return ll
.
.
so finaly ll will be returned

Jay said:   1 decade ago
Thanks sundar.

Prudhvi said:   1 decade ago
I read some where a function can return only one value at a time. So now according to the above pgm has two return values. How come it executes the code?

Sundar said:   1 decade ago
Comma (,) operator:

The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

Commas can be used as separators in some contexts, such as function argument lists. Do not confuse the use of the comma as a separator with its use as an operator; the two uses are completely different.

Consider the expression

e1 , e2

The type and value of the expression are the type and value of e2; the result of evaluating e1 is discarded. The result is an l-value if the right operand is an l-value.

Where the comma is normally used as a separator (for example in actual arguments to functions or aggregate initializers), the comma operator and its operands must be enclosed in parentheses. For example:

func_one( x, y + 2, z );
func_two( (x--, y + 2), z );

In the function call to func_one above, three arguments, separated by commas, are passed: x, y + 2, and z. In the function call to func_two, parentheses force the compiler to interpret the first comma as the sequential-evaluation operator. This function call passes two arguments to func_two. The first argument is the result of the sequential-evaluation operation (x--, y + 2), which has the value and type of the expression y + 2; the second argument is z.

Example:

#include <stdio.h>
int main ()
{
int i = 10, b = 20, c= 30;
i = b, c;
printf("%i\n", i);

i = (b, c);
printf("%i\n", i);
}

//output:
20
30

Ajay debata said:   1 decade ago
return 1,2,3;
here the right most value is returned.

so 3 will be returned.

Thi said:   1 decade ago
@preethi:
After encountering a return statement the control of the function should be transferred to the next of the statement where it is called. So it ll always returns the value of the first return statement.

Sandy said:   1 decade ago
Step 1: int i=3, j=4, k, l; The variables i, j, k, l are declared as an integer type and variable i, j are initialized to 3, 4 respectively.

The function addmult(i, j); accept 2 integer parameters.

Step 2: k = addmult(i, j); becomes k = addmult(3, 4)

In the function addmult(). The variable kk, ll are declared as an integer type int kk, ll;

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll); It returns the value of variable ll only.

The value 12 is stored in variable 'k'.

Step 3: l = addmult(i, j); becomes l = addmult(3, 4)

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll); It returns the value of variable ll only.

The value 12 is stored in variable 'l'.

Step 4: printf("%d, %d\n", k, l); It prints the value of k and l

Hence the output is "12, 12".

Ravindra bagale said:   1 decade ago
Keep one thing in mind, comma operator means, the rightmost value only.

Ex. Return (1, 2) = 2.

Ex. i = j+ (1, 4, 7, 9, 12) then expression is i = j+12.

That's it.

Viren said:   1 decade ago
I had run this program in visual studio and its showing compilation error. I don't think that its output is 12, 12. If you think so, than please give an explanation.


Post your comments here:

Your comments will be displayed after verification.