python function
Python hosting: Host, run, and code Python in the cloud!
A function is reusable code that can be called anywhere in your program. Functions improve readability of your code: it’s easier for someone to understand code using functions instead of long lists of instructions.
On top of that, functions can be reused or modified which also improve testability and extensibility.
Related Course:
Python Programming Bootcamp: Go from zero to hero
Function definition
We use this syntax to define as function:
def function(parameters): |
The def keyword tells Python we have a piece of reusable code (A function). A program can have many functions.
Practical Example
We can call the function using function(parameters).
#!/usr/bin/python |
Output:
9 |
The function has one parameter, x. The return value is the value the function returns. Not all functions have to return something.
Parameters
We can pass multiple variables:
#!/usr/bin/python |
Output:
|
If you are new to Python programming, I highly recommend this book.
Posted in Beginner
Leave a Reply:
THe concept of scope isn't really all that clear in your example. The first code snippet will actually work, because the function isn't actually doing anything with z.
Perhaps:
Perhaps other examples of code that does work as well:
... and an example of the confusion that can arise from reusing variable names:
Hi Tony,
I extended this part to contain more code examples. It's rather late here so hope everything went fine. Thanks for the heads up!
Not sure how this one got missed as Tony obviously time looking at this one, but in all examples, the end of the first print line is incomplete.
print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str($
will never work as you fail to correctly wrap the last str parameter ($), which itself is invalid.
should it not read
print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y)
Also, in this example
It is important to note that doB will print 3 which is the value passed to it, a=5 is contained within Doa and is still never used and therefore never visible to doB or the rest of the code.
Thanks stuartjk, I extended the article :-)
Thanks stuartjk! It seemed to disturb nobody thus far, but you are right.
The error came from copying source code from a terminal which capped the line. I fixed it and I'll copy from an IDE in the future. I hope you enjoy my articles, more will come soon.
Hey Frank, thank you very much for providing this tutorial set. it is the best one i have come across. i have no programming experience besides playing around in HS, and the KISS method you use made this very easy
Hey frank .. What does +str (x) ...+Str(y) means ???
Hi, this converts integers/floats to strings. Integers or floats are simply bytes in the memory and you cannot output them directly to the screen. str() converts them to human readable text.
I agree with Tony.
Right now the article states:
but I do see 3 get printed when I run the code.
Hi, thanks for your comment! You can print it but not modify it. Try z = 4 inside the function and it will give you an error. I will update the article.
Oh, didn't noticed that it's also explained here, really sorry.
Does pointers exist in Python?
I'm not sure if 'pointers' is the right word ... I mean the direction of a variable in memory.
Can you use that as a parameter in order to modify the content of a variable as you can do in C programming?
If you mean accessing the memory address directly, this is not a standard practice in Python. The language abstracts that for the programmer, in a similar way that C abstracts assembly or assembly abstracts machine language.
In Python 2.7 you can get the memory address, but this does not work with Jython, PyPy and Iron Python. It's a little tricky but here goes:
Python functions can return multiple return values, unlike C or C++, which you can then use to set your output variables. Variables (or objects) can be passed as parameters to a function.
Hi Roger, no problem
Please correct me if i wrong, but in this example:
we declared global variable z and then trying to use it inside function. This variable is global and should be reached everywhere in the code, isn't it? So, why cannot we change a value? and what i have to do if i want to use the same name inside the function, but for local variable? is it impossible?
Hi Petr, It is global if we define 'global z' inside the function. If not, Python creates a local variable z and sets that to 4. printing z after f(3,2) will show z is still 3.
Thus we have two variables 'z' in this case, one outside the function and one inside. To use the global z, we must define 'global z' on top of the function. To use a global variable z (no locals):
To summarize, if we do not define 'global z', the global variable is out of the scope of the function. I hope you enjoy the site!
Hi Frank,
When I run below program in https://repl.it/BG8w/2
I do get this result: local variable in function, z = 3. z is accessible in f(x,y). Why?
You can print the variable inside f(x,y), but if you try z=2 inside the function it throws an exception. The Python interpreter executes all code at the 0th level of indention, which is why you can print z. The value of z cannot be changed inside the function because it is not a global variable nor parameter.
Dear Frank,
i am trying this code and it is giving me error: method() missing 1 required positional argument: 'a'
please guide me what i am doing wrong in this code.
Hi Sadia, you are mixing OOP with functional programming
Try this:
Hi Frank,
please let me know why it is important to keep same name of list while passing to function. here is a simple function that receives list on call and then add. if i change the name of list "a" in method call then it gives me error. why it is not allowed to change. in java while passing arguments we can change name.
In your example you give the name in the function call. Try calling it with method([1,2,3])