In a word, it makes it possible to use all the processes together. I don't think this alone makes sense, so I'll actually write the code.
We want to define a method called hello to output hello.
def hello #When defining a method, start with def and end with end
puts "Hello"
end
hello #Method call
Hello#Hi defined is output at hello
The method returns the last line.
def number
"1"
"2"
"3"
end
puts number
3 #The last 3 is the output
def number
"1"
return "2"
"3"
end
puts number
2 #2 in the middle is output
Things that can pass values to methods etc. This allows you to bring in values outside of the defined method. (Values defined outside the basically defined method cannot be used inside the method, this range is called scope)
Formal arguments that are described and used for processing when defining a method The value that is actually assigned to the formal argument when calling the method is called the actual argument.
def method name(Formal argument)
#processing
end
Method name(Actual argument) #Method call
def add_number(number) #(2) Substitute the actual argument 5 for the formal argument number
puts number + number #③ Since the value of number is 5, 5+5 calculations are made.
end
add_number(5) #(1) Enter a value in the actual argument and call the defined method
Multiple arguments can be specified
def method name(First argument,Second argument)
#processing
end
Method name(First argument,Second argument)
that's all.
Recommended Posts