<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Functions</id>
	<title>Functions - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Functions"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Functions&amp;action=history"/>
	<updated>2026-05-14T01:20:24Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=Functions&amp;diff=9199&amp;oldid=prev</id>
		<title>Kristian Rother at 19:58, 5 February 2013</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Functions&amp;diff=9199&amp;oldid=prev"/>
		<updated>2013-02-05T19:58:48Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Cat|Python Commands}}&lt;br /&gt;
{{Python Links}}&lt;br /&gt;
&lt;br /&gt;
== Structuring code with functions ==&lt;br /&gt;
A function is an autonomous sub-program with local variables, input and output. In Python, a function definition must be followed by a code block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def calc_price(fruit,n):&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Returns the price of fruits.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
if fruit==&amp;#039;banana&amp;#039;:&lt;br /&gt;
return 0.75 * n&lt;br /&gt;
&lt;br /&gt;
# call the function&lt;br /&gt;
print calc_price(&amp;#039;banana&amp;#039;,10)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Parameters ==&lt;br /&gt;
Function parameters may have default values. In that case, they become optional.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not use lists or dictionaries as default values!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def calc_price(fruit,n=1):&lt;br /&gt;
…&lt;br /&gt;
&lt;br /&gt;
print calc_prices(&amp;#039;banana&amp;#039;)&lt;br /&gt;
print calc_prices(&amp;#039;banana&amp;#039;,100)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Return values ==&lt;br /&gt;
A function may return values to the program part that called it. More than one value is returned as a tuple. In any case, the return statement ends the functions execution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
return &amp;#039;banana&amp;#039;,0.75&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Good style for writing functions ==&lt;br /&gt;
* Each function should have one purpose only.&lt;br /&gt;
* A clear name starting with a verb.&lt;br /&gt;
* A documentation string (with triple quotes).&lt;br /&gt;
* One return type or one modified input object.&lt;br /&gt;
* Necessary parameters.&lt;br /&gt;
* Optional parameters.&lt;br /&gt;
* Do not use global variables in a function.&lt;br /&gt;
* Keep functions small (&amp;lt;100 lines).&lt;/div&gt;</summary>
		<author><name>Kristian Rother</name></author>
	</entry>
</feed>