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 »

Related Programs

Comments and Discussions!

Load comments ↻





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