Skip to content

Global function Python

  • by

Python Global function can define using the def keyword followed by the name of the function and the parameters it takes (if any). A global function in Python is a function that can be accessed from anywhere in a program, without the need to import it or specify its location within the code.

def my_function():
    print("Hello, world!")

Global function Python example

A simple example code set_value is a global function that sets the value of my_var, which is a global variable. get_value is another global function that returns the value of my_var. Both functions can be called from anywhere in the program.

def set_value(val):
global my_var
my_var = val

def get_value():
return my_var



Global function Python

Do comment if you have any doubts or suggestions on this Python function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *