The document provides examples of defining and calling functions in Python, illustrating the use of parameters, arguments, local variables, and scope. It demonstrates how functions can accept different types of arguments, such as values, variables, and expressions. Additionally, it highlights the behavior of local variables and their interaction with variables defined in the outer scope.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views4 pages
Discussion
The document provides examples of defining and calling functions in Python, illustrating the use of parameters, arguments, local variables, and scope. It demonstrates how functions can accept different types of arguments, such as values, variables, and expressions. Additionally, it highlights the behavior of local variables and their interaction with variables defined in the outer scope.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
Example 1: Define a function that takes an argument
# Define a function that takes an argument
def greet(name): # 'name' is the parameter print("Hello, " + name + "!")
# Call the function with an argument
greet("Azeez") # '"Azeez"' is the argument
In this example, the code `def greet(name):` defines a function with a
parameter `name`. The code greet("Azeez") calls the function with the argument "Azeez" Example 2: Call the function with different kinds of arguments # Define the function again def greet(name): print("Hello, " + name + "!")
# Call the function with a value
greet("Azeezat") # '"Azeezat"' is a value
# Call the function with a variable
username = "Rosa" greet(username) # 'username' is a variable
# Call the function with an expression
greet("David " + "Elliott") # "David " + "Elliott" is an expression In this example, the function is called with three different kinds of arguments: a value ("Rosa"), a variable (`username`), and an expression ("David " + "Elliott").
Example 3: Construct a function with a local variable
# Define a function with a local variable def calculate_area(width, height): area = width * height # 'area' is a local variable print("The area is", area) # Call the function calculate_area(4, 6) # Try to use the local variable outside the function try: print("The area is", area) except NameError: print("Error: 'area' is not defined") In this example, the function `calculate_area` defines a local variable `area`. When we try to use the variable `area` outside the function, we get a `NameError` because `area` is not defined in the outer scope. Example 4: Construct a function that takes an argument with a unique parameter name # Define a function with a unique parameter name def greet_person(person_name): # 'person_name' is the parameter print("Hello, " + person_name + "!") # Call the function greet_person("Yekeen")
# Try to use the parameter name outside the function
try: print("The person's name is", person_name) except NameError: print("Error: 'person_name' is not defined") In this example, the function `greet_person` defines a parameter `person_name`. When we try to use the parameter name `person_name` outside the function, we get a `NameError` because `person_name` is not defined in the outer scope. Example 5: Show what happens when a variable defined outside a function has the same name as a local variable # Define a variable outside the function x = 10 # Define a function with a local variable with the same name def calculate_sum(y): x = y + 5 # 'x' is a local variable print("The sum is", x)
# Call the function
calculate_sum(20) # Print the value of the outer variable print("The outer variable x is still", x) In this example, the variable `x` is defined outside the function with the value `10`. The function `calculate_sum` defines a local variable `x` with a different value. When we call the function, the local variable `x` is used, but the outer variable `x` remains unchanged.