• Post Reply Bookmark Topic Watch Topic
  • New Topic

Order of function calls  RSS feed

 
Bob Sherry
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I write the following statement in Java:

can I count on the function call of f occurring before the function call in g? I am fairly sure that you cannot in most languages include C and C++.

Bob
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C and C++ don't strictly define that sort of thing, but Java® fortunately does. I suspect C# also defines such things, but I am not sure. The details are in the Java® Language Specification (=JLS). You should find the JLS tells you the left operand is evaluated first.

That may not apply if either function starts a new thread or runs concurrently.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, maybe even asynchronous evaluation won't alter the left to right order of evaluation. You can't start g(x) before f(x) completes.
 
German Martinez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: The sample code should not depend on which function is executed first. f (x) and g (x) must return their values depending on their input parameter x and not on which of the two functions was executed first.
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
German Martinez wrote:f (x) and g (x) must return their values depending on their input parameter x and not on which of the two functions was executed first.

In general, this is not true. If both f() and g() are instance methods, and they both reference and/or modify one or more instance variables, then it is quite possible that the result is not commutative.
Also, if they reference/modify any static variables (even if either f or g or both are static) the order might be important.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, lets call them methods and not functions. I know Java8 is changing that, but in this case, I don't think that function is the right term.

German Martinez wrote:Note: The sample code should not depend on which function is executed first.

That's not always true. For e.g. If the first method modifies a shared variable that's read by the second method, then the order matters.

As Campbell pointed out,
Campbell Ritchie wrote: ... the JLS tells you the left operand is evaluated first.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe OP was using the word function in its mathematical sense, in which case it shouldn't have any side‑effects like that, and it fulfils the recommendation in the JLS link I posted yesterday:-
That JLS section wrote:It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!