✏️ Explanatory Question

How do you define a function in Python?

👁 248 Views
📘 Detailed Answer
💡

Answer with Explanation

In Python, you can define a function using the def keyword followed by the function name, a set of parentheses containing any required parameters, and a colon. The code block within the function must be indented. For example:


def my_function(param1, param2):
    # Function code here
    result = param1 + param2
    return result

In this example, my_function takes two parameters param1 and param2, performs an operation to add them, and returns the result. To call the function, you simply call the function name and pass in any required arguments:


result = my_function(10, 20)
print(result)

This would output 30.