Check out this video for an overview of the article. Then, read the article and solve exercises to strengthen your understanding.
1 Functions
In “Python” language, a function serves as a block of code that performs a specific task and can be reused whenever needed. Functions help in organizing code, reducing redundancy, and improving the readability of the code.
1.1 Defining a Function
To define a function in “Python” language, you use the def
keyword, followed by the function name and parentheses. You can also specify parameters inside the parentheses. Parameters are the variables that catch input values.
def greet(name):
print(f"Hello, {name}")
Let’s break down the definition.
def greet(name):
: The def
keyword is used to define a function. The greet
is the name of the function being defined. name
inside the parentheses is the parameter that the function takes. The function expects an argument to be passed when it is called (See Section 1.2). For the difference between parameters and arguments, see Tip 1.
print(f"Hello, {name}")
: This line is the body of the function greet()
. It prints a greeting message using the print
function. The body of a function is indented and separated by other parts of the codes. It is recommended that you use 4 spaces to indent the body of the function.
1.2 Calling a Function
To execute a function and perform the task defined within it, you need to call the function by using its name, followed by parentheses. If the function requires any arguments, you can pass them within the parentheses.
# Calling greet function
greet("Alice")
Hello, Alice
Here, Alice
is the argument passed to the function, and it is assigned to the parameter name
. Now you can use the value Alice
inside the function.
Tip 1: TIPS: Difference between Parameters and Arguments
Parameters are the variables that a function takes. When you call a function, a value is passed to the parameter. This value is called the argument.
1.3 Return Statement
Functions can return a value using the return
statement. The return
statement allows the function to send data back to the code that called it.
def add_numbers(x, y):
return x + y
result = add_numbers(5, 10)
print(result)
15
The x
and y
inside the parentheses are the parameters that the function takes. The function expects two arguments to be passed when it is called. In the function body, there is return x + y
. This line adds values in x
and y
. Then it returns the result when the function is called.
In result = add_numbers(5, 10)
, the function add_numbers()
is called with arguments 5
and 10
. The function returns x + y
, assigning the value to the variable result
.
1.4 Scope of Variables
Variables defined inside a function are local to that function and are not accessible outside of it unless explicitly returned. Global variables, defined outside the function, can be accessed within the function.
global_var = 10
def display_var():
local_var = 20
print(f"local_var: {local_var}")
# You can access a global variable
print(f"global_var: {global_var}")
display_var()
print(global_var)
try:
# You can not access a variable inside the function
# This raises an error
print(local_var)
except NameError as e:
print(e)
local_var: 20
global_var: 10
10
name 'local_var' is not defined