Home » Python Tutorial » Python Functions

Python Functions


Reading Time - 9 mins

In this article, we will discuss Python functions.

What is a function

In any programming language, a function is defined as:

  • as a block code that is executed only when its called.
  • which may accept input.
  • which may return output as a result.

Create a Function in Python

In python, a function can be created using the def keyword followed by the name of the function.

def my_function():
  print("Hello")
  • def – keyword to denote that its a function.
  • my_function – the name of the function which can be any name
  • () – where we can pass the arguments(inputs) to the function. In the above function, we are not passing any arguments, so there is an empty parenthesis.
  • The code follows the def keyword at the same indent level will be executed when the function is called.

In the above example, we are creating a function called ‘my_function’ which will print ‘Hello’, when the function is called.

Call a Function in Python

If you execute the above code, nothing will get printed because we are not calling the function yet. Since we are not passing any arguments to the function, we are calling the function with empty parenthesis.

def my_function():
  print("Hello")

my_function()

Pass Arguments to Python Function

Sometimes, we need to pass additional information to the function in the name of the arguments. The arguments are given inside the parenthesis.

def my_function(name):
  print("Hello"+' '+name)

my_function()

The above function is accepting a variable called ‘name’ as an argument. If a function is accepting input parameters, we need to mention that while calling the function too. Else it will throw an error. Execute the code and check the error. We can understand from the error that we need to pass a value for the variable ‘name’ while calling the function.

def my_function(name):
  print("Hello"+' '+name)

my_function("Mary")

We can pass as many arguments to the python function followed by a comma.

def my_function(fname,lname):
  print("Hello"+' '+fname+' '+lname)

my_function("Mary","Smith")

The arguments can be of any datatype.

def my_function(l):
  for i in l:
    print(i)
  
my_function([1,2,3,4,5])

The arguments can be a combination of data types.

def my_function(fname,lname,l):
  print(fname+' '+lname)
  for i in l:
    print(i)

my_function("Mary","Smith",[1,2,3,4,5])

We still have a few more things to be covered on Python functions.

READ  Python Pass, Break and Continue

Returning value from Function in Python

We can return the output of a function using the return keyword.

def my_function(fname,lname): 
  name=fname+' '+lname
  return name

my_function("Mary","Smith")

The above code won’t print anything because we are returning the value but not capturing it anywhere. In order to capture the value returned, we can either use:

print function in Python

def my_function(fname,lname): 
  name=fname+' '+lname
  return name

print(my_function("Mary","Smith"))

Saving the return value to a variable

def my_function(fname,lname): 
  name=fname+' '+lname
  return name

output=my_function("Mary","Smith")
print(output)

We can save the return value to a variable and use it wherever it’s required.

Return  multiple values in Python

We can return multiples values from a function by returning the values separated by a comma.

def my_function(l): 
  l.sort()
  smallest_number=l[0]
  largest_number=l[-1]
  return smallest_number,largest_number

smallest_number,largest_number=my_function([7,8,2,11,20])
print(smallest_number,largest_number)

In the above example, we are passing the list as an argument to the function and returning the smallest and largest number as an output. In order to get the smallest and largest number, we are sorting the list in ascending order so the first element will be smallest and the last element will be largest.

Advantages of Function in Python

To understand the advantage of using the function lets consider the below example:

def my_function(l): 
  l.sort()
  smallest_number=l[0]
  largest_number=l[-1]
  print(smallest_number,largest_number)

my_function([7,8,2,11,20])
my_function([9,1,4,2,7])
my_function([50,30,100,20,250])

In short, we can say that the above function accepts the list as an argument and prints the smallest and largest in the list.

We are calling the function 3 times with different input. And the output is printed according to the input. How we would have achieved without functions?

l1=[7,8,2,11,20])
l2=[9,1,4,2,7])
l3=[50,30,100,20,250]
l1.sort()
smallest_number=l1[0]
largest_number=l1[-1]
print(smallest_number,largest_number)
l2.sort()
smallest_number=l2[0]
largest_number=l2[-1]
print(smallest_number,largest_number)
l3.sort()
smallest_number=l3[0]
largest_number=l3[-1]
print(smallest_number,largest_number)

We can say that in the above code there is a lot of repetitive lines and it looks big.

READ  Python Numbers

Whenever there is a repetitive code, we can replace it with a function and call it wherever it’s needed. And using functions make the code looks small and easy to understand.

References

Reference1

Python Operators

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions