ADVERTISEMENT
ADVERTISEMENT

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?
Submitted by Shivang Yadav, on March 17, 2021

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.

Python Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.