Summary: In this tutorial, we will learn what a function is, types of functions, how to create a function in Python using the def keyword.
Introduction to Functions in Python
A function is a set of statements that are grouped together under a common name to perform a specific task.
Function implements code reusability because a function can be called multiple times to perform the same task without rewriting the code.
Some of the prominent features of functions in Python are:
- Makes the code reusable.
- Reduces the program complexity.
- Makes program design easy and less lengthy.
Types of Functions
Python has two types of functions:
- Built-In Functions: Functions that are already defined in Python e.g. max().
- User-Defined Functions: Functions defined by the users.
How to Create a Function in Python?
Following is the syntax to declare a function in python:
def name_of_function(): """ docstring """ <statements> #return desired result
The function declaration begins with a
def keyword then a space followed by the name of the function.
It is recommended to not to name a function same as a built-in function in Python (such as len()) otherwise that built-in function will be overridden and lost.
Next, comes a pair of parentheses with arguments separated by a comma. These arguments are inputs to the function. You can use these inputs inside the function for your task.
After this, a colon (:) to mark end of the function header.
The function body is indented and starts with an optional docstring.
docstring in a human-readable statement that describes what the function does. It serves no purpose to the logic of the program.
Then comes the one or more python statement which is the main part of the function and performs the task which the function is intended to do.
The function ends with an optional
Important: you must indent to begin the code inside your function correctly. Python makes use of whitespace to organize code. Lots of other programming languages do not do this, keep that in mind.
Python Functions Examples
Here are some examples of Python user defined function.
def say_hello(): """ This function output 'hello' """ print('hello') #call the function say_hello()
#Function accepting a argument def greeting(name): print('Hello ',name) #must call function with a proper argument value greeting('Pencil')
Examples with Return Statement
return inside a function is used for returning result back to the calling statement.
Here are some examples using the return statement.
def add(x, y): return x+y print(add(5,4))
def add(x, y): return x+y #can also store the returned value in a variable sum = add(5,4) print(sum)
Examples using Break Statement
Break statement in Python is often used to stop and exit out of the loop iteration.
Not Prime prime
def check_prime(num): for x in range(2,num): if num%x == 0: print('Not Prime') break if x == num-1: print('prime') check_prime(4) check_prime(11)
In this tutorial, we learned what is a function in Python, types of python function and how to create a user defined funtion in Python using def keyword.