Defining and Calling Functions
🔹 Use the def
keyword to define a function.
def greet():
print("Hello, welcome!")
greet()
Arguments
🔹 Functions can accept parameters to make them more dynamic.
def greet(name):
print("Hello, " + name)
greet("Alice")
Return Values
🔹 Use return
to send a result back from a function.
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Scope
🔹 Scope determines where a variable can be accessed. There are two main types: local and global.
x = 10 # Global variable
def show():
x = 5 # Local variable
print("Inside function:", x)
show()
print("Outside function:", x)