summaryrefslogtreecommitdiffhomepage
path: root/julia.html.markdown
diff options
context:
space:
mode:
authorLeah Hanson <astrieanna@gmail.com>2013-07-01 17:58:25 -0400
committerLeah Hanson <astrieanna@gmail.com>2013-07-01 17:58:25 -0400
commit3ebd8c55fd018aec6381ef69cca9c9f55f0e4796 (patch)
treeec537fb2013b64fd2881f00a6f1677f61787eda9 /julia.html.markdown
parent43129d86e1d94c7fec5806b37d1f5522992c25b8 (diff)
edited functions through keyword args
Diffstat (limited to 'julia.html.markdown')
-rw-r--r--julia.html.markdown42
1 files changed, 28 insertions, 14 deletions
diff --git a/julia.html.markdown b/julia.html.markdown
index f26694d7..c6e54f70 100644
--- a/julia.html.markdown
+++ b/julia.html.markdown
@@ -298,31 +298,45 @@ end
## 4. Functions
####################################################
-# Use def to create new functions
-def add(x, y):
- print "x is %s and y is %s" % (x, y)
- return x + y # Return values with a return statement
+# Use the keyword function to create new functions
+function add(x, y)
+ println("x is $x and y is $y")
+ 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"
-# 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
# positional arguments
-def varargs(*args):
+function varargs(args...)
return args
+end
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
+
+defaults('h','g') #=> "h g and 5 6"
+defaults('h','g','j') #=> "h g and j 6"
+defaults('h','g','j','k') #=> "h g and j k"
+defaults('h') #=> ERROR: no method defaults(Char,)
+defaults() #=> ERROR: no methods defaults()
-# You can define functions that take a variable number of
-# keyword arguments, as well
-def keyword_args(**kwargs):
- return kwargs
+# You can define functions that take keyword arguments
+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]
-# Let's call it to see what happens
-keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}
+####
+#### In progress point
+####
# You can do both at once, if you like
def all_the_args(*args, **kwargs):