Do parameters still hold some value when the function call completes?

Question

In Python, do parameters still hold some value when the function call completes?

Answer

When a function call completes, all the parameters will no longer store any values in them.

Unlike regular variables we’ve worked with before, that were not parameters, parameters do not continue to reference some value after the function finishes running. Parameters are essentially “temporary placeholder variables”, so they only temporarily, for the duration of the function call, hold onto values, and reference them while the function is running. Once the function call completes entirely, the parameters will dereference any values and become “empty” again.

28 Likes

what happens if a value is declared using assignment with the same name as parameter_name being called. Which value would the function take? Define scope of the Value.

2 Likes

something like this:

def example(parameter_name):
   print(parameter_name)

parameter_name  = 5 
print(parameter_name)

that goes fine, the parameter has a local scope (only exists within the function), and can live along side the global variable

9 Likes

Mr. stetim94,
Thank you for the reply.
What if:

def example(parameter_name):
   parameter_name=7
   print(parameter_name)

Do i get a syntax error here?
What would be the value inside print statement of function definition, 5 or 7?
parameter_name = 5
print(parameter_name)

1 Like

def example(parameter_name):
print(parameter_name)

parameter_name = 5
print(parameter_name)
From the above problem i would like to ask whether 5 would be printed once or twice since there is no return statement after function definition?
Thank you.

If you reassign the parameter name within the function, then within the function, that name will have its new value. Assuming that this is what you mean (please use the </> icon to post code),

def example(parameter_name):
    parameter_name=7
    print("From inside the function, ",parameter_name)
parameter_name = 5
print("This is the variable defined outside the function, ",parameter_name)
example(parameter_name)
print("This is still the variable defined outside the function, ",parameter_name)

… the output would be:

This is the variable defined outside the function,  5
From inside the function,  7
This is still the variable defined outside the function,  5
18 Likes

Given you already have the code, surely you can run it, and then figure out what must have happened? Sometimes that is really useful to do, given then you figured it out, which is almost always better then an explanation.

5 Likes

Hello
Mr. stetim94
i’m currently in bash shell terminal.Can i test the commands directly from the terminal?
Thank you for making the effort to help me.

Hello
Mr. patrickd314
Thank you for the reply. This is the reply i’ve been waiting for and you explained meticulously.
Thank you for making the effort to teach me.

@stetim94 is correct: Many of these things you can just try out for yourself.

Do you have Python installed on your computer? If so, try typing python into the shell. If you are lucky (i.e., if the path is set up correctly), you will get a Python shell to use.

If you don’t have Python installed, there are several online Python interpreters that you can use. TutorialsPoint has them for both Python2 and Python 3.

In addition, Python Tutor has an excellent tool that lets you visualize code execution step by step.

There are many others; I would certainly advocate getting into the habit of keeping one open at all times (when you are doing anything having to do with coding, of course!!), and using it as a scratchpad.

5 Likes

In this exercise can we assign more than one formal parameters while defining a function?58%20PM
like in this example what if we also have one more greeting line that says
print("Todays sale special is " + Discount rate + “bars.”)

You never called the example function so it would only print once. and even if you called the example function you would have to pass it an argument to get any output.

I am new to python but I tried the following and it worked fine.
def greet_customer(special_item, discount):
print(“Welcome to Engrossing Grocers.”)
print("Our special is " + special_item + discount + “.”)
print(“Have fun shopping!”

greet_customer(“peanuts” , “at 3% discount”)

1 Like

@palinalyze thanks!
I was about to ask for this.
Anyway, I noticed that once you assign two arguments, the code will not work if you then try to call it with only one of them :thinking:

Python, which is very similar to most languages (except JavaScript) requires you to have all of the parameters in the function call. There are a few exceptions, though:

def someFunc(arg1, arg2="hello"):
  print(arg2 + arg1)
someFunc("World!")

This will print "Hello World!" as the second parameter (arg2) is given a default value, if it’s not called.
This article explains this very nicely, including some information on keyword arguments and arbitrary arguments.

1 Like

@codeneutrino thanks man!

1 Like

What about:

>>> def test(a, b=[]):
	b.append(a)
	return b

>>> test(1)
[1]
>>> test(2)
[1, 2]

This is covered here:

Common Gotchas — The Hitchhiker's Guide to Python

python will just make one list, and then use the list (or dictionary) every time. So you can’t use mutable data types as default argument.

1 Like