The Ruby language makes it easy to create functions.
Function Syntax
Your function can compute values and store them in local variables that are specific to the function. Those values can then be returned with the return statement.
To call a function
Function Syntax
def functionname(variable)Examples
return <value>
end
Your function can compute values and store them in local variables that are specific to the function. Those values can then be returned with the return statement.
def say_hello(name)The return statement also can be shortened for very simple functions into a single line
var = “Hello, ” + name
return var
end
def say_hello(name)You can simplify the function further. The last expression that is evaluated is automatically returned by the method. For example:
return “Hello, ” + name
end
def say_hello(name)This would return the same value as the prior functions.
“Hello, ” + name
end
To call a function
function param1, param2or
function(param1,param2)Example
puts say_hello(“Geek”)
Ruby Function (method) Syntax
Reviewed by Pakainfo
on
August 08, 2018
Rating:
No comments: