Discussion Assignment (PYTHON) ..
Discussion Assignment (PYTHON) ..
Example No.2:
solution:
Function(5)
num=2
function(num)
function(3-1)
Explanation:
1. A value:
function(5)
In no.1, the argument is 5; a value
2. A Variable
num = 2
function(num)
In no.2, the argument is num; a variable
3. An Expression
function(3-1)
In no.3, the argument is 3-1; an expression
Example no.3:
solution:
def main():
print(f”5+6={add(5,6)}”)
main()
Output:
#Sum result=45
#5+6=11
Explanation:
The above function created with a local variable.
def add(number1,number2):
#1. A local variable
sumResult=number1+number2
return sumResult
#2. Use the same variable outside the function.
sumResult=45
print(f”sumResult={sumResult}”)
Hence, the local variable inside a function (1) and a variable and outside
the function (2) are two different variables. The values of these
variables don’t overlap eachother.
Example no.4
solution:
def func(x):
print(x)
x=8
func(x)
Explanation:
#giving a unique name to the function parameter (x)
#what happens to the value of each of the variable as the program runs
Using parameter outside the function may just work well without
producing any error. For example, the func function will just output 8
without producing any error.
Example no.5
solution:
When a parameter has the same name as a variable defined outside,
instead of the function using the variable defined outside, it will only
reference the value that was passed to the parameter. So, parameters
will be used over variables of the same name within a function.
Explanation:
For example:
name = "Tyler"
# The parameter has the same name as the variable above.
# The function will not use the variable's value, but the value
# passed into this parameter instead.
def sayHi(name):
print("Hi, " + name + "!")
sayHi("Anne")
# This will print out “Hi, Anne!”