From: Xavier Noria Date: 2007-02-19T23:20:37+09:00 Subject: Re: return statement On Feb 19, 2007, at 2:48 PM, Derek Teixeira wrote: > So you are saying that it basically saves the confusion of > elsif's and > it just makes more sense.. in your example you could have easily > written > > def is_positive?(n) > if n > 0 > answer= true > elsif > n<0 > answer= false > end > > Right? but that just looks sloppy and could really get confusing if i > had more lines of code.. am i getting it right? Right. Now that you have the idea, there are some details that depend on the programming language here. I expect that book to abstract them from the reader so I won't mention them. Just let me add that return is generally used to end the execution of a method and (normally) give some result back. That can happen anywhere, it is not required to be in the middle so to speak. Thus, this is valid: def square(n) s = n*n return s end > so basically when you define a method you are saying that when i > call > this method, i want this done with the thing next to it. So here it is > saying i want you to run all these checks on the number next to > "englishNumber"? then it runs each of those tests .. the if statements > and once one is true to that statement, then it stops there and prints > out the last line before end? When you call a method you say: "please, execute the code inside the method definition with the values I pass and return some result". In the example above, the method is "square" and it does a different thing if you call "square(3)" or "square(5)" because you pass a different number in the call, a "5" in the former, a "3" in the latter. In each call, the value of the variable "n" is the one in the call, and so the code is executed for that particular value of "n". As if the method definition was a template where you substitute "n" with some actual number in each call. OK, the _result_ of the computation is returned to who issued the call and its usage depends on the actual surrounding code. For example, here a = square(3) you take the result of "square(3)", which is 9, and assign it to "a". After that line "a" holds the number "9". You can chain calls, that was the point in your original code: b = square(square(3)) The result of "square(3)" becomes the value passed to the outer call to "square", and so it receives "9". In consequence, "b" will hold "81". Chained calls do not require chained methods to be equal, you chained "englishNumber" and "puts" in your code. -- fxn