How do I call a Variable from Another Function in Python?


A variable is a way of storing values in the Python programming language so they can be used later in the program. These variables frequently find use within functions, necessitating the need to access a variable from another function. We shall examine Python's methods for calling a variable from another function in this article.

In Python, calling a variable from another function can be done in one of two ways −

  • Global Variables

  • Return Statement

Let's take a closer look at each of these techniques −

Global Variables

A global variable is a special kind of computer variable that can be accessed from any place in the program. Regardless of where the variable was initially declared, this property of global variables enables the retrieval of its values from any function inside the program. In other words, if you designate a variable as global in one function, you can access its value in any other function across the entire program.

Algorithm

A global variable can be called from another function by using the code snippet above.

  • Define the first function first_function() − This function is used to declare a global variable variable. The keyword global is used to specify that the variable is a global variable and can be accessed from anywhere in the program.

  • Assign a value to the global variable variable − The value "I am a global variable" is assigned to the variable variable.

  • Define the second function second_function() − This function is used to print the value of the global variable variable.

  • Call the first function first_function() − The first function is called, which declares the global variable variable.

  • Call the second function second_function() − The second function is called, which prints the value of the global variable variable.

Here's an example −

Example

def first_function():
   global variable
   variable = "I am a global variable"

def second_function():
   print(variable)

first_function()
second_function()

The output will be −

Output

I am a global variable

Use caution while utilizing global variables because they can be changed in any function.

Return Statement

An alternative method of calling a variable from another function is through the use of the return statement. A return statement is a valuable tool that allows for the passing of a value from within a function to an external location, such as a variable. Essentially, it enables the storage of the returned value from a function into a designated variable.

Algorithm

  • Define the first function first_function() − This function is used to return a value "I am a returned variable". The return statement is used to return a value from a function.

  • Define the second function second_function() − This function is used to store the returned value from the first function in a variable variable.

  • Call the first function first_function() − The first function is called and its returned value is stored in the variable variable.

  • Print the value of the variable variable − The value of the variable variable is printed using the print statement.

  • Call the second function second_function() − The second function is called, which executes the code inside the function.

Here's an example −

Example

def first_function():
   return "I am a returned variable"

def second_function():
   variable = first_function()
   print(variable)

second_function()

The output will be −

I am a returned variable

This approach is thought to be superior to utilizing global variables because it gives the user more control over the values transferred between functions and lowers the possibility of unintentional modifications to the variables.

Conclusion

To summarize, when it comes to calling a variable from another function in Python, there are two viable options available: utilizing global variables or employing the return statement. The preferred approach between these two options is contingent on the requirements of the specific scenario. However, it's commonly accepted that using the return statement offers greater control over the values being transferred between functions and is thus considered a more optimal method.

Updated on: 28-Aug-2023

33K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements