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

Hardik vachhani said:   2 years ago
Step 1: int i=3, j=4, k, l; The variables i, j, k, l are declared as an integer type and variables 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".
(3)

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.
(3)

Sohom Chowdhury said:   5 years ago
In the given question, the comma(,) acts as an operator & the comma operator returns the rightmost operand in the expression and simply evaluates the rest of the operands and finally rejects them.
(2)

Bhanu said:   8 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".

Nandkishor said:   10 years ago
Note: In C ', ' act as an operator which returns the last value.

E.g: int i = (1, 2); this expression will store the value 2 in I.

Same is in return statement. The '(kk, ll)' acts as an expression which returns ll.

Value of ll = 12.

Hence the output: 12 12.

Ketaki said:   8 years ago
The answer is 12.

Abhishek said:   8 years ago
Here return (kk, ll); it will return only kk as RETURN only return one value i.e first one.

Rohan someshetty said:   8 years ago
Thanks for the explanation @Sundar.

Kamrul Hasan said:   9 years ago
Comma (,) Operator has LEFT to RIGHT associativity rules, so if we assign/return a VARIABLE a list of values with comma then it takes the RIGHT most value.

Ashish ranjan said:   9 years ago
In this program, there are two errors.

1. We cannot return two value at a function.

2. Declaration of the function is not available.

Because of this, it is compile time error.


Post your comments here:

Your comments will be displayed after verification.