1
1
# Python functions
2
2
3
- Defining a function:
3
+ Define a function using the ` def ` keyword. Once defined, it can be called (or executed) by using the function name and a pair of brackets.
4
4
5
5
``` python
6
6
def greet ():
7
7
name = input (" Enter your name: " )
8
8
print (f " Hello, { name} ! " )
9
- ```
10
-
11
- To call a function use the function's name followed by parentheses:
12
-
13
- ``` python
14
- # defining the function
15
- def greet ():
16
- name = input (" Enter your name: " )
17
- print (f " Hello, { name} ! " )
18
9
19
10
20
- # calling the function
21
11
greet()
22
12
```
23
13
24
- ## The ` def ` keyword
25
-
26
- A function is defined by 4 elements:
27
- - the ` def ` keyword
28
- - the name of the function
29
- - parentheses and
30
- - colon
31
-
32
- Syntax: ` def name(): `
33
-
34
- ## Indentation
35
-
36
- - To write code inside the function, you must increase the indentation level with one
37
- - When you reduce the indentation level, you exit the function
38
-
39
- Example:
40
-
41
- ``` python
42
- def greet ():
43
- name = input (" Enter your name: " ) # inside the function
44
- print (f " Hello, { name} ! " ) # inside the function
14
+ > Could show small callouts beside each section of the shown code sample.
15
+ > E.g. beside ` def greet() ` show "Defining the function name"
16
+ > Then beside the function body, show "Function body, will run when we execute the function".
17
+ > Then beside ` greet() ` , show "Executing the function starts the function body"
18
+ > Highlight the difference in indentation level with a callout.
19
+ > Signal that when we have less indentation, we exit the function.
45
20
46
- print (" Outside the function" )
47
-
48
- greet()
49
- ```
50
21
51
22
## The ` return ` keyword
52
23
@@ -59,56 +30,40 @@ def greet():
59
30
pass # Do nothing
60
31
61
32
62
- # print the function's return
33
+ # print the function's return value
63
34
print (greet()) # None
64
35
```
65
36
66
- - ` pass ` it's a keyword, and it means "do nothing"
37
+ - ` pass ` is a keyword which means "do nothing". It's necessary since a function needs a body.
67
38
- you can control what a function returns using the ` return ` keyword.
68
39
69
40
Example:
70
41
71
42
``` python
72
- def greet ():
43
+ def greet_user ():
73
44
name = " John Doe"
74
45
return f " Hello, { name} ! "
75
46
76
- # printing the function
77
- print (greet() ) # John Doe
47
+ greeting = greet()
48
+ print (greeting ) # John Doe
78
49
```
79
50
80
- A function can return anything .
51
+ A function can return any value .
81
52
82
- ## Parameters
53
+ ## Arguments and parameters
83
54
84
- A parameter is the variable defined inside the function's definition parentheses .
55
+ Functions can accept values from the caller, and return a value in their place .
85
56
86
- Example:
87
-
88
- ``` python
57
+ ``` py
89
58
def greet (name ):
90
- return f " Hello, { name} ! "
91
- ```
92
-
93
- You can have any number of parameters in a function.
59
+ return f " Hello, { name}
94
60
95
-
96
- ## Arguments
97
-
98
- - When a function is called, an argument is the value that is passed to it
99
- - To call a function that has a parameter, you have to provide an argument
100
-
101
- Example:
102
-
103
- ``` python
104
- def greet (name ):
105
- return f " Hello, { name} ! "
106
-
107
-
108
- print (greet(" John Doe" )) # Hello, John Doe!
61
+ greeting = greet(" Rolf Smith" )
62
+ print (greeting) # Hello, Rolf Smith
109
63
```
110
64
111
- The variable ` name ` will hold whatever it was passed inside the function's parentheses upon function call.
65
+ > Here we could have a diagram showing the flow of data using arrows.
66
+ > From the string "Rolf Smith" to the function, then through the return value to the ` greeting ` variable.
112
67
113
68
## Number of parameters
114
69
@@ -144,20 +99,11 @@ def greet(name="John Doe"):
144
99
145
100
146
101
print (greet()) # Hello, John Doe!
102
+ print (greet(" Rolf Smith" )) # Hello, Rolf Smith
147
103
```
148
104
149
105
When passing an argument to the function, the default parameter value will be overwritten by the given argument.
150
106
151
- For example:
152
-
153
- ``` python
154
- def greet (name = " John Doe" ):
155
- return f " Hello, { name} ! "
156
-
157
-
158
- print (greet(" Jimmy Boy" )) # Hello, Jimmy Boy!
159
- ```
160
-
161
107
If the function has a mix of normal and default parameters, the default parameters have to be passed last.
162
108
163
109
Correct:
@@ -186,29 +132,30 @@ Read more about Python functions [here](https://www.teclado.com/30-days-of-pytho
186
132
187
133
- Lambda expressions are an alternative syntax for defining simple functions.
188
134
189
- Syntax : ` lambda arguments: expression `
135
+ Example : ` lambda name: f"Hello, {name}" `
190
136
191
- - they don't have a name
192
- - they can be given a name by assigning them to a variable name
193
- - they're not so easy to read
137
+ - No name
138
+ - Can be assigned to a variable
139
+ - Can be difficult to read
194
140
195
- Example :
141
+ Calling a lambda inline requires multiple pairs of brackets :
196
142
197
143
``` python
198
144
result = (lambda x , y : x + y)(15 , 3 )
199
145
```
200
146
201
- Read more about lambda expressions and first-class functions [ here] ( https://www.teclado.com/30-days-of-python/python-30-day-16-lambda-expressions ) .
202
-
203
147
# First Class Functions
204
148
205
149
- Functions in Python are "first class citizens".
206
- - This means they are objects that can be stored in data structures and passed as arguments, and stored in variables.
150
+ - They can be stored in data structures, passed as arguments, and stored in variables.
207
151
208
152
Example:
209
153
210
154
``` python
211
- # Each of avg, sum, max are functions, and since they are First Class Citizens, they can be stored in this dictionary.
155
+ def avg (* args ):
156
+ return sum (args) / len (args)
157
+
158
+
212
159
operations = {
213
160
" average" : avg,
214
161
" total" : sum ,
@@ -220,3 +167,6 @@ selection = "average"
220
167
operation = operations[selection]
221
168
operation() # This is acts the same as simply calling avg()
222
169
```
170
+
171
+
172
+ Read more about lambda expressions and first-class functions [ here] ( https://www.teclado.com/30-days-of-python/python-30-day-16-lambda-expressions ) .
0 commit comments