Junior — Middle
Can you give an example of calling a function in Python and explain how it works?
sobes.tech AI
Answer from AI
Calling a function in Python is the process of transferring control to a specific block of code that performs certain actions and may return a result.
Example:
def greet(name):
return f"Hello, {name}!"
message = greet("Anna")
print(message) # Will output: Hello, Anna!
Explanation:
def greet(name):— declares a function with the parametername.- When calling
greet("Anna"), the string "Anna" is passed to the function. - The function constructs a greeting string and returns it.
- The result is stored in the variable
messageand displayed on the screen.