In chapter 12 of Python Tutorial, we will learn about Functions
If you are reading this tutorial on Mobile, please rotate your mobile horizontally for best reading experience. You can enable auto-rotate in your phone settings.
In any programming language, there will be instances where you may need to execute certain code/task multiple times. So, instead of writing this code/task again in our program, we can define that code/task as a function and call that function whenever we want to execute it.
Also, by using functions, we can divide our code into useful blocks, keep our code in an order and make it more readable. This will save lot of your time!
There are three types of functions in Python:
- Built-in functions, such print() to print an object to the terminal, input() to take input from the user. You can find out all the available built-in functions here.
- User-Defined Functions, which are created by us(users) based on our requirements
- Anonymous functions, this function don’t use standard keyword when they are declared. These are also called as lambda functions
As you are already aware about few of built-in functions, in this tutorial, we will learn about how we can define and use our own functions which are nothing but User-Defined functions. Let’s get started!
To define a function in Python, we use the def keyword and then followed by the function’s name. The following code shows the syntax to define a function in Python:
def user_function():
print("Hello, I defined a function")
In the above example, we have defined a function named user_function(). Did you click on “Run” to get an output?
Well, there will be no output yet that is because we have defined the function only. To get output, we need to call the function.
Calling a Function:
def user_function():
print("Hello, I defined a function")
#calling the function
user_function()
We may add arguments to the functions. These arguments are passed from the caller to the function. Let’s look at an example:
def user_function_with_args(name, age):
print("Hello %s, are you %d years old?" %(name,age))
#calling the function with arguments
user_function_with_args("Alex", 28)
There are two type of arguments, Positional Arguments and Keyword Arguments.
In the above example, the arguments in the function are called Positional Arguments.
The first argument “Alex” in the function call is used as the value for first parameter “name” and the second argument “28” is used as value for second parameter “age”. Here, the order of the arguments in the function call is critical as they should match the parameters.
In Keyword Arguments, we specify parameters and it’s associated values in the function itself. Here, the order of arguments in the function call does not matter as long as the names of the parameters are correct. Let’s look at an example:
def user_function_with_args(age=28, name="Alex"):
print("Hello %s, are you %d years old?" %(name,age))
#calling the function with arguments
user_function_with_args()
Please note the order of the arguments is not followed in the above example but still, we got the output we wanted.
Return statement is used to exit the function, go back to the function caller and return the value or data to the caller.
def add_numbers(x,y):
addition = x + y
return addition
#calling the function with return statement
result = add_numbers(3,4)
print("Result of the adding two numbers:", result)
The return statement may consist a variable, an expression, or a data which is returned at the end of the function. If none of the above is present with the return statement a None object is returned.
Assignment:
Write a function to get the result of subtraction and multiplication for two numbers
#write your code here
def two_numbers(x,y):
subtract =
multiply =
return subtract, multiply
#calling the function with return statement
subtract,multiply =
print("Result of the subtraction is %d and multiplication is %d" %())
#write your code here
def two_numbers(x,y):
subtract = x - y
multiply = x * y
return subtract, multiply
#calling the function with return statement
subtract,multiply = two_numbers(10,5)
print("Result of the subtraction is %d and multiplication is %d" %(subtract,multiply))
That brings to the end of chapter 12 of Python tutorial, see you in the next tutorial!!!
By the way, if you haven’t installed Python and Pycharm IDE on your Windows laptop, you can visit the below links to learn on how to install and configure/install Python and Pycharm IDE on Windows OS. This might be helpful in understanding the Python language easier.
How to Install Python 3 and Pycharm IDE on Windows
How to configure Pycharm IDE and “Run” your first Python 3 program
For your reference, to download Pycharm IDE, visit below link:
https://www.jetbrains.com/pycharm/download/?section=windows
However, if you don’t want to go through installation process, you can practice your learnings on this page itself by using integrated Python IDE above.
If you have any questions or comments about chapter 12 of Python tutorial, please let us know in the comments section below.