Saturday, April 20, 2024
HomePythonUsing return and international key phrases

Using return and international key phrases


Okay so right here we’ve got one other put up. This put up is in regards to the return key phrase. you may need encountered some features written in python which have a return key phrase in the long run of the perform. Are you aware what it does? Lets look at this little perform:

>>> def add(value1,value2):
...     return value1 + value2

>>> consequence = add(3,5)
>>> print consequence
8

The perform above takes two values as enter after which output their addition. We may have additionally executed:

>>> def add(value1,value2):
...     international consequence
...     consequence = value1 + value2

>>> add(3,5)
>>> print consequence
8

So first lets discuss in regards to the first little bit of code which entails the return key phrase. What that perform is doing is that it’s assigning the worth to the variable which is asking that perform which in our case is consequence.

It’s fairly helpful generally and also you received’t want to make use of the worldwide key phrase. Nevertheless lets look at the opposite little bit of code as effectively which incorporates the worldwide key phrase. So what that perform is doing is that it’s making a worldwide variable consequence. What does international imply right here ? World variable signifies that we will entry that variable outdoors the perform as effectively. Let me reveal it with an instance:

# first with out the worldwide variable
>>> def add(value1,value2):
	consequence = value1 + value2
	
>>> add(2,4)
>>> consequence

# Oh crap we encountered an exception. Why is it so ?
# the python interpreter is telling us that we don't 
# have any variable with the title of consequence. It's so 
# as a result of the consequence variable is simply accessible inside 
# the perform by which it's created if it's not international.
Traceback (most up-to-date name final):
  File "", line 1, in 
    consequence
NameError: title 'consequence' isn't outlined

# Now lets run the identical code however after making the consequence 
# variable international
>>> def add(value1,value2):
	international consequence
	consequence = value1 + value2

	
>>> add(2,4)
>>> consequence
6

So hopefully there have been no errors within the second run as anticipated. If you wish to additional discover them it is best to take a look at the next hyperlinks.

  1. Stackoverflow
  2. Stackoverflow
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments