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

DISCUSSION ASSIGNMENT

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

Answer

def solve(o,b):

print(o*b)

solve(32,8)

256

Argument >>> 32,8

Parameter >>> o . b

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.

Answer

def solve(o,b):

print(o*b)

1. Value
Print (o, b)
Solve (24, 5)

2. Variable
def solve(o, b):
print (o*b)
a=14
b=19
solve (a,b)
14, 19
3 .Expression
def solve(o, b):
print (o*b)
d=8
solve(32 ,8 +d)
264.
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.
Answer
def test():
num1=33
num2=21
print(num1+num2)
>>> test()
54
>>> print(num1)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
print(num1)
NameError: name 'num1' is not defined
- The local variable are rather seen or visible to the inside function and are only visible
in the execution of the function. When calling a variable outside the function, the
outcome or result will be NameError. Therefore in other to work outside the function,
It should be a global variable, because they are used outside on the outside of the
function(Global Variables).

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.
Answer
>>> def my_name(b):
... print('My name is Makafui')
... print(b)
... 14
- I am not convinced , if my example is correct or incorrect. It seems to me that , there
is an error in my example , because parameters inside a function cannot be used on
outside a 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.
Answer
>>> qrs=555
>>> def ex_5():
... qrs=777
... print(qrs)
...
...
>>> print(qrs)
555
- This states that, inside the function only . the variable ‘qrs’ gets a new value , which
does not affect the value of the global variable.

My Question.
Apart from python programming which other way can one used to create his or own
code ?
Thank You.

You might also like