Skip to content
Advertisement

Why does my program run without parameters inside the functions’ parenthesis?

I am going to ask probably a very silly question. I designed a code that looks like the following:

import pandas as pd 
import seaborn as sns
import matplotlib.pyplot as plt

def menu():
   print("[1]: Favorite color") 
   print("[2]: Number of cats")

#Option1
def FavColor():
   sns.countplot(x= "Color", data = main_data)
   plt.show()

#Option2
def NumCats():
   sns.countplot(x= "Cats", data = main_data)
   plt.show()

#Main Code

try:
   main_data = pd.read_csv("DATAFIN.csv")
except IOError:
   print("Error while trying to open the file")
else:
   menu()
   option = int(raw_input())
   if option == 1:
         FavColor()
   elif option == 2:
         NumCats()

main_data looks like this:

    Color   Cats
1    Blue      1
2    Yellow    2
3    Blue      2
4    Red       1
5    Blue      3

Now, the “problem” is that my program runs exactly as it should. I just realized, however, that all of my functions, options(), Option1() and Option2(), do not have parameters inside the parenthesis. This is due to me forgetting to do so, but my understanding was that my functions should not work without me giving them the parameters that needed to be used.

For example, if Option1 had to use a precise dataframe I should have written Option1(dataframe).

Everything that my functions need to you is defined in the main code, which you go through before calling the functions, so maybe that’s the reason. But is it wrong not to put the parameters inside? Does it make my program less efficient?

Advertisement

Answer

You just happen to be referring to a global variable

Here’s a simpler example

def foo():
    print(x) 
    
x = 2
foo() 

x could be defined before or after the function; the function definition captures the closure of the variable reference, not the variable itself

On the other hand, if you defined a main function with local variables, it wouldn’t work

def foo():
    print(x) 
    
def main():
    x = 2
    foo() 

main()

In general, global variables should be avoided, and therefore parameters should be used, which are just references and the overhead of the memory space allocated for function parameters is negligible

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement