Skip to content

Commit 7128232

Browse files
committed
Add functions basis
1 parent df585f3 commit 7128232

File tree

1 file changed

+249
-0
lines changed

1 file changed

+249
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
# Defining and calling functions
2+
3+
A function is a separate piece of code that can be used multiple times in different places in the program. So far, the use of functions, such as print, len, range, etc. has been shown several times. You did not have to write it yourself, because it was already done by the creators of Python. It was enough to know how they work. Most functions require external information - numbers, texts, other objects - and they are given as arguments to the function. Most also return some result, which, for example, can be assigned to a variable or used directly in an expression.
4+
5+
A function is a separate piece of code that can be used multiple times in different places in the program. So far, the use of functions, such as print, len, range, etc. has been shown several times. You did not have to write it yourself, because it was already done by the creators of Python. It was enough to know how they work. Most functions require external information - numbers, texts, other objects - and they are given as arguments to the function. Most also return some result, which, for example, can be assigned to a variable or used directly in an expression.
6+
7+
## Defining a function
8+
In every modern programming language it is possible to define functions independently. Without them, teamwork on even an average program would be almost impossible. New functions should be created whenever we are able to extract a code fragment that performs a certain, defined operation for certain data (and possibly returns a result). Also, when writing a program, you see the need to use an identical piece of code several times, you should put it in a separate function - thanks to this, the code only needs to be written once and you can easily use it multiple times.
9+
10+
We define functions using the **`def`** keyword:
11+
12+
```python
13+
def function_name (argument1, argument2 ...):
14+
block containing commands
15+
performed in a function
16+
```
17+
18+
The name of the function and the names of the arguments must follow exactly the same rules as the names of the variables. For example, below is the definition of a function that prints a string in a frame:
19+
20+
```python
21+
def print_int_frame (text):
22+
line = "+-" + "-" * len(text) + "-+"
23+
print(line)
24+
print("|", text, "|")
25+
print(line)
26+
```
27+
28+
Once a function has been defined, it is called with its name followed by parentheses containing arguments:
29+
30+
```python
31+
print_int_frame("Hello!")
32+
33+
print_int_frame("How are you?")
34+
35+
```
36+
**Note!** Even if the function takes no arguments, calling it requires giving the parentheses, even if nothing is in them:
37+
38+
```python
39+
def greet ():
40+
print ("How are you?")
41+
42+
greet()
43+
```
44+
45+
## Returning a value
46+
47+
Functions can also return values ​​as the result of their operation. The **`return`** keyword is used for this, which can only appear inside a function. After this keyword, we put the value to be returned. For example
48+
49+
```python
50+
def sum(number1, number2):
51+
return number1 + number2
52+
```
53+
54+
When calling such a function, the result returned by it can be used in any way, e.g. by assigning it to a variable or using it directly in an expression (it has already been demonstrated many times, e.g. using the `len` or `range` functions). In the case of the above function, it could be:
55+
56+
```python
57+
number1 = float(input("Enter the first number:"))
58+
number2 = float(input("Enter the second number:"))
59+
60+
print("The sum of these numbers is:", sum(number1, number2))
61+
```
62+
63+
Within a function, the **`return`** command can be anywhere, including a conditional block or inside a loop. In any case, it will cause the function to exit immediately: the execution of the loop will be interrupted and the commands below will not be executed. For example:
64+
65+
```python
66+
def quotient(dividend, divisor):
67+
if divisor == 0:
68+
return
69+
quotient = dividend / divisor
70+
return quotient
71+
```
72+
73+
The **`return`** command itself, with no return value given, will return `None`. The same happens when the function ends without the **`return`** command (as in the first example, the `print_in_frame` function). So, in Python, each function returns its result: defaulting to `None`.
74+
75+
The function can also return several results simultaneously. To do this, all results should be given after the return command, separated by a comma. For example
76+
77+
```python
78+
def sum_and_difference(number1, number2):
79+
sum = number1 + number2
80+
difference = number1 - number2
81+
return sum, difference
82+
```
83+
84+
When calling such a function, its result can be assigned to two variables, separated by a comma:
85+
86+
```python
87+
sum21, difference21 = sum_and_difference (2, 1)
88+
```
89+
90+
You can also assign the results of such a function to one variable or use it directly in an expression. In this case, it will be treated as a tuple containing a given number of elements:
91+
92+
```python
93+
result = sum_and_difference(5, 3) # result = (8, 2)
94+
print(type(result)) # tuple
95+
```
96+
97+
The type function used in the above example returns the type of the value given as its argument.
98+
99+
## Local and global variables
100+
101+
Variables defined outside of functions (as in all the previous lessons) are *global variables*. They are available throughout the program from the moment of their definition (value assignment). In some of the examples above, variables were also defined inside a function. These are local variables and they are shown <u>exclusively</u> inside the function, and after leaving they are destroyed. Function arguments are also treated as local variables (with values defined at the time the function was called).
102+
103+
If there are variables with the same names inside and outside the function, the values assigned to the local variable will not change the global variable:
104+
105+
If there are variables with the same names inside and outside the function, the values assigned to the local variable will not change the global variable:
106+
107+
```python
108+
def function ():
109+
variable = 2
110+
print ("Local variable:", variable ) # 2
111+
112+
variable = 1
113+
function()
114+
print ("Global variable:", variable ) # 1
115+
```
116+
117+
The two variables in the example above have the same name. However, they are completely independent of each other. There is no access to the global variable inside the function because it is "obscured" by a local variable with the same name. If we do not assign any value to the variable inside the function, then we can read and use the value of the global variable:
118+
119+
```python
120+
def function():
121+
print ("Global variable:", variable ) # 1
122+
123+
variable = 1
124+
function()
125+
```
126+
127+
**Attention!** For a variable to be treated as a local variable, it is enough that it is assigned a value anywhere inside the function. The following code won't work:
128+
129+
```python
130+
def function ():
131+
print ("Global variable:", variable ) # ERROR - local variable is not defined yet
132+
variable = 2
133+
134+
variable = 1
135+
function()
136+
```
137+
138+
It is possible to force the variables inside the function to be treated as global by a command `global` followed by a list of global variable names. Assigning values to them inside this function changes the global variable:
139+
140+
```python
141+
def function():
142+
global variable
143+
variable = 2
144+
print ("Global variable:", variable ) # 2
145+
146+
variable = 1
147+
function()
148+
print ("Global variable:", variable ) # 2
149+
```
150+
151+
**<u>An important rule!</u>**
152+
153+
Each extractable code fragment should be placed in a separate function. Also, make sure that functions that perform calculations do not print anything on the screen or change any global variables (generally avoid using a keyword `global`). When writing a function, we are often unable to predict how many times it will be performed. These can be thousands of times - in which case any message inside the function will be printed thousands of times. In general, parts of the program responsible for calculations and for interaction with the user (e.g. through functions `print` and `input`) should be clearly separated from each other. Thanks to this, in the future it will be possible and relatively simple to change the way of communication with the user, e.g. to a windowed version or via a website. <span style="background-color: #ffff99;">Under no circumstances you may use `input` in functions which purpose is different than directly reading from the keyboard All necessary data must be given as arguments to the function and the result returned with **`return`**.</span>
154+
155+
156+
## Calling a function
157+
The function is always called by giving its name followed by parentheses. The arguments of the function are placed inside these parentheses. Their number must be the same as the number of arguments given when defining the function (except for default arguments and functions with a variable number of arguments, which are described below).
158+
159+
There are two ways to pass arguments to a function: by position and by name. To explain this, suppose we have a defined function:
160+
161+
```python
162+
def function(a, b, c, d, e):
163+
function block
164+
```
165+
166+
It takes 5 arguments. In the basic method of calling a function, we give 5 values ​​in parentheses, separated from each other by commas:
167+
168+
```python
169+
function(1, 2, 3, 4, 5)
170+
```
171+
172+
Then the first argument (`a`) will be 1 for a given call, the second (`b`) will be 2, etc. When calling a function, its arguments can also be passed by name by giving `argument_name=value` (with a single `=` character as in the variable assignment command):
173+
174+
```python
175+
function(a=1, c=3, b=2, e=5, d=4)
176+
```
177+
178+
In this case, the order in which we gave the arguments does not matter. The argument `a` will be 1, `b`: 2, `c`: 3, `d`: 4, and `e`: 5. It is also possible to combine both of the above methods. Then we first pass any number of arguments by position, then arguments by name (no need to follow the order or arguments passed by name):
179+
180+
```python
181+
function(1, 2, d=4, c=3, e=5)
182+
```
183+
184+
In this case, the values ​​1 and 2 will be assigned to the arguments `a` and `b` (as they appear in the definition as the first and second arguments), and the remaining arguments are passed by name (that is `c`: 3, `d`: 4, and `e`: 5).
185+
186+
**Note!** Once we start supplying arguments using their names, we must not revert to positional specification on the same call. This will cause a syntax error.
187+
188+
### Default arguments
189+
It often happens that some arguments of a function have sensible defaults. Then, when defining the function, we can give these default values ​​using the sign `=`:
190+
191+
```python
192+
def function(a, b, c, d=0, e=None):
193+
function block
194+
```
195+
196+
The important thing is that all arguments with default values must be at the end of the argument list . This means that if a default value is given for any argument, each subsequent argument must also have a default value.
197+
198+
When calling a function defined in this way, we can omit these arguments. Then, when invoked, their default values will be adopted:
199+
200+
```python
201+
function(1, 2, 3)
202+
```
203+
204+
In the above call, the value of the argument `d` will be 0, and the argument `e`: `None`. Calling:
205+
206+
```python
207+
function(1, 2, 3, 4)
208+
```
209+
In this call, the argument `d` will be set to 4 and `e` will remain at its default value.
210+
211+
When using default arguments, it is very useful to pass arguments by name. Let's see the following example of a call:
212+
213+
```python
214+
function(1, 2, 3, e=5)
215+
```
216+
In this case, the argument `d` will keep its default value, and the argument `e` will take the value given in the function call (5). Python often uses functions with a very large number of arguments, most of which have default values. When calling such a function, you can easily change only the arguments you need.
217+
218+
**Attention!** Default argument values ​​are evaluated when the function is defined, not when it is invoked. For this reason, you should not use mutable types (lists and dictionaries) as default arguments. Instead of
219+
220+
```python
221+
def function(numbers=[1, 2, 3]):
222+
function body
223+
```
224+
you should write
225+
226+
```python
227+
def function(numbers=None):
228+
if numbers is None:
229+
numbers = [1, 2, 3]
230+
function body
231+
```
232+
## Recurrence
233+
A function can call itself to solve a problem. We call such a function a __recursive function__. Example calculating the factorial:
234+
235+
```python
236+
def factorial(n):
237+
if n == 1:
238+
return 1
239+
elif n > 1:
240+
return n * factorial (n-1)
241+
else:
242+
return None
243+
```
244+
For a recursive function to work properly, each subsequent call must be made for a different value of the argument controlling the the recurrence (in the example above it is `factorial(n-1)`). It must also be guaranteed to stop subsequent function calls when the value of this argument reaches a given value (in the example it will be for _n_ equal to 1).
245+
246+
247+
<hr />
248+
249+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license.

0 commit comments

Comments
 (0)