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 is python function, the function syntax, types of function as well as we will see how to create 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 used with parameters and without parameters.
function in Python created using the def keyword. Python function return data as result.

Types of Python function

There are two types of function available in Python.

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

Syntax of function

The syntax of function is:-

def function_name:
	body of function

Create a function

To create a function in python, use def keyword.

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

Calling a function

To calling a function in python, use function name.

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

#Calling function	
myFunction()

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 passed into function during the calling of 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')

Output will be:- My name is Vishvajit Rao

Python function without parameter

Function in Python also can be used without parameter.

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

#Calling function without parameter    
Details()

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)
print(result)

Output :- 180

Arbitrary Arguments ( *args )

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

def Name(*name):
	print(name[2])
	
Name('John', 'Mariya', 'Alex', 'Kohli')

Output:- Alex

Arbitrary keyword arguments ( **kwargs )

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

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

Docstring

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?
To get docstring of the function uses 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)
print(result)
print(Calculator.__doc__)

Passing a List as an Argument

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

def myfunction:
  pass

Conclusion

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

If this article help you, please keep visiting for further python tutorials.

For more information:- Click Here

Python Tuple Tutorial
Python Lambda ( Anonymous ) Function

Was this post helpful?

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *