In the previous lesson we were working with functions that were written by someone else - they are already built-in in Python.
from math import pi
print(pi)
Today we will learn how to code our own functions which will be helpful when we need to run tasks repeatedly.
It's not hard:
def find_perimeter( width , height ):
"Returns the rectangle's perimeter of the given sides"
return 2 * (width + height)
print ( find_perimeter(4 , 2))
How does it work?
You define a function with the command def
. Right after that
you have to write name of the function, parentheses (which may contain
arguments) and then, of course, a colon.
We have already said that after a colon, everything that
belongs to (in our case) the function must be indented.
The indented code is called function body and it contains
the commands that the function performs.
In the body you can use various commands including if
, loop
, etc.
The body can start with a documentation comment which describes what the function is doing.
A function can return a value with the return
command.
def print_score(name, score):
print (name, 'score is', score)
if score > 1000:
print('World record!')
elif score > 100:
print('Perfect!')
elif score > 10:
print('Passable.')
elif score > 1:
print('At least something. ')
else:
print('Maybe next time. ')
print_score('Your', 256)
print_score('Denis', 5)
When you call a function, the arguments you write in parentheses
are assigned to the corresponding variables in the function definition's
parentheses.
So when you call our new function with print_score('Your', 256)
,
imagine that, internally, it assigns the values like this:
name = 'Your'
score = 256
print (name, 'score is', score)
if score > 1000:
... #etc.
The return
command terminates the function and returns the calculated value
out of the function. You can use this command only in functions.
It behaves similar to the break
command that terminates loops.
def yes_or_no(question):
"Returns True or False, depending on the user's answers."
while True:
answer = input(question)
if answer == 'yes':
return True
elif answer == 'no':
return False
else:
print('What do you want!! Just type "Yes" or "No".')
if yes_or_no('Do you want to play a game?'):
print('OK, but you have to program it first.')
else:
print('That is sad.' )
Same as if
and break
, return
is a command, not a function.
That's why return
has no parentheses after it.
Try to write a function that returns the area of an ellipse with given dimensions. The formula is A = πab, where a and b are the lengths of the axes. Then call the function and print the result.
The last program could be also written like that:
from math import pi
def ellipse(a, b):
print('The area is', pi * a * b) # Caution, 'print' instead of 'return'!
ellipse(3, 5)
The program works this way, too. But it loses one of the main advantages
that functions have - when you want to use the value differently than to print
it.
A function that returns its result can be used as part of other calculations:
def elliptical_cylinder(a, b, hight):
return ellipse(a, b) * hight
print(elliptical_cylinder(3, 5, 3))
But if our ellipse function just printed the result, we wouldn't be able to calculate the area of elliptical cylinder this way.
The reason why return
is better than print
is that a function
can be re-used in many different situations. When we don't actually
want to know the intermediate results, we can't use functions with print
.
Using return
, we can re-use the same function, for example, in graphic games,
web applications, or even to control a robot.
It is similar with input: If I hardcoded input
into a function, I could use
it only in situations where there's a user with keyboard present.
That's why it's always better to pass arguments to a function, and call
input
outside of the function:
from math import pi
def ellipse(a, b):
"""This reusable function returns only the result - the ellipse's area with a and b axes"""
#This is only the calculation
return pi * a * b
#print and input are "outside" the reusable function!
x = input('Enter length of 1st axis: ')
y = input('Enter length of 2nd axis: ')
print('The ellipsis area is', ellipse(x, y),'cm2.')
There are of course exceptions: A function that directly generates
a text can be written with print
, or a function that processes text information.
But when the function calculates something it's better to not have
print
and input
inside it.
When the function does not end with an explicit return
,
the value that it returns is automatically None
.
None
is a value that is already "inside" Python (same as True
and False
).
It's literally "none, nothing".
def nothing():
"This function isn't doing anything."
print(nothing())
Congratulations! You can now define your own functions! Now we have to explain what local and global variables are.
A function can use variables from "outside":
pi = 3.1415926 # a variable defined outside the function
def circle_area(radius):
return pi * radius ** 2
print(circle_area(100))
But every variable and argument that is defined within the function body are brand new and they share nothing with "outside" code.
Variables that are defined inside a function body are local variables, because they work only locally inside the function. For example, the following won't work how you would expect:
x = 0 # Assign value to global variable x
def set_x(value):
x = value # Assign value to local variable x
set_x(40)
print(x)
Variables that are not local are global variables -
they exist throughout the whole program. But if a function defines
a local variable with the same name, this local variable will only
have the value that was assigned within the function.
Let's look at an example. Before you run the next program, try to guess how it will behave. Then run it, and if it did something different than you expected, try to explain why. There is a catch! :)
from math import pi
area = 0
a = 30
def ellipse_area(a, b):
area = pi * a * b # Assign value to 'area`
a = a + 3 # Assign value to 'a`
return area
print(ellipse_area(a, 20))
print(area)
print(a)
Now try to answer the following questions:
pi
local or global?area
local or global?a
local or global?b
local or global?If it seems confusing and complicated just avoid naming variables (and function's arguments) within a function the same as those outside.
{ "data": { "sessionMaterial": { "id": "session-material:2018/pyladies-en-prague:def:2", "title": "Custom functions", "html": "\n \n \n\n <h1>Functions</h1>\n<p>In the previous <a href=\"/2018/pyladies-en-prague/beginners/functions/\">lesson</a> \nwe were working with functions that were written by someone else - they are\nalready built-in in Python.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">pi</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">pi</span><span class=\"p\">)</span>\n</pre></div><p>Today we will learn how to code our own functions\nwhich will be helpful when we need to run tasks repeatedly.</p>\n<p>It's not hard:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">find_perimeter</span><span class=\"p\">(</span> <span class=\"n\">width</span> <span class=\"p\">,</span> <span class=\"n\">height</span> <span class=\"p\">):</span> \n <span class=\"s2\">"Returns the rectangle's perimeter of the given sides"</span> \n <span class=\"k\">return</span> <span class=\"mi\">2</span> <span class=\"o\">*</span> <span class=\"p\">(</span><span class=\"n\">width</span> <span class=\"o\">+</span> <span class=\"n\">height</span><span class=\"p\">)</span>\n\n<span class=\"k\">print</span> <span class=\"p\">(</span> <span class=\"n\">find_perimeter</span><span class=\"p\">(</span><span class=\"mi\">4</span> <span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">))</span>\n</pre></div><p>How does it work?</p>\n<p>You <em>define</em> a function with the command <code>def</code>. Right after that\nyou have to write name of the function, parentheses (which may contain \n<em>arguments</em>) and then, of course, a colon.</p>\n<p>We have already said that after a colon, everything that \nbelongs to (in our case) the function must be indented. \nThe indented code is called <em>function body</em> and it contains \nthe commands that the function performs. \nIn the body you can use various commands including <code>if</code>, <code>loop</code>, etc.</p>\n<p>The body can start with a <em>documentation comment</em> which describes what\nthe function is doing.</p>\n<p>A function can return a value with the <code>return</code> command.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">print_score</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"n\">score</span><span class=\"p\">):</span> \n <span class=\"k\">print</span> <span class=\"p\">(</span><span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"s1\">'score is'</span><span class=\"p\">,</span> <span class=\"n\">score</span><span class=\"p\">)</span> \n <span class=\"k\">if</span> <span class=\"n\">score</span> <span class=\"o\">></span> <span class=\"mi\">1000</span><span class=\"p\">:</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'World record!'</span><span class=\"p\">)</span> \n <span class=\"k\">elif</span> <span class=\"n\">score</span> <span class=\"o\">></span> <span class=\"mi\">100</span><span class=\"p\">:</span> \n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Perfect!'</span><span class=\"p\">)</span> \n <span class=\"k\">elif</span> <span class=\"n\">score</span> <span class=\"o\">></span> <span class=\"mi\">10</span><span class=\"p\">:</span> \n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Passable.'</span><span class=\"p\">)</span> \n <span class=\"k\">elif</span> <span class=\"n\">score</span> <span class=\"o\">></span> <span class=\"mi\">1</span><span class=\"p\">:</span> \n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'At least something. '</span><span class=\"p\">)</span> \n <span class=\"k\">else</span><span class=\"p\">:</span> \n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Maybe next time. '</span><span class=\"p\">)</span>\n\n<span class=\"n\">print_score</span><span class=\"p\">(</span><span class=\"s1\">'Your'</span><span class=\"p\">,</span> <span class=\"mi\">256</span><span class=\"p\">)</span> \n<span class=\"n\">print_score</span><span class=\"p\">(</span><span class=\"s1\">'Denis'</span><span class=\"p\">,</span> <span class=\"mi\">5</span><span class=\"p\">)</span>\n</pre></div><p>When you call a function, the arguments you write in parentheses\nare assigned to the corresponding variables in the function definition's\nparentheses.\nSo when you call our new function with <code>print_score('Your', 256)</code>,\nimagine that, internally, it assigns the values like this:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"s1\">'Your'</span>\n<span class=\"n\">score</span> <span class=\"o\">=</span> <span class=\"mi\">256</span>\n\n<span class=\"k\">print</span> <span class=\"p\">(</span><span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"s1\">'score is'</span><span class=\"p\">,</span> <span class=\"n\">score</span><span class=\"p\">)</span> \n<span class=\"k\">if</span> <span class=\"n\">score</span> <span class=\"o\">></span> <span class=\"mi\">1000</span><span class=\"p\">:</span>\n <span class=\"o\">...</span> <span class=\"c1\">#etc.</span>\n</pre></div><h2>Return</h2>\n<p>The <code>return</code> command <em>terminates</em> the function and returns the calculated value \nout of the function. You can use this command only in functions.</p>\n<p>It behaves similar to the <code>break</code> command that terminates loops.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">yes_or_no</span><span class=\"p\">(</span><span class=\"n\">question</span><span class=\"p\">):</span>\n <span class=\"s2\">"Returns True or False, depending on the user's answers."</span>\n <span class=\"k\">while</span> <span class=\"bp\">True</span><span class=\"p\">:</span>\n <span class=\"n\">answer</span> <span class=\"o\">=</span> <span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"n\">question</span><span class=\"p\">)</span>\n <span class=\"k\">if</span> <span class=\"n\">answer</span> <span class=\"o\">==</span> <span class=\"s1\">'yes'</span><span class=\"p\">:</span>\n <span class=\"k\">return</span> <span class=\"bp\">True</span>\n <span class=\"k\">elif</span> <span class=\"n\">answer</span> <span class=\"o\">==</span> <span class=\"s1\">'no'</span><span class=\"p\">:</span>\n <span class=\"k\">return</span> <span class=\"bp\">False</span>\n <span class=\"k\">else</span><span class=\"p\">:</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'What do you want!! Just type "Yes" or "No".'</span><span class=\"p\">)</span>\n\n<span class=\"k\">if</span> <span class=\"n\">yes_or_no</span><span class=\"p\">(</span><span class=\"s1\">'Do you want to play a game?'</span><span class=\"p\">):</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'OK, but you have to program it first.'</span><span class=\"p\">)</span>\n<span class=\"k\">else</span><span class=\"p\">:</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'That is sad.'</span> <span class=\"p\">)</span>\n</pre></div><div class=\"admonition note\"><p>Same as <code>if</code> and <code>break</code>, <code>return</code> is a <em>command</em>, not a function.\nThat's why <code>return</code> has no parentheses after it.</p>\n</div><p>Try to write a function that returns the area of an ellipse with given \ndimensions.\nThe formula is <var>A</var> = π<var>a</var><var>b</var>,\nwhere <var>a</var> and <var>b</var> are the lengths of the axes.\nThen call the function and print the result.</p>\n<div class=\"solution\" id=\"solution-0\">\n <h3>Řešení</h3>\n <div class=\"solution-cover\">\n <a href=\"/2018/pyladies-en-prague/beginners-en/def/index/solutions/0/\"><span class=\"link-text\">Ukázat řešení</span></a>\n </div>\n <div class=\"solution-body\" aria-hidden=\"true\">\n <div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">pi</span>\n\n<span class=\"k\">def</span> <span class=\"nf\">ellipse</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">):</span> \n <span class=\"k\">return</span> <span class=\"n\">pi</span> <span class=\"o\">*</span> <span class=\"n\">a</span> <span class=\"o\">*</span> <span class=\"n\">b</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'The ellipsis area with 3 cm and 5 cm axes length is'</span><span class=\"p\">,</span> <span class=\"n\">ellipse</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"mi\">5</span><span class=\"p\">),</span><span class=\"s1\">'cm2.'</span><span class=\"p\">)</span>\n</pre></div>\n </div>\n</div><h3>Return or print?</h3>\n<p>The last program could be also written like that:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">pi</span>\n\n<span class=\"k\">def</span> <span class=\"nf\">ellipse</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">):</span> \n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'The area is'</span><span class=\"p\">,</span> <span class=\"n\">pi</span> <span class=\"o\">*</span> <span class=\"n\">a</span> <span class=\"o\">*</span> <span class=\"n\">b</span><span class=\"p\">)</span> <span class=\"c1\"># Caution, 'print' instead of 'return'!</span>\n\n<span class=\"n\">ellipse</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"mi\">5</span><span class=\"p\">)</span>\n</pre></div><p>The program works this way, too. But it loses one of the main advantages\nthat functions have - when you want to use the value differently than to <code>print</code> it.</p>\n<p>A function that <em>returns</em> its result can be used as part of other calculations:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">elliptical_cylinder</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">,</span> <span class=\"n\">hight</span><span class=\"p\">):</span>\n <span class=\"k\">return</span> <span class=\"n\">ellipse</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">)</span> <span class=\"o\">*</span> <span class=\"n\">hight</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">elliptical_cylinder</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"mi\">5</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">))</span>\n</pre></div><p>But if our ellipse function just <em>printed</em> the result, we wouldn't be\nable to calculate the area of elliptical cylinder this way.</p>\n<p>The reason why <code>return</code> is better than <code>print</code> is that a function\ncan be re-used in many different situations. When we don't actually\nwant to know the intermediate results, we can't use functions with <code>print</code>.</p>\n<p>Using <code>return</code>, we can re-use the same function, for example, in graphic games, \nweb applications, or even to control a robot.</p>\n<p>It is similar with input: If I hardcoded <code>input</code> into a function, I could use\nit only in situations where there's a user with keyboard present.\nThat's why it's always better to pass arguments to a function, and call\n<code>input</code> outside of the function:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">pi</span>\n\n<span class=\"k\">def</span> <span class=\"nf\">ellipse</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">):</span> \n <span class=\"sd\">"""This reusable function returns only the result - the ellipse's area with a and b axes"""</span>\n <span class=\"c1\">#This is only the calculation</span>\n <span class=\"k\">return</span> <span class=\"n\">pi</span> <span class=\"o\">*</span> <span class=\"n\">a</span> <span class=\"o\">*</span> <span class=\"n\">b</span>\n\n<span class=\"c1\">#print and input are "outside" the reusable function!</span>\n<span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">'Enter length of 1st axis: '</span><span class=\"p\">)</span>\n<span class=\"n\">y</span> <span class=\"o\">=</span> <span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">'Enter length of 2nd axis: '</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'The ellipsis area is'</span><span class=\"p\">,</span> <span class=\"n\">ellipse</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">y</span><span class=\"p\">),</span><span class=\"s1\">'cm2.'</span><span class=\"p\">)</span>\n</pre></div><p>There are of course exceptions: A function that directly generates \na text can be written with <code>print</code>, or a function that processes text information.\nBut when the function calculates something it's better to not have\n<code>print</code> and <code>input</code> inside it.</p>\n<h2>None</h2>\n<p>When the function does not end with an explicit <code>return</code>,\nthe value that it returns is automatically <code>None</code>.</p>\n<p><code>None</code> is a value that is already "inside" Python (same as <code>True</code> and <code>False</code>).\nIt's literally "none, nothing".</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">nothing</span><span class=\"p\">():</span>\n <span class=\"s2\">"This function isn't doing anything."</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">nothing</span><span class=\"p\">())</span>\n</pre></div><h2>Local variables</h2>\n<p>Congratulations! You can now define your own functions!\nNow we have to explain what local and global variables are.</p>\n<p>A function can use variables from "outside":</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">pi</span> <span class=\"o\">=</span> <span class=\"mf\">3.1415926</span> <span class=\"c1\"># a variable defined outside the function</span>\n\n<span class=\"k\">def</span> <span class=\"nf\">circle_area</span><span class=\"p\">(</span><span class=\"n\">radius</span><span class=\"p\">):</span>\n <span class=\"k\">return</span> <span class=\"n\">pi</span> <span class=\"o\">*</span> <span class=\"n\">radius</span> <span class=\"o\">**</span> <span class=\"mi\">2</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">circle_area</span><span class=\"p\">(</span><span class=\"mi\">100</span><span class=\"p\">))</span>\n</pre></div><p>But every variable and argument that is defined within the function body are\n<em>brand new</em> and they share nothing with "outside" code.</p>\n<p>Variables that are defined inside a function body are <em>local variables</em>,\nbecause they work only locally inside the function.\nFor example, the following won't work how you would expect:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"mi\">0</span> <span class=\"c1\"># Assign value to global variable x</span>\n\n<span class=\"k\">def</span> <span class=\"nf\">set_x</span><span class=\"p\">(</span><span class=\"n\">value</span><span class=\"p\">):</span>\n <span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"n\">value</span> <span class=\"c1\"># Assign value to local variable x</span>\n\n<span class=\"n\">set_x</span><span class=\"p\">(</span><span class=\"mi\">40</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">)</span>\n</pre></div><p>Variables that are not local are <em>global variables</em> -\nthey exist throughout the whole program. But if a function defines\na local variable with the same name, this local variable will only<br>\nhave the value that was assigned within the function.</p>\n<p>Let's look at an example.\nBefore you run the next program, try to guess how it will behave.\nThen run it, and if it did something different than\nyou expected, try to explain why.\nThere is a catch! :)</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">pi</span>\n<span class=\"n\">area</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n<span class=\"n\">a</span> <span class=\"o\">=</span> <span class=\"mi\">30</span>\n\n<span class=\"k\">def</span> <span class=\"nf\">ellipse_area</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">):</span>\n <span class=\"n\">area</span> <span class=\"o\">=</span> <span class=\"n\">pi</span> <span class=\"o\">*</span> <span class=\"n\">a</span> <span class=\"o\">*</span> <span class=\"n\">b</span> <span class=\"c1\"># Assign value to 'area`</span>\n <span class=\"n\">a</span> <span class=\"o\">=</span> <span class=\"n\">a</span> <span class=\"o\">+</span> <span class=\"mi\">3</span> <span class=\"c1\"># Assign value to 'a`</span>\n <span class=\"k\">return</span> <span class=\"n\">area</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">ellipse_area</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"mi\">20</span><span class=\"p\">))</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">area</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">)</span>\n</pre></div><p>Now try to answer the following questions:</p>\n<ul>\n<li>Is the variable <code>pi</code> local or global?</li>\n<li>Is the variable <code>area</code> local or global?</li>\n<li>Is the variable <code>a</code> local or global?</li>\n<li>Is the variable <code>b</code> local or global?</li>\n</ul>\n<div class=\"solution\" id=\"solution-1\">\n <h3>Řešení</h3>\n <div class=\"solution-cover\">\n <a href=\"/2018/pyladies-en-prague/beginners-en/def/index/solutions/1/\"><span class=\"link-text\">Ukázat řešení</span></a>\n </div>\n <div class=\"solution-body\" aria-hidden=\"true\">\n <ul>\n<li><code>pi</code> is global - it's not defined within the function and it's\naccessible in the whole program.</li>\n<li><code>area</code> - Note there are two variables of that name! One is global\nant the other one is local inside the function <code>ellipse_area</code>.</li>\n<li><code>a</code> - Note there are also two variables of that name. This was that catch:\nWriting <code>a = a + 3</code> has no point. A value is assigned to the local\nvariable <code>a</code>, but the function ends right after that, and this <code>a</code> is no \nlonger available, it will never be used.</li>\n<li><code>b</code> is only local - it's an argument for the <code>ellipse_area</code> function. </li>\n</ul>\n </div>\n</div><p>If it seems confusing and complicated just avoid naming variables (and\nfunction's arguments) within a function the same as those outside.</p>\n\n\n " } } }