In Python, you can define a function using the def keyword. Here's an example of how to define a simple function that takes two arguments and returns their sum:
def add_numbers(x, y): return x + y |
In this example, the function is named add_numbers and takes two arguments, x and y. The function body consists of a single line that returns the sum of x and y. To call this function, you simply pass in two numbers as arguments:
result = add_numbers(3, 4)print(result) # Output: 7 |
This will call the add_numbers function with x=3 and y=4, and return the result, which is then printed to the console.
You can also define functions with default arguments, which are used when an argument is not provided:
def greet(name='world'): |
In this example, the greet function has a default argument of 'world'. If no argument is provided when the function is called, it will use the default argument. If an argument is provided, it will use that instead.