How to pass parameters to functions inside of fuctions

Question:

There is something in Python that has been bugging me for a long time.

I can’t figure out how to pass parameters from one function to the functions that are defined inside of that function.

I have tried researching this issue, but with no luck. Even W3Schools didn’t show a solution.

def func1(arg1):
    def func2(arg1):
        print(arg1)
    func2()
var1 = 123
func1(var1)

Here func1 and func2 should have the same parameters but don’t.

Asked By: Tetrapak

||

Answers:

Can’t you use it like this?

def func1(arg1):
    def func2(): <-- Removed parameter 
        print(arg1)
    func2()
var1 = 123
func1(var1)

Because when you are calling func2 inside func1, the arg1 in func2 is undefined since you passed no parameters; you should read about global and local variables in programming.

Answered By: Elio Rahi

You have only missed the argument in the call of func2. The code below highlights your forgetfulness:

def func1(arg1):

    def func2(arg1):
        print(arg1)

    # ---> here you have missed the argument
    func2(arg1)

var1 = 123
func1(var1)
Answered By: frankfalse