• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need some help with methods  RSS feed

 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there!

Cant figure out how the return statement in method2 works. Should´nt it be something like : return (sum + "_greetings"); I know that the other method1 calculate it but how on earth does only the "n" result in 170 ? n = 10? n*k = 170..?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:I know that the other method1 calculate it but how on earth does only the "n" result in 170 ? n = 10? n*k = 170..?


The "n" local variable of the method1() call is different from the "n" local variable of the method2() call. You do know why right?

Henry
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now when you say it I realise that its a different "n" in the method2. Got confused by the indentical variblename. But I still cant se how (if we name the other int in method2 "x") that "x" is equal to n*k..
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that class it is equal, because you are passing n * k as a parameter. If you call that method differently, then the n in that method would have a different value.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:In that class it is equal, because you are passing n * k as a parameter. If you call that method differently, then the n in that method would have a different value.



I understand what you are saying. I just does not feel straigt logical like other things do. Maybe my eyes will be clearer by time? Or is it just like java is built this way and doesn´t always seem straight logical? If we talk Java/coding in general?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be the same in any language which supports that sort of method/function/procedure calls.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!