Discussion Assignment Unit2
Discussion Assignment Unit2
Write 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: Construct a function with a local variable. Show what happens when you try to
use that variable outside the function. Explain the results.
Example 4: Construct 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.
The code and its output must be explained technically whenever asked. The explanation
can be provided before or after the code, or in the form of code comments within the code.
For any descriptive type question, Your answer must be at least 150 words.
End your discussion post with one question related to programming fundamentals learned
in this unit from which your colleagues can formulate a response or generate further
discussion. Remember to post your initial response as early as possible, preferably by
Sunday evening, to allow time for you and your classmates to have a discussion.
Example 1
argument(name)
In this example, we have a function called argument that takes a parameter named name. The name
parameter represents the argument that will be passed to the function when called, and is passed by
the result statement returning the function argument.
The code ‘name’ is the parameter, it allows us to use the value provided during the function call.
The code ‘Sayile’ is the argument, this is the value that we want to pass into the parameter ‘name’,
which is then passed into the function argument.
…………
Example 2
// Value Argument
#Value argument
argument('This is a value argument')
In the value Argument, we directly pass the value ‘This is a value argument’ as the argument when
calling the function.
// Variable argument
#Variable argument
Test = 'This is a variable argument'
argument(Test)
When it comes to variable arguments, I assigned the value ‘This is a variable argument’ to the variable
‘Test’. Then pass the variable as an argument when calling the function.
// Expression argument
#Expression argument
Test = "This" + " is an " + "Expression argument."
argument(Test)
An expression argument in Python refers to an argument that is an expression, rather than a specific
value or variable.
In Python, an expression is a combination of values, variables, operators, and function calls that are
evaluated to produce a result. It can involve mathematical operations, logical operations, string
concatenation, or any other valid Python expression.
When a function takes an expression as an argument, it means that the function expects the result of
evaluating that expression to be passed in. The expression can be inline within the function call or
assigned to a variable beforehand.
From the above code, we define a variable ‘Test’ and assign the value ‘this’ + ‘ is an ‘ + “ Expression
argument’ to it.
……………
Example 3
……………….
Example 4
def junub(x):
print(x)
junub(10)
In this example, the function `junub()` takes an argument named `x`. When the function is called, the
argument is passed to the function and the value of the argument is printed.However, when you try to
use the name `x` outside the function, you get an error 'NameError:' because the name is not
defined.This is because the name `x` is only valid inside the function `junub()`.
Example 5
#using the same variable name in function and outside the function
x = 10
def junub():
x = 20
print(x)
junub()
print(x)
In this example, the variable `x` is first defined outside the function `junub()` with the value 10. Then,
inside the function `junub()`, a new variable `x` is defined with the value 20. When the function `junub()`
is called, the value of the local variable `x` is printed, which is 20. Then, the value of the global variable
`x` is printed, which is still 10.