• Post Reply Bookmark Topic Watch Topic
  • New Topic

function calling in a program  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to ask that when we invoke a function by passing certain arguments in it ,so for instance I have certain variables like a,b and c with some values assigned to them,so when I invoke a function like func(a*b , 8, c+a) where the function func accepts parameters like func(int x,int y,int z) ,so is there any order of evaluation of expressions defined like the value of c+d would be evaluated first and then assigned to variable z ,because in C we have sequence points and since comma when acting like a separator is not a sequence point so any of the expression can be executed first ,so bascially is there any sequence in which the function arguments are assigned to the parameters of the function or any relation with the stack implementation.

Even in C ,the printf starts executing its expressions from right to left so basically in why does it happen there when comma is a separator in a function argument ,I am really confused in it.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To find the answer to such questions, you have to look in the Java Language Specification.

You can find the answer to this in paragraph 15.12.4.2 Evaluate Arguments:

The argument expressions, if any, are evaluated in order, from left to right. If the evaluation of any argument expression completes abruptly, then no part of any argument expression to its right appears to have been evaluated, and the method invocation completes abruptly for the same reason. The result of evaluating the j'th argument expression is the j'th argument value, for 1 ≤ j ≤ n. Evaluation then continues, using the argument values, as described below.

By the way, Java does not have functions like in C, they are called methods instead.

The Java Language Specification does not use the concept of sequence points as in the specification of the C programming language.

In C, the order of function arguments is not defined, which means that any C compiler might do it in any order, whatever is more efficient or more convenient. The fact that on your particular C compiler you see that the arguments to printf are evaluated from left to right does not mean that it works like that on all C compilers, or even on all printf calls with your own C compiler. In C there is simply no guarantee that the arguments are evaluated in any particular order.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!