0
Why is x =5 and not x =4?
def demo x=4, y=2 puts x**y end demo 5 Above demo is defined as x = 4 and y =2 why does the result is 25 (5*5) and not 16 (4*4)?
3 Antworten
+ 8
The definition of the function assumes that the function takes two arguments, with their default values at 4 and 2, respectively. So if you just run the function with no arguments specified, it will take 4 and 2.
Meanwhile, you call the function and give it only one parameter. So it assumes the second one at its default value. And so you have x=5 (specified by user), y=2 (default).
5**2 = 25
0
because you didn't define x as global so it just took the value you specified at last when calling function. that's it