Tutorial 5.1 - Functions
Defining and calling functions
Let's delve deeper into defining and calling functions in Python, exploring different aspects and features.1. Defining Functions:
To define a function, you use the def keyword followed by the function name, a set of parentheses containing optional parameters, and a colon. The function body is indented below the definition.
def greet(name):
"""
This function greets the person with the given name.
"""
print("Hello,", name)
You can add a docstring to explain the purpose and usage of your function. Triple quotes (`'''` or `"""`) are used to define docstrings.
def greet(name):
2. Function Documentation (Docstrings):
You can add a docstring to explain the purpose and usage of your function. Triple quotes (`'''` or `"""`) are used to define docstrings.
def greet(name):
"""
This function greets the person with the given name.
Parameters:
name (str): The name of the person to greet.
"""
print("Hello,", name)
Functions can have parameters, which are placeholders for data that you pass when calling the function.
def add(a, b):
3. Function Parameters:
Functions can have parameters, which are placeholders for data that you pass when calling the function.
def add(a, b):
return a + b
You can provide default values for parameters, making them optional when calling the function.
def greet(name="Guest"):
4. Default Parameters:
You can provide default values for parameters, making them optional when calling the function.
def greet(name="Guest"):
print("Hello,", name)
Functions can return values using the return statement.
def multiply(a, b):
5. Return Values:
Functions can return values using the return statement.
def multiply(a, b):
return a * b
You can use the *args syntax to pass a variable number of arguments to a function as a tuple.
def print_args(*args):
6. Variable Number of Arguments:
You can use the *args syntax to pass a variable number of arguments to a function as a tuple.
def print_args(*args):
for arg in args: print(arg)
You can use named arguments to pass values to functions based on parameter names, regardless of their order.
def person_info(name, age):
7. Named Arguments (Keyword Arguments):
You can use named arguments to pass values to functions based on parameter names, regardless of their order.
def person_info(name, age):
print("Name:", name)
print("Age:", age)
A function can return multiple values as a tuple.
def square_and_cube(x):
8. Returning Multiple Values:
A function can return multiple values as a tuple.
def square_and_cube(x):
return x ** 2, x ** 3
Variables defined within a function are local to that function and don't affect variables outside of it.
def example():
9. Function Scopes:
Variables defined within a function are local to that function and don't affect variables outside of it.
def example():
local_variable = "This is local"
print(local_variable)
To call a function, use its name followed by parentheses containing the required arguments.
greet("Alice")
Understanding functions in-depth is crucial for writing efficient, maintainable, and organized code. They allow you to encapsulate logic, reuse code, and improve the readability of your programs.
10. Calling Functions:
To call a function, use its name followed by parentheses containing the required arguments.
greet("Alice")
11. Lambda Functions:
Lambda functions are small, anonymous functions defined using the lambda keyword.
double = lambda x: x * 2
You can access a function's docstring using help().
help(greet)
A function can call itself, which is known as recursion.
def factorial(n):
double = lambda x: x * 2
12. Docstring and Help:
You can access a function's docstring using help().
help(greet)
13. Recursive Functions:
A function can call itself, which is known as recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
Understanding functions in-depth is crucial for writing efficient, maintainable, and organized code. They allow you to encapsulate logic, reuse code, and improve the readability of your programs.