The following is a example of defining a function.
# -*- coding: utf-8 -*- # python def ff(x, y): """ff(x, y) returns x+y.""" return x+y print ff(3,4) # prints 7
A string immediately following the function definition is the function's documentation.
If a function definition does not have a “return” statement, it returns None.
To define unspecified number of positional parameters, use *‹xyz› as last item. Your function will receive it as a tuple.
# -*- coding: utf-8 -*- # python # function with unspecified number of arguments. use * before the name. def ff(*xx): # xx is received as a tuple. print "first arg:", xx[0] print "total args:", len(xx) return xx rr = ff(3, 4, 5) print rr # (3,4,5) print type(rr) # <type 'tuple'>
A function can have Named Parameters, called Keyword Argument in Python. If no argument is given, a default value is used. To define keyword parameters, use the form ‹name› = ‹value›.
# -*- coding: utf-8 -*- # python # Defining keyword parameters. # Keyword parameters are automatically optional, with default values. # They must come after positional parameters, if any def ff(x, y=1, z=2): return "x → %d, y → %d, z → %d" % (x, y, z) print ff(3) # x → 3, y → 1, z → 2 # optional parameter name can be omitted. If so, they go by order. print ff(3, 2) # x → 3, y → 2, z → 2 print ff(3, 5, 6) # x → 3, y → 5, z → 6 # keyword argument must come after positional arguments, if any print ff(3, y=2) # x → 3, y → 2, z → 2 print ff(3, z=2) # x → 3, y → 1, z → 2 print ff(3, z=8, y=9) # x → 3, y → 9, z → 8
You can define a function to receive Arbitrary number of Keyword Arguments.
You need to put a **‹name› as last item in your parameter list in definition.
Your function will receive it as a dictionary.
# -*- coding: utf-8 -*- # python # use 「**‹ddd›」 to receive arbitrary keyword arguments given by user def ff(**ddd): # ddd is a dictionary return ddd print ff( z = 8, c = 888, aa = 4, b = 33) # {'aa': 4, 'c': 888, 'z': 8, 'b': 33}
The **‹name› can be used with normal paramaters. But it must be the last.
# -*- coding: utf-8 -*- # python def ff(a, b=1, c=1, **xyz): # xyz is a a dictionary print "a is:", a print "b is:", b print "c is:", c print "xyz is:", xyz ff(3, rabbit = 7, cat = "a", tiger = 33, c = 8) # a is: 3 # b is: 1 # c is: 8 # xyz is: {'tiger': 33, 'rabbit': 7, 'cat': 'a'}
The positional parameters, and *‹tup› tuple and **‹dict› dictionary can be used together, but in this order.
# -*- coding: utf-8 -*- # python def ff(a, *mmm, **zzz): # mmm is a tuple # zzz is a dictionary print "a is:", a print "mmm is:", mmm print "zzz is:", zzz ff(3, 4, 5, 6, rabbit = 7, cat = 9, tiger = 33) # a is: 3 # mmm is: (4, 5, 6) # zzz is: {'tiger': 33, 'rabbit': 7, 'cat': 9}
The following is a example of defining a function.
Function name should start with lowercase.
# -*- coding: utf-8 -*- # ruby # function with no parameter def ff 4 # return 4 # return is optional. If no return, returns last expression end p ff # ⇒ 4
If a function definition does not have a “return” statement, it returns last expression. In Ruby, everything has a return value.
# -*- coding: utf-8 -*- # ruby # function with 2 parameters def gg (aa, bb) aa + bb end p gg 3, 4 # ⇒ 7
To define unspecified number of parameters, use *‹xyz› as last item. Your function will receive it as a array named ‹xyz›.
# -*- coding: utf-8 -*- # ruby # function with unspecified number of arguments. # Use *‹name› # Your function will receive a array of name ‹name› def ff(*xx) xx[0] # first arg xx[1] # second arg xx # return the array end p ff(3) # [3] p ff(3, 4, 5) # [3, 4, 5]
A function can have optional Parameters. When a function call doesn't supply enough arguments, default values are used.
Ruby doesn't have named parameters.
# -*- coding: utf-8 -*- # ruby # Defining optional parameters. def ff(x, y = 7, z = 8) [x, y, z] # returns a array of the arguments received end p ff(3) # [3, 7, 8] p ff(3, 2) # [3, 2, 8] p ff(3, 5, 6) # [3, 5, 6]
Here is a example of a function.
# -*- coding: utf-8 -*- # perl use Data::Dumper; $Data::Dumper::Indent = 0; # print in compact style # define a function sub ff { $a = $_[0]; # get first arg $b = $_[1]; # get second arg # arguments are automatically assigned to array @_ print Dumper(\@_); # prints the array @_ # use “return” to return value and exit function return $a + $b; } ff(3, 4, "rabbit"); # $VAR1 = [3,4,'rabbit'];
Note: Unlike most other languages, perl subroutine's parameters are usually not declared.
Arguments are automatically assigned to the array @_. So, $_[0] is the first element of the array @_. The @_ array is a predefined array.
To define a function with optional parameters, just use defined($_[n]) to check if the argument is given.
# -*- coding: utf-8 -*- # perl # myFun(x,y) returns x+y. y is optional and default to 1. sub myFun { $x = $_[0]; if (defined $_[1]) { $y = $_[1]; } else { $y = 1; } return $x+$y; } print myFun(3); # 4
For another example, see: Python & Perl: Defining A Function.
blog comments powered by Disqus