|
| 1 | +# Python Functions |
| 2 | + |
| 3 | +**Video link:** |
| 4 | + |
| 5 | +In this video, we learned about Python functions that make our program more organized and manageable by dividing our code into smaller and modular chunks. |
| 6 | + |
| 7 | +**Programs in the Video** |
| 8 | + |
| 9 | +- [Python Functions](#python-functions-1) |
| 10 | +- [Function Arguments](#function-arguments) |
| 11 | +- [Passing Multiple Arguments](#passing-multiple-arguments) |
| 12 | +- [Return Value from Function](#return-value-from-function) |
| 13 | +- [**Example**: Grading Students](#example) |
| 14 | +- [**Task**: Function to Add and Multiply](#programming-task) |
| 15 | + |
| 16 | +--- |
| 17 | +## Python Functions |
| 18 | +A function is a group of related statements that performs a specific task. |
| 19 | + |
| 20 | +For example, |
| 21 | + |
| 22 | +```python |
| 23 | +def greet(): |
| 24 | + print("Hello") |
| 25 | + print("How do you do?") |
| 26 | +``` |
| 27 | +Here, we have defined a function named `greet`. |
| 28 | + |
| 29 | +To create a function, we use the `def` keyword followed by the function name, parenthesis `()`, and a colon `:`. |
| 30 | +The body of the function is specified using indentation. |
| 31 | + |
| 32 | +When we run the program, we don't see any output. |
| 33 | + |
| 34 | +It is because defining a function won't do anything. To bring the function into action, we need to call it. |
| 35 | + |
| 36 | +```python |
| 37 | +def greet(): |
| 38 | + print("Hello") |
| 39 | + print("How do you do?") |
| 40 | + |
| 41 | +greet() |
| 42 | +``` |
| 43 | + |
| 44 | +**Output** |
| 45 | + |
| 46 | +``` |
| 47 | +Hello |
| 48 | +How do you do? |
| 49 | +``` |
| 50 | + |
| 51 | +One advantage of defining a function is that we can call it any number of times. |
| 52 | + |
| 53 | +```python |
| 54 | +def greet(): |
| 55 | + print("Hello") |
| 56 | + print("How do you do?") |
| 57 | + |
| 58 | +greet() |
| 59 | +greet() |
| 60 | +greet() |
| 61 | +``` |
| 62 | + |
| 63 | +**Output** |
| 64 | + |
| 65 | +``` |
| 66 | +Hello |
| 67 | +How do you do? |
| 68 | +Hello |
| 69 | +How do you do? |
| 70 | +Hello |
| 71 | +How do you do? |
| 72 | +``` |
| 73 | + |
| 74 | +Also, we need to define a function first before we can call it. |
| 75 | + |
| 76 | +The following code gives an error: |
| 77 | + |
| 78 | +```python |
| 79 | +# function call |
| 80 | +greet() |
| 81 | + |
| 82 | +# function definition |
| 83 | +def greet(): |
| 84 | + print("Hello") |
| 85 | + print("How do you do?") |
| 86 | +``` |
| 87 | + |
| 88 | +When the `greet()` function is called, Python doesn't know that this function exists because it's defined after the function call. |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Function Arguments |
| 93 | + |
| 94 | +Suppose we want to make our `greet()` function a bit more personal. |
| 95 | + |
| 96 | +Instead of printing `Hello`, we want to print something like `Hello Jack` or whatever the person's name is. |
| 97 | + |
| 98 | +For this, we can use function arguments: |
| 99 | + |
| 100 | +```python |
| 101 | +def greet(name): |
| 102 | + print("Hello", name) |
| 103 | + print("How do you do?") |
| 104 | + |
| 105 | +greet("Jack") |
| 106 | +``` |
| 107 | +**Output** |
| 108 | + |
| 109 | +``` |
| 110 | +Hello Jack |
| 111 | +How do you do? |
| 112 | +``` |
| 113 | + |
| 114 | +Function arguments are passed inside the parenthesis during the function call. |
| 115 | + |
| 116 | +It can then be accessed using the `name` parameter in the function definition. |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## Passing Multiple Arguments |
| 121 | + |
| 122 | +If we need to pass multiple arguments to a function, we can separate them by commas. |
| 123 | + |
| 124 | +Let's create a function to add two numbers. |
| 125 | + |
| 126 | +```python |
| 127 | +def add_numbers(n1, n2): |
| 128 | + result = n1 + n2 |
| 129 | + print("The sum is", result) |
| 130 | + |
| 131 | + |
| 132 | +number1 = 5.4 |
| 133 | +number2 = 6.7 |
| 134 | +add_numbers(number1, number2) |
| 135 | + |
| 136 | +``` |
| 137 | + |
| 138 | +**Output** |
| 139 | + |
| 140 | +``` |
| 141 | +The sum is 12.100000000000001 |
| 142 | +``` |
| 143 | +We have passed `number1` and `number2` as arguments to the `add_numbers()` function. |
| 144 | +These arguments are accepted as `n1` and `n2` once they are passed to the `add_numbers()` function. |
| 145 | + |
| 146 | +>**Note:** We get this number instead of 12.1 because of floating-point representation error in Python. |
| 147 | +
|
| 148 | +--- |
| 149 | + |
| 150 | +## Return Value from Function |
| 151 | + |
| 152 | +```python |
| 153 | +def add_numbers(n1, n2): |
| 154 | + result = n1 + n2 |
| 155 | + print("The sum is", result) |
| 156 | + |
| 157 | + |
| 158 | +add_numbers(5.4, 6.7) |
| 159 | +``` |
| 160 | +Sometimes it's better just to find the sum inside the function and print the result somewhere else. |
| 161 | + |
| 162 | +We can achieve that by using the `return` statement. |
| 163 | + |
| 164 | +```python |
| 165 | +def add_numbers(n1, n2): |
| 166 | + result = n1 + n2 |
| 167 | + return result |
| 168 | + |
| 169 | +result = add_numbers(5.4, 6.7) |
| 170 | +print("The sum is", result) |
| 171 | +``` |
| 172 | + |
| 173 | +**Output** |
| 174 | + |
| 175 | +``` |
| 176 | +The sum is 12.100000000000001 |
| 177 | +``` |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## Types of functions |
| 182 | + |
| 183 | +There are two types of functions: |
| 184 | + |
| 185 | +- **Built-in functions** - Functions that are built into Python. |
| 186 | +- **User-defined functions** - Functions defined by the users themselves. |
| 187 | + |
| 188 | +Some built-in functions: |
| 189 | + |
| 190 | +|Function|Description| |
| 191 | +|---|---| |
| 192 | +|`float()`|converts to decimal number and returns it| |
| 193 | +|`int()`|converts to integer and returns it| |
| 194 | +|`input()`|function to take input from the user| |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## Example |
| 199 | + |
| 200 | +#### Grading Student Based on Marks Obtained by Making Functions |
| 201 | + |
| 202 | +Suppose you just attended a University examination. The marks you obtained in various subjects are stored in a list like this: |
| 203 | + |
| 204 | +```python |
| 205 | +marks = [55, 64, 75, 80, 65] |
| 206 | +``` |
| 207 | + |
| 208 | +**You want to find the average marks you obtained in the exam.** |
| 209 | + |
| 210 | +**Based on the average marks you want to find your grade as:** |
| 211 | + |
| 212 | +- **You will get Grade A if the average marks is equal to or above 80** |
| 213 | +- **You will get Grade B if the average marks is equal to or above 60 and less than 80** |
| 214 | +- **You will get Grade C if the average marks is equal to or above 50 and less than 60** |
| 215 | +- **And if the average marks is less than 50, you will get Grade F** |
| 216 | + |
| 217 | +```python |
| 218 | +# find the average marks and return it |
| 219 | +def find_average_marks(marks): |
| 220 | + sum_of_marks = sum(marks) |
| 221 | + number_of_subjects = len(marks) |
| 222 | + |
| 223 | + average_marks = sum_of_marks/number_of_subjects |
| 224 | + |
| 225 | + return average_marks |
| 226 | + |
| 227 | +# compute grade and return it |
| 228 | +def compute_grade(average_marks): |
| 229 | + if average_marks >= 80.0: |
| 230 | + grade = 'A' |
| 231 | + elif average_marks >= 60: |
| 232 | + grade = 'B' |
| 233 | + elif average_marks >= 50: |
| 234 | + grade = 'C' |
| 235 | + else: |
| 236 | + grade = 'F' |
| 237 | + |
| 238 | + return grade |
| 239 | + |
| 240 | +marks = [55, 64, 75, 80, 65] |
| 241 | +average_marks = find_average_marks(marks) |
| 242 | +grade =compute_grade(average_marks) |
| 243 | + |
| 244 | +print("Your average marks is", average_marks) |
| 245 | +print("Your grade is", grade) |
| 246 | +``` |
| 247 | + |
| 248 | +**Output** |
| 249 | +``` |
| 250 | +Your average marks is 67.8 |
| 251 | +Your grade is B |
| 252 | +``` |
| 253 | + |
| 254 | +--- |
| 255 | + |
| 256 | +## Programming Task |
| 257 | + |
| 258 | +**Can you create a program to add and multiply two numbers?** |
| 259 | + |
| 260 | +**For this, create two functions `add_numbers()` and `multiply_numbers()`. |
| 261 | +These functions should compute the result and return them to the function call and should print from outside the function.** |
| 262 | + |
| 263 | + |
| 264 | +```python |
| 265 | +# function to add two numbers |
| 266 | +def add_numbers(num1, num2): |
| 267 | + return num1 + num2 |
| 268 | + |
| 269 | +# function to multiply two numbers |
| 270 | +def multiply_numbers(num1, num2): |
| 271 | + return num1 * num2 |
| 272 | + |
| 273 | +number1 = 5 |
| 274 | +number2 = 30 |
| 275 | + |
| 276 | +num_sum = add_numbers(number1, number2) |
| 277 | +print("Sum is", num_sum) |
| 278 | + |
| 279 | +num_product = multiply_numbers(number1, number2) |
| 280 | +print("Product is", num_product) |
| 281 | +``` |
| 282 | + |
| 283 | +**Output** |
| 284 | + |
| 285 | +``` |
| 286 | +Sum is 35 |
| 287 | +Product is 150 |
| 288 | +``` |
0 commit comments