<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.darenet.org/skins/common/feed.css?12"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Python en:Functions - Revision history</title>
		<link>http://wiki.darenet.org/index.php?title=Python_en:Functions&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.15.1</generator>
		<lastBuildDate>Thu, 21 May 2026 05:48:30 GMT</lastBuildDate>
		<item>
			<title>Admin:&amp;#32;1 revision</title>
			<link>http://wiki.darenet.org/index.php?title=Python_en:Functions&amp;diff=5540&amp;oldid=prev</link>
			<description>&lt;p&gt;1 revision&lt;/p&gt;

		&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
		&lt;col class='diff-marker' /&gt;
		&lt;col class='diff-content' /&gt;
		&lt;col class='diff-marker' /&gt;
		&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 02:52, 3 April 2010&lt;/td&gt;
		&lt;/tr&gt;
		&lt;!-- diff generator: internal 2026-05-21 04:58:14 --&gt;

&lt;!-- diff cache key wiki:diff:version:1.11a:oldid:5539:newid:5540 --&gt;
&lt;/table&gt;</description>
			<pubDate>Sat, 03 Apr 2010 02:52:48 GMT</pubDate>			<dc:creator>Admin</dc:creator>			<comments>http://wiki.darenet.org/Talk:Python_en:Functions</comments>		</item>
		<item>
			<title>80.101.53.60:&amp;#32;/* Summary */</title>
			<link>http://wiki.darenet.org/index.php?title=Python_en:Functions&amp;diff=5539&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Summary&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;div class=&amp;quot;python book&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Functions are reusable pieces of programs. They allow you to give a name to a block of statements and you can run that block using that name anywhere in your program and any number of times. This is known as ''calling'' the function. We have already used many built-in functions such as the &amp;lt;tt&amp;gt;len&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;range&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The function concept is probably ''the'' most important building block of any non-trivial software (in any programming language), so we will explore various aspects of functions in this chapter.&lt;br /&gt;
&lt;br /&gt;
Functions are '''def'''ined using the &amp;lt;tt&amp;gt;def&amp;lt;/tt&amp;gt; keyword. This is followed by an ''identifier'' name for the function followed by a pair of parentheses which may enclose some names of variables and the line ends with a colon. Next follows the block of statements that are part of this function. An example will show that this is actually very simple:&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: function1.py&lt;br /&gt;
&lt;br /&gt;
def sayHello():&lt;br /&gt;
    print('Hello World!') # block belonging to the function&lt;br /&gt;
# End of function&lt;br /&gt;
&lt;br /&gt;
sayHello() # call the function&lt;br /&gt;
sayHello() # call the function again&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python function1.py&lt;br /&gt;
    Hello World!&lt;br /&gt;
    Hello World!&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
We define a function called &amp;lt;tt&amp;gt;sayHello&amp;lt;/tt&amp;gt; using the syntax as explained above. This function takes no parameters and hence there are no variables declared in the parentheses. Parameters to functions are just input to the function so that we can pass in different values to it and get back corresponding results.&lt;br /&gt;
&lt;br /&gt;
Notice that we can call the same function twice which means we do not have to write the same code again.&lt;br /&gt;
&lt;br /&gt;
== Function Parameters ==&lt;br /&gt;
&lt;br /&gt;
A function can take parameters, which are values you supply to the function so that the function can ''do'' something utilising those values. These parameters are just like variables except that the values of these variables are defined when we call the function and are already assigned values when the function runs.&lt;br /&gt;
&lt;br /&gt;
Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the function, we supply the values in the same way.  Note the terminology used - the names given in the function definition are called ''parameters'' whereas the values you supply in the function call are called ''arguments''.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: func_param.py&lt;br /&gt;
&lt;br /&gt;
def printMax(a, b):&lt;br /&gt;
    if a &amp;gt; b:&lt;br /&gt;
        print(a, 'is maximum')&lt;br /&gt;
    elif a == b:&lt;br /&gt;
        print(a, 'is equal to', b)&lt;br /&gt;
    else:&lt;br /&gt;
        print(b, 'is maximum')&lt;br /&gt;
&lt;br /&gt;
printMax(3, 4) # directly give literal values&lt;br /&gt;
&lt;br /&gt;
x = 5&lt;br /&gt;
y = 7&lt;br /&gt;
&lt;br /&gt;
printMax(x, y) # give variables as arguments&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python func_param.py&lt;br /&gt;
    4 is maximum&lt;br /&gt;
    7 is maximum&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
Here, we define a function called &amp;lt;tt&amp;gt;printMax&amp;lt;/tt&amp;gt; where we take two parameters called &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt;.  We find out the greater number using a simple &amp;lt;tt&amp;gt;if..else&amp;lt;/tt&amp;gt; statement and then print the bigger number.&lt;br /&gt;
&lt;br /&gt;
In the first usage of &amp;lt;tt&amp;gt;printMax&amp;lt;/tt&amp;gt;, we directly supply the numbers i.e. arguments. In the second usage, we call the function using variables. &amp;lt;tt&amp;gt;printMax(x, y)&amp;lt;/tt&amp;gt; causes value of argument &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; to be assigned to parameter &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; and the value of argument &amp;lt;tt&amp;gt;y&amp;lt;/tt&amp;gt; assigned to parameter &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt;. The printMax function works the same in both the cases.&lt;br /&gt;
&lt;br /&gt;
== Local Variables ==&lt;br /&gt;
&lt;br /&gt;
When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function i.e. variable names are ''local'' to the function. This is called the ''scope'' of the variable. All variables have the scope of the block they are declared in starting from the point of definition of the name.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: func_local.py&lt;br /&gt;
&lt;br /&gt;
x = 50&lt;br /&gt;
&lt;br /&gt;
def func(x):&lt;br /&gt;
    print('x is', x)&lt;br /&gt;
    x = 2&lt;br /&gt;
    print('Changed local x to', x)&lt;br /&gt;
&lt;br /&gt;
func(x)&lt;br /&gt;
print('x is still', x)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python func_local.py&lt;br /&gt;
    x is 50&lt;br /&gt;
    Changed local x to 2&lt;br /&gt;
    x is still 50&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
In the function, the first time that we use the ''value'' of the name &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;, Python uses the value of the parameter declared in the function.&lt;br /&gt;
&lt;br /&gt;
Next, we assign the value &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt;. The name &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; is local to our function.  So, when we change the value of &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; in the function, the &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; defined in the main block remains unaffected.&lt;br /&gt;
&lt;br /&gt;
In the last &amp;lt;tt&amp;gt;print&amp;lt;/tt&amp;gt; function call, we display the value of &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; in the main block and confirm that it is actually unaffected.&lt;br /&gt;
&lt;br /&gt;
== Using The global Statement ==&lt;br /&gt;
&lt;br /&gt;
If you want to assign a value to a name defined at the top level of the program (i.e. not inside any kind of scope such as functions or classes), then you have to tell Python that the name is not local, but it is ''global''. We do this using the &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; statement. It is impossible to assign a value to a variable defined outside a function without the &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
You can use the values of such variables defined outside the function (assuming there is no variable with the same name within the function). However, this is not encouraged and should be avoided since it becomes unclear to the reader of the program as to where that variable's definition is. Using the &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; statement makes it amply clear that the variable is defined in an outermost block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: func_global.py&lt;br /&gt;
&lt;br /&gt;
x = 50&lt;br /&gt;
&lt;br /&gt;
def func():&lt;br /&gt;
    global x&lt;br /&gt;
&lt;br /&gt;
    print('x is', x)&lt;br /&gt;
    x = 2&lt;br /&gt;
    print('Changed global x to', x)&lt;br /&gt;
&lt;br /&gt;
func()&lt;br /&gt;
print('Value of x is', x)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python func_global.py&lt;br /&gt;
    x is 50&lt;br /&gt;
    Changed global x to 2&lt;br /&gt;
    Value of x is 2&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; statement is used to declare that &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; is a global variable - hence, when we assign a value to &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; inside the function, that change is reflected when we use the value of &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; in the main block.&lt;br /&gt;
&lt;br /&gt;
You can specify more than one global variable using the same &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; statement. For example, &amp;lt;tt&amp;gt;global x, y, z&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Using nonlocal statement ==&lt;br /&gt;
&lt;br /&gt;
We have seen how to access variables in the local and global scope above. There is another kind of scope called &amp;quot;nonlocal&amp;quot; scope which is in-between these two types of scopes. Nonlocal scopes are observed when you define functions inside functions.&lt;br /&gt;
&lt;br /&gt;
Since everything in Python is just executable code, you can define functions anywhere.&lt;br /&gt;
&lt;br /&gt;
Let's take an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: func_nonlocal.py&lt;br /&gt;
&lt;br /&gt;
def func_outer():&lt;br /&gt;
    x = 2&lt;br /&gt;
    print('x is', x)&lt;br /&gt;
&lt;br /&gt;
    def func_inner():&lt;br /&gt;
        nonlocal x&lt;br /&gt;
        x = 5&lt;br /&gt;
&lt;br /&gt;
    func_inner()&lt;br /&gt;
    print('Changed local x to', x)&lt;br /&gt;
&lt;br /&gt;
func_outer()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python func_nonlocal.py&lt;br /&gt;
    x is 2&lt;br /&gt;
    Changed local x to 5&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
When we are inside &amp;lt;tt&amp;gt;func_inner&amp;lt;/tt&amp;gt;, the 'x' defined in the first line of &amp;lt;tt&amp;gt;func_outer&amp;lt;/tt&amp;gt; is relatively neither in local scope nor in global scope. We declare that we are using this x by &amp;lt;tt&amp;gt;nonlocal x&amp;lt;/tt&amp;gt; and hence we get access to that variable.&lt;br /&gt;
&lt;br /&gt;
Try changing the &amp;lt;tt&amp;gt;nonlocal x&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;global x&amp;lt;/tt&amp;gt; and also by removing the statement itself and observe the difference in behavior in these two cases.&lt;br /&gt;
&lt;br /&gt;
== Default Argument Values ==&lt;br /&gt;
&lt;br /&gt;
For some functions, you may want to make some of its parameters as ''optional'' and use default values if the user does not want to provide values for such parameters. This is done with the help of default argument values. You can specify default argument values for parameters by following the parameter name in the function definition with the assignment operator (&amp;lt;tt&amp;gt;=&amp;lt;/tt&amp;gt;) followed by the default value.&lt;br /&gt;
&lt;br /&gt;
Note that the default argument value should be a constant. More precisely, the default argument value should be immutable - this is explained in detail in later chapters. For now, just remember this.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: func_default.py&lt;br /&gt;
&lt;br /&gt;
def say(message, times = 1):&lt;br /&gt;
    print(message * times)&lt;br /&gt;
&lt;br /&gt;
say('Hello')&lt;br /&gt;
say('World', 5)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python func_default.py&lt;br /&gt;
    Hello&lt;br /&gt;
    WorldWorldWorldWorldWorld&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
The function named &amp;lt;tt&amp;gt;say&amp;lt;/tt&amp;gt; is used to print a string as many times as specified. If we don't supply a value, then by default, the string is printed just once. We achieve this by specifying a default argument value of &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt; to the parameter &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
In the first usage of &amp;lt;tt&amp;gt;say&amp;lt;/tt&amp;gt;, we supply only the string and it prints the string once. In the second usage of &amp;lt;tt&amp;gt;say&amp;lt;/tt&amp;gt;, we supply both the string and an argument &amp;lt;tt&amp;gt;5&amp;lt;/tt&amp;gt; stating that we want to ''say'' the string message 5 times.&lt;br /&gt;
&lt;br /&gt;
; Important&lt;br /&gt;
: Only those parameters which are at the end of the parameter list can be given default argument values i.e. you cannot have a parameter with a default argument value before a parameter without a default argument value in the order of parameters declared in the function parameter list.&lt;br /&gt;
: This is because the values are assigned to the parameters by position. For example, &amp;lt;tt&amp;gt;def func(a, b=5)&amp;lt;/tt&amp;gt; is valid, but &amp;lt;tt&amp;gt;def func(a=5, b)&amp;lt;/tt&amp;gt; is ''not valid''.&lt;br /&gt;
&lt;br /&gt;
== Keyword Arguments ==&lt;br /&gt;
&lt;br /&gt;
If you have some functions with many parameters and you want to specify only some of them, then you can give values for such parameters by naming them - this is called ''keyword arguments'' - we use the name (keyword) instead of the position (which we have been using all along) to specify the arguments to the function.&lt;br /&gt;
&lt;br /&gt;
There are two ''advantages'' - one, using the function is easier since we do not need to worry about the order of the arguments. Two, we can give values to only those parameters which we want, provided that the other parameters have default argument values.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: func_key.py&lt;br /&gt;
&lt;br /&gt;
def func(a, b=5, c=10):&lt;br /&gt;
    print('a is', a, 'and b is', b, 'and c is', c)&lt;br /&gt;
&lt;br /&gt;
func(3, 7)&lt;br /&gt;
func(25, c=24)&lt;br /&gt;
func(c=50, a=100)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python func_key.py&lt;br /&gt;
    a is 3 and b is 7 and c is 10&lt;br /&gt;
    a is 25 and b is 5 and c is 24&lt;br /&gt;
    a is 100 and b is 5 and c is 50&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
The function named &amp;lt;tt&amp;gt;func&amp;lt;/tt&amp;gt; has one parameter without default argument values, followed by two parameters with default argument values.&lt;br /&gt;
&lt;br /&gt;
In the first usage, &amp;lt;tt&amp;gt;func(3, 7)&amp;lt;/tt&amp;gt;, the parameter &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; gets the value &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt;, the parameter &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt; gets the value &amp;lt;tt&amp;gt;7&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;c&amp;lt;/tt&amp;gt; gets the default value of &amp;lt;tt&amp;gt;10&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
In the second usage &amp;lt;tt&amp;gt;func(25, c=24)&amp;lt;/tt&amp;gt;, the variable &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; gets the value of 25 due to the position of the argument. Then, the parameter &amp;lt;tt&amp;gt;c&amp;lt;/tt&amp;gt; gets the value of &amp;lt;tt&amp;gt;24&amp;lt;/tt&amp;gt; due to naming i.e. keyword arguments. The variable &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt; gets the default value of &amp;lt;tt&amp;gt;5&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
In the third usage &amp;lt;tt&amp;gt;func(c=50, a=100)&amp;lt;/tt&amp;gt;, we use keyword arguments completely to specify the values. Notice, that we are specifying value for parameter &amp;lt;tt&amp;gt;c&amp;lt;/tt&amp;gt; before that for &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; even though &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; is defined before &amp;lt;tt&amp;gt;c&amp;lt;/tt&amp;gt; in the function definition.&lt;br /&gt;
&lt;br /&gt;
== VarArgs parameters ==&lt;br /&gt;
&lt;br /&gt;
; TODO&lt;br /&gt;
: Should I write about this in a later chapter since we haven't talked about lists and dictionaries yet?&lt;br /&gt;
&lt;br /&gt;
Sometimes you might want to define a function that can take ''any'' number of parameters, this can be achieved by using the stars:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: total.py&lt;br /&gt;
&lt;br /&gt;
def total(initial=5, *numbers, **keywords):&lt;br /&gt;
    count = initial&lt;br /&gt;
    for number in numbers:&lt;br /&gt;
        count += number&lt;br /&gt;
    for key in keywords:&lt;br /&gt;
        count += keywords[key]&lt;br /&gt;
    return count&lt;br /&gt;
&lt;br /&gt;
print(total(10, 1, 2, 3, vegetables=50, fruits=100))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python total.py&lt;br /&gt;
    166&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
When we declare a starred parameter such as &amp;lt;tt&amp;gt;*param&amp;lt;/tt&amp;gt;, then all the positional arguments from that point till the end are collected as a list called 'param'.&lt;br /&gt;
&lt;br /&gt;
Similarly, when we declare a double-starred parameter such as &amp;lt;tt&amp;gt;**param&amp;lt;/tt&amp;gt;, then all the keyword arguments from that point till the end are collected as a dictionary called 'param'.&lt;br /&gt;
&lt;br /&gt;
We will explore lists and dictionaries in a [[Python_en:Data Structures|later chapter]].&lt;br /&gt;
&lt;br /&gt;
== Keyword-only Parameters ==&lt;br /&gt;
&lt;br /&gt;
If we want to specify certain keyword parameters to be available as keyword-only and ''not'' as positional arguments, they can be declared after a starred parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: keyword_only.py&lt;br /&gt;
&lt;br /&gt;
def total(initial=5, *numbers, vegetables):&lt;br /&gt;
    count = initial&lt;br /&gt;
    for number in numbers:&lt;br /&gt;
        count += number&lt;br /&gt;
    count += vegetables&lt;br /&gt;
    return count&lt;br /&gt;
&lt;br /&gt;
print(total(10, 1, 2, 3, vegetables=50))&lt;br /&gt;
print(total(10, 1, 2, 3))&lt;br /&gt;
# Raises error because we have not supplied a default argument value for 'vegetables'&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python keyword_only.py&lt;br /&gt;
    66&lt;br /&gt;
    Traceback (most recent call last):&lt;br /&gt;
      File &amp;quot;test.py&amp;quot;, line 12, in &amp;lt;module&amp;gt;&lt;br /&gt;
    print(total(10, 1, 2, 3))&lt;br /&gt;
    TypeError: total() needs keyword-only argument vegetables&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
Declaring parameters after a starred parameter results in keyword-only arguments. If these arguments are not supplied a default value, then calls to the function will raise an error if the keyword argument is not supplied, as seen above.&lt;br /&gt;
&lt;br /&gt;
If you want to have keyword-only arguments but have no need for a starred parameter, then simply use an empty star without using any name such as &amp;lt;tt&amp;gt;def total(initial=5, *, vegetables)&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== The return Statement ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;return&amp;lt;/tt&amp;gt; statement is used to ''return'' from a function i.e. break out of the function. We can optionally ''return a value'' from the function as well.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: func_return.py&lt;br /&gt;
&lt;br /&gt;
def maximum(x, y):&lt;br /&gt;
    if x &amp;gt; y:&lt;br /&gt;
        return x&lt;br /&gt;
    else:&lt;br /&gt;
        return y&lt;br /&gt;
&lt;br /&gt;
print(maximum(2, 3))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python func_return.py&lt;br /&gt;
    3&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;maximum&amp;lt;/tt&amp;gt; function returns the maximum of the parameters, in this case the numbers supplied to the function. It uses a simple &amp;lt;tt&amp;gt;if..else&amp;lt;/tt&amp;gt; statement to find the greater value and then ''returns'' that value.&lt;br /&gt;
&lt;br /&gt;
Note that a &amp;lt;tt&amp;gt;return&amp;lt;/tt&amp;gt; statement without a value is equivalent to &amp;lt;tt&amp;gt;return None&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;None&amp;lt;/tt&amp;gt; is a special type in Python that represents nothingness. For example, it is used to indicate that a variable has no value if it has a value of &amp;lt;tt&amp;gt;None&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every function implicitly contains a &amp;lt;tt&amp;gt;return None&amp;lt;/tt&amp;gt; statement at the end unless you have written your own &amp;lt;tt&amp;gt;return&amp;lt;/tt&amp;gt; statement. You can see this by running &amp;lt;tt&amp;gt;print(someFunction())&amp;lt;/tt&amp;gt; where the function &amp;lt;tt&amp;gt;someFunction&amp;lt;/tt&amp;gt; does not use the &amp;lt;tt&amp;gt;return&amp;lt;/tt&amp;gt; statement such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def someFunction():&lt;br /&gt;
    pass&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;pass&amp;lt;/tt&amp;gt; statement is used in Python to indicate an empty block of statements.&lt;br /&gt;
&lt;br /&gt;
; Note&lt;br /&gt;
: There is a built-in function called &amp;lt;tt&amp;gt;max&amp;lt;/tt&amp;gt; that already implements the 'find maximum' functionality, so use this built-in function whenever possible.&lt;br /&gt;
&lt;br /&gt;
== DocStrings ==&lt;br /&gt;
&lt;br /&gt;
Python has a nifty feature called ''documentation strings'', usually referred to by its shorter name ''docstrings''. DocStrings are an important tool that you should make use of since it helps to document the program better and makes it easier to understand. Amazingly, we can even get the docstring back from, say a function, when the program is actually running!&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# Filename: func_doc.py&lt;br /&gt;
&lt;br /&gt;
def printMax(x, y):&lt;br /&gt;
    '''Prints the maximum of two numbers.&lt;br /&gt;
&lt;br /&gt;
    The two values must be integers.'''&lt;br /&gt;
    x = int(x) # convert to integers, if possible&lt;br /&gt;
    y = int(y)&lt;br /&gt;
&lt;br /&gt;
    if x &amp;gt; y:&lt;br /&gt;
        print(x, 'is maximum')&lt;br /&gt;
    else:&lt;br /&gt;
        print(y, 'is maximum')&lt;br /&gt;
&lt;br /&gt;
printMax(3, 5)&lt;br /&gt;
print(printMax.__doc__)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
    $ python func_doc.py&lt;br /&gt;
    5 is maximum&lt;br /&gt;
    Prints the maximum of two numbers.&lt;br /&gt;
    &lt;br /&gt;
            The two values must be integers.&lt;br /&gt;
&lt;br /&gt;
How It Works:&lt;br /&gt;
&lt;br /&gt;
A string on the first logical line of a function is the ''docstring'' for that function. Note that DocStrings also apply to [[Python_en:Modules|modules]] and [[Python_en:Object Oriented Programming|classes]] which we will learn about in the respective chapters.&lt;br /&gt;
&lt;br /&gt;
The convention followed for a docstring is a multi-line string where the first line starts with a capital letter and ends with a dot. Then the second line is blank followed by any detailed explanation starting from the third line. You are ''strongly advised'' to follow this convention for all your docstrings for all your non-trivial functions.&lt;br /&gt;
&lt;br /&gt;
We can access the docstring of the &amp;lt;tt&amp;gt;printMax&amp;lt;/tt&amp;gt; function using the &amp;lt;tt&amp;gt;__doc__&amp;lt;/tt&amp;gt; (notice the ''double underscores'') attribute (name belonging to) of the function. Just remember that Python treats ''everything'' as an object and this includes functions.  We'll learn more about objects in the chapter on [[Python_en:Object Oriented Programming|classes]].&lt;br /&gt;
&lt;br /&gt;
If you have used &amp;lt;tt&amp;gt;help()&amp;lt;/tt&amp;gt; in Python, then you have already seen the usage of docstrings! What it does is just fetch the &amp;lt;tt&amp;gt;__doc__&amp;lt;/tt&amp;gt; attribute of that function and displays it in a neat manner for you. You can try it out on the function above - just include &amp;lt;tt&amp;gt;help(printMax)&amp;lt;/tt&amp;gt; in your program. Remember to press the &amp;lt;tt&amp;gt;q&amp;lt;/tt&amp;gt; key to exit &amp;lt;tt&amp;gt;help&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Automated tools can retrieve the documentation from your program in this manner. Therefore, I ''strongly recommend'' that you use docstrings for any non-trivial function that you write. The &amp;lt;tt&amp;gt;pydoc&amp;lt;/tt&amp;gt; command that comes with your Python distribution works similarly to &amp;lt;tt&amp;gt;help()&amp;lt;/tt&amp;gt; using docstrings.&lt;br /&gt;
&lt;br /&gt;
== Annotations ==&lt;br /&gt;
&lt;br /&gt;
Functions have another advanced feature called annotations which are a nifty way of attaching additional information for each of the parameters as well as the return value. Since the Python language itself does not interpret these annotations in any way (that functionality is left to third-party libraries to interpret in any way they want), we will skip this feature in our discussion. If you are interested to read about annotations, please see the [http://www.python.org/dev/peps/pep-3107/ Python Enhancement Proposal No. 3107].&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
&lt;br /&gt;
We have seen so many aspects of functions but note that we still haven't covered all aspects of them. However, we have already covered most of what you'll use regarding Python functions on an everyday basis.&lt;br /&gt;
&lt;br /&gt;
Next, we will see how to use as well as create Python modules.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;paging&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;span class=&amp;quot;prev&amp;quot;&amp;gt;[[Python_en:Control Flow|Previous]]&amp;lt;/span&amp;gt;&lt;br /&gt;
&amp;lt;span class=&amp;quot;next&amp;quot;&amp;gt;[[Python_en:Modules|Next]]&amp;lt;/span&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:A Byte of Python|Introduction]]&lt;/div&gt;</description>
			<pubDate>Tue, 31 Mar 2009 19:28:23 GMT</pubDate>			<dc:creator>80.101.53.60</dc:creator>			<comments>http://wiki.darenet.org/Talk:Python_en:Functions</comments>		</item>
	</channel>
</rss>