edited functions through keyword args

This commit is contained in:
Leah Hanson 2013-07-01 17:58:25 -04:00
parent 43129d86e1
commit 3ebd8c55fd

View File

@ -298,31 +298,45 @@ end
## 4. Functions ## 4. Functions
#################################################### ####################################################
# Use def to create new functions # Use the keyword function to create new functions
def add(x, y): function add(x, y)
print "x is %s and y is %s" % (x, y) println("x is $x and y is $y")
return x + y # Return values with a return statement x + y # or equivalently: return x + y
end
# Calling functions with parameters
add(5, 6) #=> 11 and prints out "x is 5 and y is 6" add(5, 6) #=> 11 and prints out "x is 5 and y is 6"
# Another way to call functions is with keyword arguments
add(y=6, x=5) # Keyword arguments can arrive in any order.
# You can define functions that take a variable number of # You can define functions that take a variable number of
# positional arguments # positional arguments
def varargs(*args): function varargs(args...)
return args return args
end
varargs(1, 2, 3) #=> (1,2,3) varargs(1, 2, 3) #=> (1,2,3)
# You can define functions with optional positional arguments
function defaults(a,b,x=5,y=6)
return "$a $b and $x $y"
end
# You can define functions that take a variable number of defaults('h','g') #=> "h g and 5 6"
# keyword arguments, as well defaults('h','g','j') #=> "h g and j 6"
def keyword_args(**kwargs): defaults('h','g','j','k') #=> "h g and j k"
return kwargs defaults('h') #=> ERROR: no method defaults(Char,)
defaults() #=> ERROR: no methods defaults()
# Let's call it to see what happens # You can define functions that take keyword arguments
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"} function keyword_args(;k1=4,name2="hello") # note the ;
return ["k1"=>k1,"name2"=>name2]
end
keyword_args(name2="ness") #=> ["name2"=>"ness","k1"=>4]
keyword_args(k1="mine") #=> ["k1"=>"mine","name2"=>"hello"]
keyword_args() #=> ["name2"=>"hello","k2"=>4]
####
#### In progress point
####
# You can do both at once, if you like # You can do both at once, if you like
def all_the_args(*args, **kwargs): def all_the_args(*args, **kwargs):