|
| 1 | +# Python functions |
| 2 | + |
| 3 | +Defining a function: |
| 4 | + |
| 5 | +```python |
| 6 | +def greet(): |
| 7 | + name = input("Enter your name: ") |
| 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 | + |
| 19 | + |
| 20 | +# calling the function |
| 21 | +greet() |
| 22 | +``` |
| 23 | + |
| 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 |
| 45 | + |
| 46 | +print("Outside the function") |
| 47 | + |
| 48 | +greet() |
| 49 | +``` |
| 50 | + |
| 51 | +## The `return` keyword |
| 52 | + |
| 53 | +By default, functions return `None`. |
| 54 | + |
| 55 | +Example: |
| 56 | + |
| 57 | +```python |
| 58 | +def greet(): |
| 59 | + pass # Do nothing |
| 60 | + |
| 61 | + |
| 62 | +# print the function's return |
| 63 | +print(greet()) # None |
| 64 | +``` |
| 65 | + |
| 66 | +- `pass` it's a keyword, and it means "do nothing" |
| 67 | +- you can control what a function returns using the `return` keyword. |
| 68 | + |
| 69 | +Example: |
| 70 | + |
| 71 | +```python |
| 72 | +def greet(): |
| 73 | + name = "John Doe" |
| 74 | + return f"Hello, {name}!" |
| 75 | + |
| 76 | +# printing the function |
| 77 | +print(greet()) # John Doe |
| 78 | +``` |
| 79 | + |
| 80 | +A function can return anything. |
| 81 | + |
| 82 | +## Parameters |
| 83 | + |
| 84 | +A parameter is the variable defined inside the function's definition parentheses. |
| 85 | + |
| 86 | +Example: |
| 87 | + |
| 88 | +```python |
| 89 | +def greet(name): |
| 90 | + return f"Hello, {name}!" |
| 91 | +``` |
| 92 | + |
| 93 | +You can have any number of parameters in a function. |
| 94 | + |
| 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! |
| 109 | +``` |
| 110 | + |
| 111 | +The variable `name` will hold whatever it was passed inside the function's parentheses upon function call. |
| 112 | + |
| 113 | +## Number of parameters |
| 114 | + |
| 115 | +A function can have any number of parameters. |
| 116 | + |
| 117 | +For example: |
| 118 | + |
| 119 | +```python |
| 120 | +def greet(name, age): |
| 121 | + return f"{name} is {age} years old!" |
| 122 | +``` |
| 123 | + |
| 124 | +## Named arguments |
| 125 | + |
| 126 | +If you want to be specific about what arguments you pass to parameters, use named arguments. |
| 127 | + |
| 128 | +Example: |
| 129 | + |
| 130 | +```python |
| 131 | +def greet(name, age): |
| 132 | + return f"{name} is {age} years old!" |
| 133 | + |
| 134 | +print(greet(name="John Doe", age=31)) |
| 135 | +``` |
| 136 | + |
| 137 | +## Default parameter values |
| 138 | + |
| 139 | +Parameters can have default values: |
| 140 | + |
| 141 | +```python |
| 142 | +def greet(name="John Doe"): |
| 143 | + return f"Hello, {name}!" |
| 144 | + |
| 145 | + |
| 146 | +print(greet()) # Hello, John Doe! |
| 147 | +``` |
| 148 | + |
| 149 | +When passing an argument to the function, the default parameter value will be overwritten by the given argument. |
| 150 | + |
| 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 | +If the function has a mix of normal and default parameters, the default parameters have to be passed last. |
| 162 | + |
| 163 | +Correct: |
| 164 | + |
| 165 | +```python |
| 166 | +def greet(age, name="John Doe"): |
| 167 | + return f"{name} is {age} years old!" |
| 168 | + |
| 169 | + |
| 170 | +print(greet(32)) |
| 171 | +``` |
| 172 | + |
| 173 | +Incorrect: |
| 174 | + |
| 175 | +```python |
| 176 | +def greet(name="John Doe", age): |
| 177 | + return f"{name} is {age} years old!" |
| 178 | + |
| 179 | + |
| 180 | +print(greet(32)) |
| 181 | +``` |
| 182 | + |
| 183 | +Read more about Python functions [here](https://www.teclado.com/30-days-of-python/python-30-day-12-functions). |
| 184 | + |
| 185 | +# Python Lambda Expressions |
| 186 | + |
| 187 | +- Lambda expressions are an alternative syntax for defining simple functions. |
| 188 | + |
| 189 | +Syntax: `lambda arguments: expression` |
| 190 | + |
| 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 |
| 194 | + |
| 195 | +Example: |
| 196 | + |
| 197 | +```python |
| 198 | +result = (lambda x, y: x + y)(15, 3) |
| 199 | +``` |
| 200 | + |
| 201 | +Read more about lambda expressions [here](https://www.teclado.com/30-days-of-python/python-30-day-16-lambda-expressions#lambda-expressions). |
0 commit comments