If you want to reuse code, you can use a function.
This prevents you from writing the same thing over and over again.
A function has a unique distinct name in the program. Once you call a function it will execute one or more lines of codes, which we will call a code block.
Related Course: Complete Python Programming Course & Exercises
What are Functions?
A function is code. You can use functions to divide your program in “blocks of code”. This makes your code more readable and your life easier. You have likely already used one or more functions, such as the print function:
print("Hello world") |
A function can be called more than once:
print("Coffee") |
Python Function
The syntax for making a Python function is:
def <function_name>([<parameters>]): |
The meaning is:
deftells Python you want to create a functionfunction_nameis the identifier, it must be uniqueparametersoptional list of parameters that can be passed in the function:end of function header<statement(s)lines of code. This is called the function body. It must have four space indention.
It’s possible to create both simple and complex functions.
Start with a small example, we define a function f() and call it:
def f(): |
A function can be called as many times as you want, so this is possible:
f() |
A function body can have many lines of code:
def hello(): |

If you are a Python beginner, then I highly recommend this book.
Function parameters
Functions can take arguments. They are positional, the order should remain the same.
The example below takes a single argument:
def hello(name): |
To take multiple arguments, add a comma between each of them.
def f(name,salary): |
Then you can call it like this:
f("Alice",80000) |
The function parameters in the function call must remain in the same order as the definition.
If you have this function:
def f(a,b,c): |
Then you must call the function as f(a,b,c) not f(a,c,b) or f(c,b,a).
They must be the same number of parameters. So not f(a,b) or f(a,b,c,d).
The pass statement is an empty function body.
Default parameters
You can set a default parameter. This is set when you don’t define any argument.
f(6) |
If you have multiple parameters, the same idea is true:
def f(x=5,y=4): |
Function scope
Variables you create in the function body only exist within it.
If you create a function with a variable inside it:
def f(): |
Then that variable can only be accessed within the function body.
If you try to access it outside the function body, it will throw an error:
print(x) |
So function variables live inside the function body.
If you want to use the function variable in your program, you must use return.
Return value
It is also possible to store the output of a function in a variable. To do so, we use the keyword return.
The function sum(a,b) returns the output.
def sum(a,b): |
Without the return statement, variable x would only exist within the function body. The variable name need not be the same, in this case we store the output as variable cost.

A function can return more than one variable:
def f(a,b,c): |
Variable-Length Argument Lists
By default, Python functions take a fixed number of arguments. That is fine for most functions, but sometimes you want the number of arguments to be of variable length.
# takes 2 arguments |
So you must call the function with the same number of arguments.
What if you want a variable-length?
Then you can use a list:
def f(x): |
This gives you the freedom to call it with any “number of parameters”:
f([1]) |
Docstring
You can add documentation to your function. The first string after the function header is the documentation string. You can output it with the __doc__ attribute.
This way you can request what a function does:
print(print.__doc__) |
If you want to create your own docstring you can do this:
def hello(): |
Then output it like this:
print(hello.__doc__) |
A docstring is optional.
Documentating code is a good practice in general. If you have large complex programs, you should add documentation at all time.
The main() function
In some Python scripts, a main function is defined. Something like this:
def main(): |
The main function is optional, your programs will run without it.
The program starts in the main() function. This is common in other programming languages (C, C++).
The line if __name__ == "__main__": is True if the program starts, so main() is called.
This main function is common in Python files that are executed as a script and imported in another module.
Conclusion
In this tutorial you learned:
- What Python functions are
- How to define Python functions
- Calling Python functions
- All about function arguments
If you are a Python beginner, then I highly recommend this book.

You got a small typo below the first block of code. I think it's supposed to be 'y=3'. :)
Thanks Sebastian!
How do you do addition and subtraction?
By the way I am a beginner
When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three e-mails with the same comment. Is there any way you can remove people from that service? Cheers!
Removed your mail address, should be good now. Let me know if you still get mails