Junior — Middle
How can you define and call a function in Python?
sobes.tech AI
Answer from AI
In Python, a function is defined using the def keyword, followed by the function name and parameters. The function is called by its name with arguments inside parentheses.
Example of defining and calling a function:
def greet(name):
return f"Hello, {name}!"
message = greet("World")
print(message) # Outputs: Hello, World!
Here, greet is a function that takes one parameter name and returns a greeting string. Calling greet("World") returns the string which is then printed.