Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 4 - Functions

4.3 Returning Values

The function can return values using the return statement.

For example, let's create a function that takes two numbers as parameters and adds them mathematically:

def add(x, y):
    return x + y

print("Result:", add(3, 5))

In the above code block, the function add() takes two numbers, adds them, and returns the result.

Now, let's create a function that solves the equation x + y - z:

def solve_equ(x, y, z):
    return add(x, y) - z

# Function to add two numbers
def add(x, y):
    return x + y

print("Result:", solve_equ(3, 5, 2))

Notice that the function solve_equ() depends on the function add(). This approach is known as nested functions, where multiple functions are interconnected with each other.

Feel free to edit the code and apply a different equation as needed.