|
| 1 | +# Python functions |
| 2 | + |
| 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 | + |
| 5 | +```python |
| 6 | +def greet(): |
| 7 | + name = input("Enter your name: ") |
| 8 | + print(f"Hello, {name}!") |
| 9 | + |
| 10 | + |
| 11 | +greet() |
| 12 | +``` |
| 13 | + |
| 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. |
| 20 | +
|
| 21 | + |
| 22 | +## The `return` keyword |
| 23 | + |
| 24 | +By default, functions return `None`. |
| 25 | + |
| 26 | +Example: |
| 27 | + |
| 28 | +```python |
| 29 | +def greet(): |
| 30 | + pass # Do nothing |
| 31 | + |
| 32 | + |
| 33 | +# print the function's return value |
| 34 | +print(greet()) # None |
| 35 | +``` |
| 36 | + |
| 37 | +- `pass` is a keyword which means "do nothing". It's necessary since a function needs a body. |
| 38 | +- you can control what a function returns using the `return` keyword. |
| 39 | + |
| 40 | +Example: |
| 41 | + |
| 42 | +```python |
| 43 | +def greet_user(): |
| 44 | + name = "John Doe" |
| 45 | + return f"Hello, {name}!" |
| 46 | + |
| 47 | +greeting = greet() |
| 48 | +print(greeting) # John Doe |
| 49 | +``` |
| 50 | + |
| 51 | +A function can return any value. |
| 52 | + |
| 53 | +## Arguments and parameters |
| 54 | + |
| 55 | +Functions can accept values from the caller, and return a value in their place. |
| 56 | + |
| 57 | +```py |
| 58 | +def greet(name): |
| 59 | + return f"Hello, {name} |
| 60 | + |
| 61 | +greeting = greet("Rolf Smith") |
| 62 | +print(greeting) # Hello, Rolf Smith |
| 63 | +``` |
| <
10000
td data-grid-cell-id="diff-6336524ef7e56b98bb0e8fef1742074cda73c914b114ccb7114d4e6a1d75ce3a-empty-64-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">64
+ |
| 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. |
| 67 | +
|
| 68 | +## Number of parameters |
| 69 | + |
| 70 | +A function can have any number of parameters. |
| 71 | + |
| 72 | +For example: |
| 73 | + |
| 74 | +```python |
| 75 | +def greet(name, age): |
| 76 | + return f"{name} is {age} years old!" |
| 77 | +``` |
| 78 | + |
| 79 | +## Named arguments |
| 80 | + |
| 81 | +If you want to be specific about what arguments you pass to parameters, use named arguments. |
| 82 | + |
| 83 | +Example: |
| 84 | + |
| 85 | +```python |
| 86 | +def greet(name, age): |
| 87 | + return f"{name} is {age} years old!" |
| 88 | + |
| 89 | +print(greet(name="John Doe", age=31)) |
| 90 | +``` |
| 91 | + |
| 92 | +## Default parameter values |
| 93 | + |
| 94 | +Parameters can have default values: |
| 95 | + |
| 96 | +```python |
| 97 | +def greet(name="John Doe"): |
| 98 | + return f"Hello, {name}!" |
| 99 | + |
| 100 | + |
| 101 | +print(greet()) # Hello, John Doe! |
| 102 | +print(greet("Rolf Smith")) # Hello, Rolf Smith |
| 103 | +``` |
| 104 | + |
| 105 | +When passing an argument to the function, the default parameter value will be overwritten by the given argument. |
| 106 | + |
| 107 | +If the function has a mix of normal and default parameters, the default parameters have to be passed last. |
| 108 | + |
| 109 | +Correct: |
| 110 | + |
| 111 | +```python |
| 112 | +def greet(age, name="John Doe"): |
| 113 | + return f"{name} is {age} years old!" |
| 114 | + |
| 115 | + |
| 116 | +print(greet(32)) |
| 117 | +``` |
| 118 | + |
| 119 | +Incorrect: |
| 120 | + |
| 121 | +```python |
| 122 | +def greet(name="John Doe", age): |
| 123 | + return f"{name} is {age} years old!" |
| 124 | + |
| 125 | + |
| 126 | +print(greet(32)) |
| 127 | +```
9E7A
div> |
| 128 | + |
| 129 | +Read more about Python functions [here](https://www.teclado.com/30-days-of-python/python-30-day-12-functions). |
| 130 | + |
| 131 | +# Python Lambda Expressions |
| 132 | + |
| 133 | +- Lambda expressions are an alternative syntax for defining simple functions. |
| 134 | + |
| 135 | +Example: `lambda name: f"Hello, {name}"` |
| 136 | + |
| 137 | +- No name |
| 138 | +- Can be assigned to a variable |
| 139 | +- Can be difficult to read |
| 140 | + |
| 141 | +Calling a lambda inline requires multiple pairs of brackets: |
| 142 | + |
| 143 | +```python |
| 144 | +result = (lambda x, y: x + y)(15, 3) |
| 145 | +``` |
| 146 | + |
| 147 | +# First Class Functions |
| 148 | + |
| 149 | +- Functions in Python are "first class citizens". |
| 150 | +- They can be stored in data structures, passed as arguments, and stored in variables. |
| 151 | + |
| 152 | +Example: |
| 153 | + |
| 154 | +```python |
| 155 | +def avg(*args): |
| 156 | + return sum(args) / len(args) |
| 157 | + |
| 158 | + |
| 159 | +operations = { |
| 160 | + "average": avg, |
| 161 | + "total": sum, |
| 162 | + "top": max |
| 163 | +} |
| 164 | + |
| 165 | +# We can also use associations to get a function from the dictionary. |
| 166 | +selection = "average" |
| 167 | +operation = operations[selection] |
| 168 | +operation() # This is acts the same as simply calling avg() |
| 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