A method is simply a sequence of instruction that the computer will execute from start to finish.
Eg:
a = 10
def shout( a )
puts "hello!"
a = 15
puts a
end
shout(a)
puts a
# hello!
# 15
# return nil
# 10
- The method will take one or more input and output something after the sequence of instruction.
- here, a is passed into the method shout , then assigned to a new variable 'a' and printed out as 15.
- the original local var outside is still 10.
- the method here has 3 lines of instruction. First one is output hello, second is assignment to number 15 to variable a , third one is output variable a.
- The last line in the method's instruction will be returned . If we print what the value of the method's returned , it is nil
puts shout(a)
# nil
Eg:
a = 10
def shout( a )
puts "hello!"
a = 15
puts a
end
shout(a)
puts a
# hello!
# 15
# return nil
# 10
- The method will take one or more input and output something after the sequence of instruction.
- here, a is passed into the method shout , then assigned to a new variable 'a' and printed out as 15.
- the original local var outside is still 10.
- the method here has 3 lines of instruction. First one is output hello, second is assignment to number 15 to variable a , third one is output variable a.
- The last line in the method's instruction will be returned . If we print what the value of the method's returned , it is nil
puts shout(a)
# nil
Comments
Post a Comment