CS1101 Discussion Assignment Unit 2 - Argument, parameter, value, variable, and expression

Questions

Create your own Python code examples that demonstrate each of the following. Do not copy examples from the book or any other source. Try to be creative with your examples to demonstrate that you invented them yourself.

Example 1: Define a function that takes an argument. Call the function. Identify what code is the argument and what code is the parameter.

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.

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.

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.

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.

1. Example 1

Question:
Define a function that takes an argument. Call the function. Identify what code is the argument and what code is the parameter.

Codes:

1
2
3
4
def hello_function(para):
print(para)

hello_function("hello")

Identification:
hello is the argument; para is the parameter.

Explanation:
Parameter: “A name used inside a function to refer to the value passed as an argument” (Downey, 2015, p. 26).

Argument: “A value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function” (Downey, 2015, p. 26).

In short, parameter is a variable, and argument is a value. “hello” is a value, so it is the argument. para is a variable in the function, so it is the parameter.

2. Example 2

Question:
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.

Codes:

1
2
3
4
5
6
7
8
def hello_function(para):
print(para)

vara = "good"

hello_function(2021)
hello_function(vara)
hello_function(1*2+3)

Identification:
2021 is a value; vara is a variable; 1*2+3 is an expression.

Explanation:
Value: “One of the basic units of data, like a number or string, that a program manipulates” (Downey, 2015, p. 7).

Variable: “A name that refers to a value” (Downey, 2015, p. 14).

Expression: “A combination of variables, operators, and values that represents a single result” (Downey, 2015, p. 14).

In detail, 2021 is a number; vara is a variable defined by me, and its value is the string “good”; 1*2+3 is an expression, which is a combination of operators and numbers.

3. Example 3

Question:
Create a function with a local variable. Show what happens when you try to use that variable outside the function. Explain the results.

Codes:

1
2
3
4
def writing():
words = "I like Python"

print(words)

Outputs:

1
NameError: name 'words' is not defined

Explanation:
words is a variable inside the function writing(), so it is local. A local variable inside a function can not be used outside the function. When we try to print it, we get a NameError exception, which means there is no such thing as words.

4. Example 4

Question:
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.

Codes:

1
2
3
4
def sing_song(lyrics):
print(lyrics)

print(lyrics)

Outputs:

1
NameError: name 'lyrics' is not defined

Explanation:
lyrics is a parameter of the sing_song(), so it is local. A local parameter of a function can not be used outside the function. When we try to print it, we get a NameError exception, which means there is no such thing as lyrics.

5. Example 5

Question:
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.

Codes:

1
2
3
4
5
6
def test():
grades = "100"

grades = "100"

print(grades)

Outputs:

1
100

Debug:
Step 1:
step 1
Step 2:
step 2
Step 3:
step 3

Explanation:
As we can see, the result is 100. But 100 is the value of the variable grades outside the function test(), not the value of the local variable grades inside the function test().

“Python program has its flow of execution, execution always begins at the first statement of the program” (Downey, 2015, p. 21). The interpreter first executes the test function in the first line, but does not enter the function, and executes the statement grades = 100. This is because the test function is not called. Therefore, the local variable grades is not created and assigned. After the line of code is executed, the assignment statement grades = 100 outside the function will be executed. At this time, the variable grades will be created and assigned a value of 100. Finally, the print function is executed, and we can see the value of 100 on the screen. But note, this 100 is the value of the variable outside the function, not the value of the local variable inside the function.

If we want to see the specific execution process, we can debug it. Debugging results are given above. I have also attached my word document, the format may be better, welcome to download it.

Word count: 730

References

1.Downey, A. (2015). Think Python. Green Tea Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf

​


This is the ending, thanks for reading.