2

When you call a function you must define it first, otherwise you will get an error message. But when you call a functions by another function it's okay to define a fuction after that, for example:

def repeat_name():
    print_name()
    print_name()

def print_name():
    print('Mahmud')

repeat_name()

In this example we call print_name function in repeat_name function before its defination, and it works fine, but why does this happen?

Note: I'm a beginner in Python.

1 Answer 1

1

Think of it being interpreted as it is run, so the items inside the repeat_name function are not checked until the first time it is called at repeat_name() after the print_name function.

If you put the call to repeat_name() above the print_name function it would also fail.

That is why if you have the code:

def my_func(x):
    print(x)

my_func("hello world")

It doesn't freak out that it doesn't know what x is, becasue x will be defined when you first call that function my_func("hello world")

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I understand now. Functions' definitions have no effect on the execution, so when I call repeat_name function I actually call print_name function twice, is that right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.