Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Discussion Assignment

Assignment no.2 (By Thomas Gillespie)


Example no.1:
solution:
def mul(num):
print(num**3)
num=2
mul(num)
Explanation:
The above easy example returns the cube of a number (the parameter
is what is being passed to a function). In this example the parameter is
Num and the value of a parameter which is referred to argument. In
this case, the argument is 2. So, when the program is executed, the
result is 8 (i.e. 2 =8)
3

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!”

# The value of the variable defined outside was unchanged.


# This will print out “Tyler”.
print(name)
References used in the Assignments:
 Python Programming Language - GeeksforGeeks. (2021).
Retrieved 10 September 2021, from
https://www.geeksforgeeks.org/python-programming-language/
 Python Programming Assignment Help From 5000+ Experts| A+
Grade. (2021). Retrieved 10 September 2021, from
https://myassignmenthelp.com/programming-help/python-
programming-assignment-help.html
 Online Python Compiler (Interpreter). (2021). Retrieved 10
September 2021, from https://www.programiz.com/python-
programming/online-compiler/

You might also like