Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass parameters to functions inside of fuctions

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.

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading