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.

Lakshman said:   2 years ago
If a function defined the above main, no need of prototype.

There is a prototype, but no definition, That is also fine till you don't call that function.

Now coming to the problem:
Many are asked how return returns the multiple values. That is correct. Return can not return multiple values at a time. But we can achieve this by storing values in an array, finally we can return multiple values. But in the coding problem, here return like (7, 12). Please consider comma operator here.

We know precedence of comma operator is left to right. Because of that the last value only return i.e, ->12.
(4)

Sundar said:   1 decade ago
The Comma Operator (,):

The comma operator, written as a comma (,) takes two operands. It evaluates its left operand and discards the value. It then evaluates its right operand, and returns that value as the result of the expression.

7 + 3, 6 * 2 // Gives 12
3,14 // Gives 14 (and not pi)

The comma operator is seldom necessary, except in function-like preprocessor macros, which we advise against anyway.

The commas used to separate the arguments in a call to a method, or the elements in an array, are not comma operators.

Ciri said:   7 years ago
The comma operator evaluates a series of expressions. The value of the comma group is the value of the last element in the list.

int multi_return_args(void)
{
return (44,66);
}

In the example, you show the leading constant expression 44 has no effect, but if the expression had a side effect, it would occur.

For example:

return printf( "we're done" ), 66;

In this case, the program would print "we're done" and then return 66.

so, final answer is based on the last argument that is ll.

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;

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.

Preethi said:   1 decade ago

#include<stdio.h>

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;
}

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


If the program is rewritten as shown above the output is 7 7 how is it possible?

Can any one explain me please?

Mayur 22kar said:   4 years ago
We can not return more than one value in the function.

Add mult return the value.
return (kk, ll) ;.

In this step, the execution of bracket from left to right and comma (,) operator left to write execution then return value will be ll and the end of file expression then function return this value to calling location.

And answer will be 12, 12.

Pratik said:   1 decade ago
@ Preethi,,,,,we can return only one value in fuctions if we are not using pointer,,,,,,so when k==addmui(i,j); executes functions is called and and kk is returned fromreturn(kk),and recieved by k,now we cant return two statement so curser goes to main();
again l=admull(i,j);
again kk is retun boy time
hence you are feting to 7; ; ;ie7,7 or 77

Aman kumar mawandia said:   7 years ago
As when admult function is called it is gone to admult definition i.e

k=admult(3,4),in calling function.
Now in function def.
int addmult(3,4)
i.e kk=3+4;
ll=3*4;
return(kk,ll)i.e
return(7,12)i.e
return(12).

AS because return statement only one value i.e 12 from right side.
Same is also done when other function is call .i.e return 12.

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.


Post your comments here:

Your comments will be displayed after verification.