In any programming language, functions play a crucial role in breaking down complex problems into smaller, more manageable pieces. Python, being a high-level language, supports an extensive array of functions to cover a broad range of applications. Functions in Python allow you to define a set of instructions that can be called multiple times with different inputs, making your code reusable, modular, and easier to read. Functions also allow you to apply the DRY (Don't Repeat Yourself) principle, which means you can write a specific block of code once and use it many times throughout your program. In this tutorial, we will dive into everything you need to know about functions in Python. So, let's get started!

Table of Contents :

  • What is a function?
  • Why do we need functions?
  • Defining a function in Python
  • Calling a function in Python
  • Code Sample for using functions

What is a function?

  • A function in programming languages is a code block that has a name.
  • The name of this code block can be used to access it and reuse it from different parts of the program.
  • We have a detailed tutorial on functions in programming languages that explain in detail what are functions along with code sample.

Why do we need functions?

  • At times there are some tasks that need to be performed again and again throughout our program.
  • Instead of copying the same exact code to all places, we can create a function that performs this task.
  • This function can then be called by its name, from different parts of our program wherever we need this task.
  • In other words, a function is usually written to create a code block that performs a specific task and needs to be used repeatedly.
  • A function may return a value to the calling code after the task has been completed.
  • Functions make the code modular as it helps break larger programs into smaller parts.
  • Functions make the code easy to maintain and manage.
  • Functions are a great tool to implement code re-usability.

Defining a function in Python

  • The basic syntax of a function in python is as follows.

def calculate(a,b):
	print("Inside calculate")

  • A function has two parts :
    • Function Definition
    • Function Body
  • A function definition in Python can be broken down into these components :
    • def keyword
    • followed by function name
    • parentheses
    • parameters wihin parantheses if needed
    • semi colon to end the definition
    • The indented code block below the function definition is called the function body.

Calling a function in Python

  • To use the function in our program we need to call it.
  • Python interpreter executes the code inside the function every time a call is made to function.
  • To call a function somewhere in our code we need to 
    • write the function name
    • followed by parentheses
    • passing argument values within parentheses if needed.
  • Basic syntax of calling a function is :

s = calculate(x, y)

Code Sample for using functions

  • Below we have a code sample for using functions in python

# Defining function calculate
def calculate(a, b):
    res = a + b
    return res

x = 2
y = 4
# calling the function calculate
s = calculate(x, y)

print("The sum of the two numbers is : ", s)

# Output
# The sum of the two numbers is :  6

Prev. Tutorial : What are functions

Next Tutorial : Passing arguments