×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python program to call a function before declaring it - Is it possible?

Here, we will see the answer to what happens when we call a function before declaring it in Python? By Shivang Yadav Last updated : January 05, 2024

Functions in Python

Functions are blocks of code (multiple program lines) that can perform a specific task like finding area.

Calling a function

A function needs to be called in order to execute the code at the required position.

Is it possible to execute a python function before declaring it?

No! It is not possible. Python does not allow calling of a function before declaring it like C. This is possible in some languages like JavaScript but not in Python.

Calling will lead to errors, lets see what happens

Program to call function before declaring

# Function Calling
hi()

# Function Definition
def hi():
    print("Hi")

Output

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    hi()
NameError: name 'hi' is not defined

This means Python does not allows calling before declaring.

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Advertisement
Advertisement

Related Programs

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.