Menu Close

Python Function Tutorial

python function tutorial

Here, You will learn about Python’s most important topic which is called Python function. Here we will see What the is Python function, the function syntax, and the types of functions as well as see the process of creating the function.

What is a function in Python?

Python function is a group of statements that are used to perform a specific task. Python function is executed when it is called. When you have large Python code, then you can use the Python function to break that program into smaller chunks. Python function is used with parameters and without parameters.
Function in Python created using the def keyword. Python function returns data as a result.

Types of Python function

There are two types of functions available in Python.

  • Python built-in function:- Python built-in functions come pre-installed When you install Python.
  • User-defined function:- A function, Created using the def keyword called a user-defined function.

Syntax of function

The syntax of the function is:-

def function_name:
	body of function

Creating a function

To create a function in Python, use the def keyword.

def myFunction():
	print("Python function")

Calling a function

To call a function in Python, use the function name.

# defining a function 
def myFunction():
	print("Python function")

# calling function	

The output will be:- Python function

Python function with parameter

Function in Python accepts arguments or parameters. Information can be passed into function through arguments or parameters. The value is passed into the function during the calling of the function.

# Function with parameters
def fullName(first_name, last_name):
	print("My name is {} {}".format(first_name, last_name))
#passing value at the time of calling
fullName('Vishvajit', 'Rao')

The output will be:- My name is Vishvajit Rao

Python function without parameter

Function in Python also can be used without parameters.

# Function without parameter
def Details():
	first_name = 'Programming'
	last_name = 'Funda'
	print("This is {} {}.".format(first_name, last_name))

#Calling function without parameter    

Output:- This is Programming Funda.

Return statement

The return statement is used to exit the function in Python and go back to the place where the function is called.

# function to calculate the multiplication of a and b 
def Calculator(a,b):
	#return statement to return the result.
	return a * b

#value is stored inside result
result = Calculator(12,15)

Output:- 180

Arbitrary Arguments ( *args )

Function in Python supports arbitrary arguments which means if you don’t know how many arguments you have then you can pass an asterisk ( * ) before the parameter name into the function definition.

def Name(*name):
Name('John', 'Mariya', 'Alex', 'Kohli')

Output:- Alex

Arbitrary keyword arguments ( **kwargs )

The function also supports arbitrary keyword arguments which means if you don’t know how many keyword arguments you have then you can pass a double-asterisk ( ** ) before the parameter name into the function definition.

def Language(**name):
Nmae(name1 = 'Python', name2 = 'Java', name3 = 'JavaScript')


The first string after the function header is called a docstring. The docstring is short for documentation string. Docstring is used to explain the function in brief, what function does do?
To get the docstring of the function use the magical method __doc__.

def Calculator(a,b):
	"This is function is used to calculate multiplication of two numbers"
	return a * b

result = Calculator(12,15)

Empty Python Function

The function can not be empty but sometimes you don’t want to write any code in the function for execution then you can use a pass statement. pass statement is a null statement that is generally used to avoid getting an error.

def myfunction:


So, In this tutorial, you have learned all about python function and their uses. function in Python is most used by Python programmers because using Python function, programmers break their large Python code into smaller parts and solve them.

If this article helps you, please keep visiting for further Python tutorials.

For more information:- Click Here

Static Method in Python
Python File Object Methods

Related Posts