Skip to content

Day 7: Python Functions

Understanding Functions in Python

Functions in Python are defined blocks of code that can be invoked by their specific names. They serve as a crucial tool for organizing and reusing code, enhancing the efficiency of your programming. To define a function in Python, the 'def' keyword is utilized, followed by the function name and a set of parentheses. These parentheses may encompass parameters as required. The code block within the function is indented for clarity and structure. Moreover, Python functions can return a value using the 'return' keyword, further expanding their utility.

Defining a Function in Python

# Define a function
def greet(name):
    print("Hello, " + name)

In the above example, we have defined a function named 'greet'. This function takes one parameter, 'name', and prints a greeting message. To call this function, we simply use its name followed by the argument in parentheses:

greet("John")  # Output: "Hello, John"

Returning a Value from a Python Function

# Return a value from a function
def add(x, y):
    return x + y

In this example, we have a function named 'add' that takes two parameters, 'x' and 'y'. The function returns the sum of these two parameters. To use this function and store the returned value, we assign the function call to a variable:

result = add(3, 4)  # Output: 7

In this case, 'result' will hold the value returned by the 'add' function, which is the sum of the arguments passed.

Learn More

Want to learn more about Python for Machine Learning? Check out the full course HERE.


Need help mastering Machine Learning?

Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.