All Tracks/lecture understanding functions and scope/
Đang tải...
Hướng dẫn thử thách
1 / 2

How Do Functions Work in Python?

Functions are reusable pieces of code that run when you call them. Python provides built-in functions, including `print()`, which you used in previous lessons. Another helpful built-in function is `input()`, which lets you prompt the user for input: ```python name = input('What is your name?') # User types "Kolade" and presses Enter print('Hello', name) # Output: Hello Kolade ``` On the other hand, `int()` converts a number, boolean, and a numeric string into an integer: ```python print(int(3.14)) # 3 print(int('42')) # 42 print(int(True)) # 1 print(int(False)) # 0 ``` You can also write your own custom functions. To do that, you use the `def` keyword, followed by the name you want to give your function, a pair of parentheses, and a colon. Then on a new line, you write the code your function should run. The code the function runs is also referred to as the function's body. Here's an example of a custom function named `hello` that prints the string `Hello World` to the terminal: ```python def hello(): print('Hello World') ``` To run the function, you need to call it with its name followed by a pair of parentheses: ```python hello() # Hello World ``` Notice the indentation before `print('Hello World')`. As you may recall from previous lessons, Python relies on indentation to determine which groups of statements belong together. These groups of statements are called code blocks. Here's another simple function that prints the sum of two numbers to the terminal: ```python def calculate_sum(a, b): print(a + b) ``` You can see that our function, `calculate_sum`, has `a` and `b` in its parentheses, separated by a comma. Those are called parameters. Think of parameters as placeholder variables that act as "slots" for the values you pass into functions when you call them. To use the parameters, you have to pass in "arguments". Arguments are the values you pass to a function when you call it. Here's how to call the `calculate_sum` function to sum together the numbers `3` and `1`: ```python calculate_sum(3, 1) # 4 ``` If you call the function without the correct number of arguments, you'll get a `TypeError`: ```python calculate_sum() # TypeError: calculate_sum() missing 2 required positional arguments: 'a' and 'b' ``` Functions also use a special `return` keyword to exit the function and return a value. If you don't explicitly use `return`, Python will return `None` by default. `None` is a special value that represents the absence of a value. It is the only value of the `NoneType` data type. `None` is immutable and falsy, which means it cannot be changed and evaluates to `False` in a boolean context. Here's an example: ```python def calculate_sum(a, b): print(a + b) my_sum = calculate_sum(3, 1) # 4 print(my_sum) # None ``` You can see that the `calculate_sum` function prints the sum of `a` and `b`, but it doesn't return anything explicitly. So when we assign its result to `my_sum`, the value is actually `None`. To fix that, you can use the `return` keyword to send back the result: ```python def calculate_sum(a, b): return a + b my_sum = calculate_sum(3, 1) print(my_sum) # 4 ``` Now, `calculate_sum` returns the sum of `a` and `b`, which gets stored in `my_sum`.
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.py
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵
Test Output
Thử thách này không có bài test tự động. Hãy quan sát kết quả trực tiếp ở khung Preview.