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;
}
Discussion:
91 comments Page 2 of 10.
Debayani said:
5 years ago
Hi, it is call by value not call by reference so the value should not get change, it should print 3 and 4. Am I right?
TDas said:
5 years ago
How it Returns two value at the same time?
Shubh said:
6 years ago
Case-1
return(10,20);
It will return only 20.
Case-2
return 10;
return 20;
It return first statement only.
return(10,20);
It will return only 20.
Case-2
return 10;
return 20;
It return first statement only.
Ashish mishra said:
6 years ago
Since ', ' comma operator is right-left associative right most value will be return at the returning time. This is the simplest explanation of this problem.
Thank you.
Thank you.
Dishank said:
6 years 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".
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".
Achal said:
7 years ago
Well here the concept of comma operator is used!.
The rightmost value in the return statement will be taken into consideration. Hence, it will be 12 each time.
The rightmost value in the return statement will be taken into consideration. Hence, it will be 12 each time.
Ayush sharma said:
7 years ago
It is not possible to return multiple values through return keywords. So I think it's a wrong answer.
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.
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.
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.
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.
Kutrapali said:
7 years ago
Why does it return value? I thought the variable "kk" is not equals to variable "k".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers