🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #80

Python Functions


Python Functions

A function is a block of code which only runs when it is called.

A function can return data as a result.

A function helps avoiding code repetition.


Creating a Function

In Python, a function is defined using the def keyword, followed by a function name and parentheses:

Example

def my_function():
  print("Hello from a function")

This creates a function named my_function that prints "Hello from a function" when called.

Note: The code inside the function must be indented. Python uses indentation to define code blocks.


Calling a Function

To call a function, write its name followed by parentheses:

Example

def my_function():
  print("Hello from a function")

my_function()

You can call the same function multiple times:

Example

def my_function():
  print("Hello from a function")

my_function()
my_function()
my_function()

Function Names

Function names follow the same rules as variable names in Python:

  • A function name must start with a letter or underscore
  • A function name can only contain letters, numbers, and underscores
  • Function names are case-sensitive (myFunction and myfunction are different)

Example

calculate_sum()
_private_function()
myFunction2()

Note: It's good practice to use descriptive names that explain what the function does.


Why Use Functions?

Imagine you need to convert temperatures from Fahrenheit to Celsius several times in your program. Without functions, you would have to write the same calculation code repeatedly:

Example

  temp1 = 77
celsius1 = (temp1 - 32) * 5 / 9
print(celsius1)

  temp2 = 95
celsius2 = (temp2 - 32) * 5 / 9
print(celsius2)

  temp3 = 50
celsius3 = (temp3 - 32) * 5 / 9
print(celsius3)

With functions, you write the code once and reuse it:

Example

def fahrenheit_to_celsius(fahrenheit):
  return (fahrenheit - 32) * 5 / 9

print(fahrenheit_to_celsius(77))
print(fahrenheit_to_celsius(95))
print(fahrenheit_to_celsius(50))

Return Values

Functions can send data back to the code that called them using the return statement.

When a function reaches a return statement, it stops executing and sends the result back:

Example

def get_greeting():
  return "Hello from a function"

message = get_greeting()
print(message)

You can use the returned value directly:

Example

def get_greeting():
  return "Hello from a function"

print(get_greeting())

Note: If a function doesn't have a return statement, it returns None by default.


The pass Statement

Function definitions cannot be empty. If you need to create a function placeholder without any code, use the pass statement:

Example

def my_function():
  pass

The pass statement is often used when developing, allowing you to define the structure first and implement details later.

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python Functions – FAQs

Quick answers about learning Python Functions in Python.

This free note from CodingNow 2.0 explains Python Functions in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Python Functions, is 100% free with no signup required.
With focused practice, most students grasp Python Functions in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now