You are on page 1of 2

Example 1:

def greet(name):
print("Hello, " + name + "!")

greet("Gideon")

Output Hello, Gideon!

With this, I define a function called greet that takes an argument name. The argument is the
value that we pass to the function when we call it. In this case, the argument is the string
"Gideon". The parameter name is the variable that is to accept and receive the value of the
argument inside the function.

Example 2:

def greet(name):
print("Hello, " + name + "!")

greet("Gideon") # Argument: "Gideon" (value)


greet(my_name) # Argument: my_name (variable)
greet("Hello, " + "World!") # Argument: "Hello, " + "World!" (expression)

In this example, I call the greet function three times with different kinds of arguments. The first
call passes a value directly ("Gideon"), the second call passes a variable (my_name), and the third
call passes an expression ("Hello, " + "World!").

Example 3:

def my_function():
x = 10
print(x)

my_function()
print(x)

With this example, I define a function called my_function that has a local variable x. When we
call the function, it prints the value of x (10). However, when we try to print x outside the
function, we get an error because x is not defined in the global scope. Local variables are only
accessible within the function they are defined in.

Example 4:

def my_function(parameter):
print(parameter)

my_function("Hello")
print(parameter)
I define a function called my_function in this example that takes a parameter called
parameter. When I call the function, it prints the value of the parameter. However, when I try
to print parameter outside the function, I got an error because parameter is not defined in the
global scope. Parameters are local variables that are initialized with the value of the argument
when the function is called.

Example 5:

x = 10

def my_function():
x = 20
print("Local x:", x)

my_function()
print("Global x:", x)

In this example, we have a variable x defined outside the function and another variable x defined
inside the function. When we call the function, it prints the value of the local x (20). However,
when we print x outside the function, it refers to the global x (10). The local variable x inside the
function shadows the global variable x, meaning that the function has its own separate variable
with the same name. Changes made to the local variable do not affect the global variable.

You might also like