In: Computer Science
Suppose as a language designer, you plan to implement a feature called “output-only” parameters. The expected semantics is that all output-only parameters are uninitialized when the callee starts execution; they can be used (both read and write) as other kinds of parameters within the callee; finally, the corresponding actual parameter in the caller is updated to the final values of the “output-only” parameters. Briefly describe one possible implementation of such “output-only” parameters
ANSWER:--
GIVEN THAT:--
Find below two possible "output-only" parameters.
As far as python is concerned python doesnt support call by reference, i.e., functions with output parameters.
so return multiple values, we can simply return in tuple
format and od unpacking at the call side.
def demonstartingOutputParameters(x,y):
x = 'Hi' # Here x and y are local
variables
y = y + 1 # which are assigned new
values
return x,y # and new values are
returned.
a, b = 'Hello',199
a, b = demonstartingOutputParameters(a,b)
print a,b
This is just the straight forward way.
second way is by passing a mutable object such as list,dictionary as shown below.
def demonstartingOutputParameters(x):
x[0] = 'Hello'
x[1] = x[1]+1
myCustomArgs = ['Hi',199]
demonstartingOutputParameters(myCustomArgs)
print myCustomArgs [0], myCustomArgs[1]
Output: