100% found this document useful (1 vote)
1K views3 pages

CS1101 Discussion U2

The document provides 5 examples of defining and calling functions in Python. It demonstrates: 1. Defining a function that takes a parameter and calling it with an argument. 2. Calling the function from example 1 multiple times with different argument types - a value, variable, and expression. 3. What happens when trying to access a local variable defined inside a function from outside the function. 4. What happens when trying to access a parameter name outside the function. 5. What happens when a variable name outside a function is the same as a local variable name inside the function. The variables are independent and do not affect each other.

Uploaded by

Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
100% found this document useful (1 vote)
1K views3 pages

CS1101 Discussion U2

The document provides 5 examples of defining and calling functions in Python. It demonstrates: 1. Defining a function that takes a parameter and calling it with an argument. 2. Calling the function from example 1 multiple times with different argument types - a value, variable, and expression. 3. What happens when trying to access a local variable defined inside a function from outside the function. 4. What happens when trying to access a parameter name outside the function. 5. What happens when a variable name outside a function is the same as a local variable name inside the function. The variables are independent and do not affect each other.

Uploaded by

Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 3

Example 1: 

Define a function that takes an argument. Call the function. Identify what code is the
argument and what code is the parameter.
>>> def message_first(name):
         print("Dear " + name) 
>>> message_first("Ahmad")
Dear Ahmad
Explanation: 
The name of the function: message_first 
The parameter: name
The argument: Ahmad

Example 2: Call your function from Example 1 three times with different kinds of arguments: a
value, a variable, and an expression. Identify which kind of argument is which. 

>>> message_first("John")
Dear John
Explanation: 
The argument is a value: “John”
…………………………………………………………..
>>> var = " Mr.Ali, Welcome to our course"
>>> message_first(var)
Dear Mr.Ali, Welcome to our course
Explanation: 
The argument is a variable: var = "Mr.Ali, Welcome to our course"
…………………………………………………………..
>>> x = "Ali "
>>> y = "Noah "
>>> exp = x + y + "." * 5
>>> message_first(exp)
Dear Ali Noah .....
Explanation: 
The argument is an expression: x + y + "." * 5

Example 3: Create a function with a local variable. Show what happens when you try to use that
variable outside the function. Explain the results.
>>> def message_first(name):
              greeting = "Dear " + name
              print(greeting)
>>> message_first("Ali")
Dear Ali
>>> print(greeting)
File "C:/Users/user/PycharmProjects/MyAssignment/U2.py", line 5, in <module>
    print(greeting)
NameError: name 'greeting' is not defined
Explanation:
The local variable in this case is: greeting = "Dear " + name
When I try to use that variable outside the function, I get this message: NameError: name
'greeting' is not defined. Because local variables exist inside the function, it can't be accessed
outside the function. NameError shows that the variable needs to be defined outside the function.

Example 4: Create a function that takes an argument. Give the function parameter a unique
name. Show what happens when you try to use that parameter name outside the function.
Explain the results.
>>> def study_book(color):
     print("Take your " + color + " book")
>>> study_book("red")
Take your red book
>>> print(color)
File "C:/Users/user/PycharmProjects/MyAssignment/U2.py", line 5, in <module>
    print(color)
NameError: name 'color' is not defined
Explanation:
The parameter is: color
I get this message: NameError: name 'greeting' is not defined. Because parameters are local, they
do not exist outside the function, and like local variables, they can't be accessed outside the
function.

Example 5: Show what happens when a variable defined outside a function has the same name
as a local variable inside a function. Explain what happens to the value of each variable as the
program runs.
>>> def child_books(color1, color2):
        color1 = "Red"
        color2 = "Blue"
        books = color1 + color2
        print(books)
>>> child_books(1, 2)
RedBlue
>>> color1="Red"
>>> color2="Blue"
>>> books = color1 + color2
>>> print(books)
RedBlue
Explanation:
Even if the variables outside and inside the function have the same name, the variables inside
child_books function do not affect outside similar variables. because these variables are
independent of each other because the Python interpreter follows the flow of execution.

You might also like