Python Functions

A Complete Overview of Python Functions:

In our previous tutorial, we discussed the types and uses of Control Statements in detail.

In this tutorial, we will discuss Python functions along with simple examples.

Do not fail to read through our complete range of Python Tutorials in this series.

Python Functions

Watch the VIDEO Tutorials

Function Arguments In Python: Video #1

Functions, Calling a Function & Return Statement in Python: Video #2

Functions

A function is a block of code which is used to perform some specific actions. A function provides higher modularity and code reusability.

Functions help to break a large code into smaller modules.

Syntax:

def function_name(parameters): 
#Block of code or statements

Defining a Function

  • Function block should always begin with the keyword ‘def, followed by the function name and parentheses.
  • We can pass any number of parameters or arguments inside the parentheses.
  • The block of a code of every function should begin with a colon (:)
  • An optional ‘return’ statement to return a value from the function.

Example:

def my_function():
       print(“Hello Python”)

Python - defining function

Simply defining a function is useless unless you call it.

Calling a Function

Once the structure of a function is finalized, you can execute it by calling the function using the function name.

Example:

def my_function():
       print(“Hello Python”)
my_function()

Python - calling function

Output:

Hello Python

calling_function_output

Calling a Function using Parameters

We can define any number of parameters while defining a function.

Syntax:

def my_function(parameters): 

#Block of code or statements

Example:

def my_function(fname):
       print(“Current language is: “, fname)

my_function(“Python”)
my_function(“Java”)

Python - calling function with parameters

Output:

Current language is: Python
Current language is: Java

Python - calling function with parameters - output

Return Statement

A return statement is used to return a value from the function.

Example:

def additions(a, b):
       sum = a+b
       return sum

print(“Sum is: “, additions(2, 3))

Output:

Sum is: 5

Python - return statement


Output:

Python - return statement - output

Function Arguments

In python, we can call a function using 4 types of arguments:

  • Required argument
  • Keyworded argument
  • Default argument
  • Variable length arguments

#1) Required Arguments:

Required arguments are the arguments which are passed to a function in a sequential order, the number of arguments defined in a function should match with the function definition.

Example:

def addition(a, b):
       sum = a+b
       print(“Sum of two numbers is:”, sum)

addition(5, 6)

Output:

Sum of two numbers is: 11

Python - required arguments

Output:

Python - required arguments - output

#2) Keyworded Arguments:

When we use keyword arguments in a function call, the caller identifies the arguments by the argument name.

Example:

def language(lname):
       print(“Current language is:”, lname)

language(lname = “Python”)

Output:

Current language is: Python

Python - keyworded arguments

Output:

Python - keyworded arguments - output

#3) Default Arguments:

When a function is called without any arguments, then it uses the default argument.

Example:

def country(cName = “India”):
       print(“Current country is:”, cName)

country(“New York”)
country(“London”)
country()

Output:

Current country is: New York
Current country is: London
Current country is: India

Python - default arguments

Output:

Python - default arguments - output

#4) Variable-length Arguments:

If you want to process more arguments in a function than what you specified while defining a function, then these type of arguments can be used.

Example 1:

Non – Keyworded argument

def add(*num): 
       sum = 0
       for n in num: 
              sum = n+sum
       print(“Sum is:”, sum)

add(2, 5)
add(5, 3, 5)
add(8, 78, 90)

Output:

Sum is: 7
Sum is: 13
Sum is: 176

Python - variable length arguments Example 1

Python - variable length arguments Example 1 Output

Example 2:

Keyworded arguments

def employee(**data):
       for(key, value in data.items()):
              print(“The value {} is {}” .format(key,value))


employee(Name = “John”, Age = 20)
employee(Name = “John”, Age = 20, Phone=123456789)

Output:

Name is John
Age is 20

Name is John
Age is 20
Phone is 123456789

Python- variable length arguments Example 2

Output:

Python- variable length arguments Example 2 Output

Conclusion

  • Functions help a large program to divide into a smaller method that helps in code re-usability and size of the program.
  • Functions help in better understating of a code for the users as well.
  • Using python input/output functions, we can get the input from the user during run-time or from external sources like text file etc.
  • If we want to write a huge number of data into a file then we can achieve it using Python file output methods.

Our upcoming tutorial will explain about the Input-Output and Files in Python!!

PREV Tutorial | NEXT Tutorial