# Python Functions

Sometimes, I see functions that call other functions that are defined later in Codecademy projects. Like this:

func1 = func2() + 1 # func2() is not defined yet def func2(): return 1 print(func1(), func2())

That is okay but with variable its not okay like this:

func1 = func2 + 1 # func2 is not defined yet func2 = 1 print(func1, func2) # Note: the variable names don't match what they actually are; I used them like that because to make the comparison between the 2 codebytes easier

.
Why?

Also, I sometimes see that functions modify global variables without a `global` statement in the Codecademy projects.
Like this:

global_var = None def global_var_changer(): global_var = 1 print(global_var)

.

What? This doesnâ€™t change it but I have seen it before, look:
Lesson:

Code:

num_in_fibonacci = 9 function_calls = [] def fibonacci(num, memo): function_calls.append(1) if num < 0: print("Not a valid number") if num <= 1: return num elif memo.get(num): return memo.get(num) else: memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo) return memo[num] fibonacci_result = fibonacci(num_in_fibonacci, {}) print("Number {0} in the fibonacci sequence is {1}.".format(num_in_fibonacci, fibonacci_result)) print("Fibonacci function called {0} total times!".format(len(function_calls)))

.

What? `function_calls` is a global variable!

Umâ€¦ no it isnâ€™t:

Of course not. Why would it? Itâ€™s 2 separate variables that happen to have the same name declared in separate scopes.

`function_calls` is a name assigned to a list.

If you need further explanation, try reading the documentation, or reviewing the lessons.

3 Likes

The lessons didnâ€™t teach this even in the scope lessons in the Learn Intermediate Python 3 course.

Thatâ€™s why midliner used the word `or`.

What area in the documentation should I look at?

Scope and functions, perhaps?

No, it just says that functions can the global namespace by using `global`.

Yes? What else about functions and scope is troubling you? Midlinerâ€™s reply was pretty informativeâ€¦

I think you should look at my previous posts.

I donâ€™t understand this:

.

When you are appending something to that list, youâ€™re not actually reassigning anything; youâ€™re just adding that list (since lists are mutable). That means you can access something in global scope.

What else(data types) can you change from a function from a global scope?

I pretty sure that you can do a similar thing for any mutable data type, although that would be a good thing for you to experiment with yourselfâ€¦

1 Like

Ok so, do I open a workspace and set up some mutable data types and then declare a function which tries to change the data types and see if the data types are changed in the global namespace?

Sure, why not?

1 Like

Also, I want to try to convert it to C# but first I have to learn about methods in C#.

It may be worth mentioning that you didnâ€™t call the function in this case, though it wouldnâ€™t have accomplished the objective.

3 Likes

What do you mean? I donâ€™t understand.

He means, in the code you posted:

Nowhere do you actually call the function `global_var_changer()`, so how do you expect it to do anything?

1 Like