How To Define, Use Functions in Python With Def? – POFTUT

How To Define, Use Functions in Python With Def?

Functions are fundamental feature of Python programming language. Functions provides reusability of code parts. Functions provides some abstraction from code. We can define some code block and use it with a single line without copy and pasting the whole code block.In this tutorial we will look how to define and use Python functions or methods.

Define Function with Def

def keyword is used to identify function start in python. After the def keyword we provide the function name and parameters. Parameters are provided in brackets ( .. ) . Parameters are separated with commas , . Parameters are optional and if we do not need them we can omit them. Functions definition ends with double dot : .

After the first line we provide function body or code block. Function body is indented to specify the body area. Here is the syntax of the function definition.


Now we will make an example to learn functions. We will create a functions where it has no parameter and only single line of code in its body or code block. The functions name will be myprint .

def myprint(): 
   print("I am working boss")

This is a simple for python but important for us.

Calling Function

Calling functions are easier than creating them. We will just provide the name of the function and add brackets end of this name. If we need to provide parameters we can put them into brackets.

In this example we will call the function, myprint , we have previously created. As it has no parameter we do not put anything into brackets.


Functions With Parameters

What makes functions strong is parameters. We can provide any number of parameters in any data type. Now create an example which have two parameters named a and b . Parameters type are integer. This function named is sum . This function will sum up given parameters.

def sum(a,b): 

Return Function

In previous example we have called the function and print some text to the output. Sometimes this is not we want. We may need to get or return some data but not put into output. We can use return in order to return data like string, integer, object, list etc. from functions. We will just put return into function with what we want to return.

LEARN MORE  C Variables and Definition

In this example we will not print sum and return from function and then print it.

def sum(a,b): 
Return Function
Return Function

Return generally put into the end of the function but there is no restriction about its location. We can use return multiple times. Multiple usage with if-else is popular.

Empty Function

Function must have code blocks or bodies to work  correctly. But in some situations we have not complete the function bodies but want to create functions. We can call these functions empty functions. We can use pass keyword to provide empty body which will do nothing.

def iamempty(name):   
Empty Function
Empty Function

As we can see there is operation or output from function named iamempty

Keyword Arguments

While providing arguments or parameters the sequence is important. We need to put parameters in required sequence according to the function definition. But there is alternative usage where we do not need to comply with sequence. We can also skip some parameters and use their default values. We will provide parameters with their keyword.

def sayhello(name,age):                  
   print("Hello {},{}").format(name,age)
Keyword Arguments
Keyword Arguments

We called function like below and all of them created same output.



Default Arguments

While calling functions we need   to provide all arguments or parameters to the function. If we  do not provide we  will get error. In some situations some parameters may be the same most of time and providing them in each function call is a trivial task. In order to make this type of usage more practical we can set some default values for arguments and while calling them if these parameters are not defined default values can be assumed by the function.

In this example we will assume name as adam if it is not provided. Default arguments are specified after normal arguments in function definition.

def sayhello(age,name='adam'):           
   print("Hello {},{}").format(name,age)
Default Arguments
Default Arguments

Variable Length Arguments

Up to now we have defined functions with specific number of arguments. We strictly provided the parameters. But sometimes this is not a solution and prevent us to work with multiple variable length arguments. We can use variable length arguments to address this. We will put variable name argument at the end of parameters and put asterisk left side like *var .

In this example we will create a function with variable argument.

def sayhello(name,age,*other): 
   print("Hello {},{}").format(name,age) 
   for var in other: 
Variable Length Arguments
Variable Length Arguments

We see that we loop all provided options arguments and print the to the terminal. The count of arguments is not important .

LEARN MORE  What is File System Block And Super Block?

1 thought on “How To Define, Use Functions in Python With Def?”

Leave a Comment