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

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.

Sivamoorthy said:   1 decade ago
The return function to return only one value.
Return(x); is true.
Return (x,y) is false it error.

K.brahmateja said:   1 decade ago
In the return statement if there is more than one argument, it will the rightmost argument value will be sent to the calling function.

In this program we have return(kk,ll) the rightmost argument here is ll, in the definition ll is assigned to multiplication of the two variables only i.e " 3*4 =12".

Sharenu said:   1 decade ago
Thank you @sundar. But how to differentiate comma and comma opertor. Where are the comma operators necessary. Can you please explain me.

Pravu said:   1 decade ago
@Prashanthi.

It depend on whether you are changing the value in called function or not if you are catching value of I and j through pointer or address and if you are doing any operation in called function then sure the original value of I and j get changed.


Post your comments here:

Your comments will be displayed after verification.