Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to pass an array to a function in Python
Next article icon

How to write an empty function in Python – pass statement

Last Updated : 10 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

During the development of a Python program, we might want to create a function without implementing its behavior. In such cases, we can define the function and use the pass statement inside it to avoid syntax errors.

To write empty functions in Python, we use pass statement. pass is a special statement that does nothing. It only works as a dummy statement.

Empty Function with pass

Python
def fun():     pass  # Function logic to be added later  # Calling the empty function fun()  # This does nothing for now 

In this example, the function fun doesn’t perform any action because of the pass statement. However, it’s still defined and can be invoked like any other function.

Table of Content

  • Empty Function with pass:
    • Syntax for an Empty Function
  • What is the pass Statement?
    • Example with while Loop
    • Example with for Loop
    • Example with if-else Statement
  • Benefits of Using pass in Functions

Example with while Loop

A while loop repeatedly executes a block of code as long as the condition is true. If you want to create a while loop structure without implementing the logic right away, you can use the pass statement.

Python
i = 0 while i < 10:     pass  # Placeholder for future code     i += 1 

In this example, the while loop runs as long as i is less than 10. The pass statement indicates that the loop does nothing for now, but the loop still increments i.

Example with for Loop

A for loop iterates over a sequence of elements. You can use the pass statement to define a for loop without any operation for each iteration.

Python
for i in range(5):     pass  # Placeholder for future code 

Here, the for loop iterates over the range from 0 to 4, but it doesn’t perform any action because of the pass statement.

Example with if-else Statement

The if-else statement executes a block of code based on a condition. You can use the pass statement in both the if and else blocks to indicate that these blocks are empty.

Python
x = 10 if x > 5:     pass  # Placeholder for future code else:     pass  # Placeholder for future code 

In this example, the if condition checks if x is greater than 5. Both the if and else blocks contain the pass statement, indicating that no action is taken regardless of the condition.

Benefits of Using pass in Functions

  • Helps Maintain Code Structure: We can set up the skeleton of your program early on without having to worry about implementing every detail at once.
  • Keeps Syntax Valid: It prevents syntax errors when defining functions that have no body yet. Without pass, Python would raise an indentation error if the function body is left empty.
  • Facilitates Incremental Development: We can focus on high-level logic first and implement specific functionality in small steps.
  • Improves Readability and Collaboration: When working in teams, using pass clearly signals that a function is yet to be implemented, making the code easier to read and understand.


Next Article
How to pass an array to a function in Python
author
kartik
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

  • How to pass an array to a function in Python
    In this article, we will discuss how an array or list can be passed to a function as a parameter in Python. Pass an array to a function in Python So for instance, if we have thousands of values stored in an array and we want to perform the manipulation of those values in a specific function, that is
    4 min read
  • How to Break a Function in Python?
    In Python, breaking a function allows us to exit from loops within the function. With the help of the return statement and the break keyword, we can control the flow of the loop. Using return keywordThis statement immediately terminates a function and optionally returns a value. Once return is execu
    3 min read
  • How to Recall a Function in Python
    In Python, functions are reusable blocks of code that we can call multiple times throughout a program. Sometimes, we might need to call a function again either within itself or after it has been previously executed. In this article, we'll explore different scenarios where we can "recall" a function
    4 min read
  • How to detect whether a Python variable is a function?
    There are times when we would like to check whether a Python variable is a function or not. This may not seem that much useful when the code is of thousand lines and you are not the writer of it one may easily stuck with the question of whether a variable is a function or not. We will be using the b
    3 min read
  • How to call a function in Python
    Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them. In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "d
    5 min read
  • Difference between continue and pass statements in Python
    Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from
    3 min read
  • Pass a List to a Function in Python
    In Python, we can pass a list to a function, allowing to access or update the list's items. This makes the function more versatile and allows us to work with the list in many ways. Passing list by Reference When we pass a list to a function by reference, it refers to the original list. If we make an
    2 min read
  • How to use Function Decorators in Python ?
    In Python, a function can be passed as a parameter to another function (a function can also return another function). we can define a function inside another function. In this article, you will learn How to use Function Decorators in Python. Passing Function as ParametersIn Python, you can pass a fu
    3 min read
  • How to Add Function in Python Dictionary
    Dictionaries in Python are strong, adaptable data structures that support key-value pair storage. Because of this property, dictionaries are a necessary tool for many kinds of programming jobs. Adding functions as values to dictionaries is an intriguing and sophisticated use case. This article looks
    5 min read
  • How to Use a Variable from Another Function in Python
    Using a variable from another function is important for maintaining data consistency and code reusability. In this article, we will explore three different approaches to using a variable from another function in Python. Use a Variable from Another Function in PythonBelow are the possible approaches
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences