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 8 of 10.

Rohan said:   1 decade ago
Let a = 15 and b = 16.

Return (b = a+b, b);

Value return will be 31.

This may clear the left to right associativity of operator.

Jain said:   1 decade ago
return (kk, ll) is not possible as 2 values cannot be returned. So, the accumulator value is taken which is 12.

For both k and l it happens therefore the o/p is 1212. (just an assumption referring previous questions).

Pradeepa said:   1 decade ago
12 12 how it is possible please help me?

Abhi said:   1 decade ago
@Ravindra.

A sufficient one Ravindra :-).

Nikhil said:   1 decade ago
Function returns values even in following two cases.

1) return a, return b;

2) return (2, 4);

Pradeep said:   1 decade ago
If function return more then one value to only one L value then right argument will be taken,

Ex:
a=func();

And,
func{
-----------
-------
return (c,b);
}

Then a==b.

Prasanthi said:   1 decade ago
Explanation:

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

Tina said:   1 decade ago
I think the answer should be compile error. Because the function return two values at a time which is not possible.

Can anyone explain me the concept ?

Shakti singh said:   1 decade ago
LL is only returning values for both variables because KK is being overwritten by LL.

Shlok said:   1 decade ago
Had there been only "kk" being returned 'addmult()' would return only '7'
but since two different computations are being returned the greater value (i.e. 12)is overriding the smaller value(i.e. 7) and so the result of the both the computations stored in k and l respectively are turning out be 12.


Post your comments here:

Your comments will be displayed after verification.