Gursheesh Singh Gursheesh Singh - 1 year ago 51
C Question

Using It's Own Call as One The Parameters in a Function call statement

Here Is my code:

#include <stdio.h>

int mul(int,int);

int main()
int sum,m,n;

int mul(int x,int y)
int sum;






Can someone tell me why I get 25 as output? Was the function called 2 times?
One during parameters and other time during sum?

Answer Source

It's perfectly simple:


You're calling mul() with 10 as the first argument, and the return value of mul(m, n) as the second argument.

m and n are 10 and 5, so mul(10, 5) returns 15. The statement in your main function then evaluates to this:

sum = mul(10, 15);

Which is 25.

TL;DR: yes, mul() is called twice. Once with m and n as arguments. The second time with the sum of m and n, adding 10

Using a debugger, or even looking at the assembler output generated by the compiler would've told you there were 2 successive calls to mul.
And yes, as others have rightfully pointed out: reading the help section (in particular how to ask) would be a good idea. It explains that you're expected to do the sensible debugging/diagnostic steps yourself. Only if that didn't solve the problem should you post a question here:

Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself.

You merely state that, given input X, you get output Y, and you don't know why.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download