Python Functions – Functions are a way of keeping the code organized in a modular fashion. There are lot of benefits in doing so. Some of them are :
- An action that is required to be performed multiple times during the lifetime of application, could be encapsulated as a function for code re-usability, thus reducing the code length and redundancy.
- As functions are the result of an organized code, they help in maintenance and following up with the code easily.
def statement is used to define a function in python. Following diagram presents the syntax for a function in python, with all the required and optional elements.
def function_name ( parameters ): //body
Even thought its trivial, but some useful understanding could be drawn from the syntax, like
- Since Python is a dynamic language, there is no return type mentioned for a function. However, variables of any type or objects could be returned from a function. And whoever is calling the function should be aware of the variable type that could be returned. For this reason, it is always a good practice to have better and in-detail documentation for functions in your application. This helps in a quick and clear understanding of the task a function can do, when working in teams or making a time travel and revisiting the code that is written by you.
- parameters which are inputs for the task done by a function, could become optional if the function is always aware about location of data that is required or if the task doesn’t need any. Multiple parameters could be sent as inputs(arguments) to the functions separated by comma(,).
- function_name is the identifier for the function. Rest of the program or another programs could call this function using identifier, the function name.
- body is the set of statements that follow the colon(:) symbol in the definition of function. These set of statements collectively make the task(i.e., required action) and is the piece of code that is executed when the function is called.
Python provides many Built-in Functions like print() to print something to output, max() to find the maximum of the numbers passed as arguments to it, and there are many more functions like these which come with the standard python installation. The list of functions may vary from version to version. For a quick view to complete list of Built-in functions in python 3.6.1, visit https://docs.python.org/3/library/functions.html.
Python also provides programmer or developer the ability to code his/her own functions. These are called user defined functions. By following the syntax of python function that is already mentioned above, you could write your own functions required for the specific problem that you chose to solve.
There are many ways you can prepare your user defined function based on the parameters they have been defined with. We cover these scenarios in the following examples.
1. Basic Example for a Python Function
In this example program, we will define a function with the name
welcome and accepts no parameters. The function just prints a string to standard console output.
def welcome(): print('Welcome to www.tutorialkart.com for learning Python.') welcome()Try Online
Welcome to www.tutorialkart.com for learning Python.
Please note that when we call the function
welcome, we are using parenthesis after the function name. Also, we are not passing any argument(s) inside parenthesis, since function definition says that
welcome does not accept any arguments.
2. Python Function with Fixed Number of Parameters
In this example, we will define a function
multiplication with two parameters:
def multiplication(num1, num2): return num1*num2 result=multiplication(12,89) print(result) result=multiplication(198,389) print(result) result=multiplication(98,56) print(result)Try Online
1068 77022 5488
When we called multiplication function, we passed two arguments, since the function requires two arguments.
3. Python Function with Default Value for Parameter
In this example, we will write a function with with two arguments, and a default value for the second argument.
def multiplication(num1,num2=24): """num1 takes number num2 takes number, if nothing is provided, 24 is taken as default multiplication(num1,num2) returns the multiplication of num1 and num2 Ex: multiplication(2,7) returns 2*7=14 multiplication(2) returns 2*24=28)""" return num1*num2 result=multiplication(12)#num1=12, num2=24 : as no value provided for num2, 24 is taken as default value print(result) result=multiplication(198,389)#num1=198, num2=389 print(result) result=multiplication(98) #num1=98, num2=24 print(result)Try Online
288 77022 2352
Note : default arguments should follow non-default arguments. Failing to do so results in syntax error as shown in the example below :
def multiplication(num1=2,num2): """num1 takes number num2 takes number, if nothing is provided, 24 is taken as default multiplication(num1,num2) returns the multiplication of num1 and num2 Ex: multiplication(2,7) returns 2*7=14 multiplication(2) returns 2*24=48)""" return num1*num2Try Online
File "/home/arjun/PycharmProjects/PythonTutorial/functions/func_args_default_val_error.py", line 1 def multiplication(num1=2,num2): ^ SyntaxError: non-default argument follows default argument
4. Python Function with Arbitrary Number of Arguments
In this example, we will define a function that can accept any number of arguments.
To accept any number of arguments, provide *args as parameter.
def multiplication(*args): """multiplication functions works with args as list of numbers multiplication() returns the multiplication of list of numbers in args Ex: multiplication(2,7,3) returns 2*7*3=42 multiplication(3,4,4,5) returns 3*4*4*5=240)""" result=1 for i in args: result=result*i return result result=multiplication(12,89, 2, 57) print(result) result=multiplication(198, 38, 47) print(result) result=multiplication(8, 6) print(result)Try Online
121752 353628 48
In this Python Tutorial of Python Functions, we have learnt what a function is, the syntax for defining a function in python, a basic understanding on the elements presented in syntax, example python programs for some of the possible functions with different kinds of arguments.