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

Hari said:   1 decade ago
In above programme last return which value taken that value prints two times

Nidhinpradeep said:   1 decade ago
Function arguments are entered into a stack from left to right but they are popped out from right to left. So here return order is (12, 7). Since only one variable to receive 12 is received and 7 is discarded.

Ajay said:   1 decade ago
Answer is soo simple guys. Whenever we try to return two values through return the last value will only gets result tats why both the time ll value returns and get stored k an l. Simple as that.

Ankush said:   1 decade ago
@viren it also depends upon the type of compiler you are using, in gcc compiler it is running of < code blocks > as comma operator is working in it.

Gourab Paul said:   1 decade ago
int addmult(int ii, int jj)
{
int kk, ll;
kk = ii + jj;//ii=3,jj=4,kk=7
ll = ii * jj;//ii=3,jj=4,kk=12
return (kk, ll);//kk=7,ll=12 comma operator has right->left associativity.so the function 'll return value 12.
}

int main()
{
int i=3, j=4, k, l;
k = addmult(i, j);//addmult(3,4),k=12
l = addmult(i, j);same as previous,l=12
printf("%d %d\n", k, l);//k=12,l=12
return 0;

Sukannya said:   1 decade ago
A function can return only one value at a time. So, return(kk, ll) is invalid.

Sunil kumar said:   1 decade ago
In this prg we can get the value
k=(7,12) and l=(7,12).
In here the comma operator and brackets performs the right to lift.
So that way only it prints 12 and 12.

We can assign k = 7,12 and l = 7,12.
That time we can got 7 and 7 is the output in here it will working in left to right.

Sahana said:   1 decade ago
During assignment, comma operator process from right to left. While during return operations, it process from left to right.

Nipun kumari said:   1 decade ago
Since it is returning the value as ll=12 then please make me clear that is this value will be stored for both ll and kk both?

Prashanthi said:   1 decade ago
Hi.

Please help(Not regarding this problem).

When a return stmt is returned the control goes back from the called function to the calling function i.e(for example)here it goes to K = addmult(i,j).

My doubt is whether i, j values get preserved(remain 3,4) or do they change if we have done sth to the values of i, j in the called function?

Suppose after return statement we have sth to do with i, j. Please answer.


Post your comments here:

Your comments will be displayed after verification.