0

Hello I have one question.I am reading book and the chapter is about function calling another function.there is given this code:

def now_say_it(content):
    print(content)

def say_something():
    what_to_say = "Hi"
    now_say_it(what_to_say)


say_something()

and there is said that "Note: The function that is called must be earlier in your code than the function that calls it." So it means that in def say_something() we are calling for now_say_it function right? as I understand in his note he says that this now_say_it function(def now_say_it) must be before(above) function say_something(def say_something).Am I right?

But when I write like this :

def say_something():
    what_to_say = "Hi"
    now_say_it(what_to_say)

def now_say_it(content):
    print(content)

say_something()

it still works.But why the author wrote that note? or I assembled code incorrectly? The book I am reading is "A smarter way to learn python" by Mark Myers(chapter 50: Functions within functions)

P.s.Please advise me some good books to learn Python

  • Does this answer your question? [Is it possible to forward-declare a function in Python?](https://stackoverflow.com/questions/1590608/is-it-possible-to-forward-declare-a-function-in-python) – deadshot May 21 '20 at 09:59

2 Answers2

1

What the author means is that you need to declare the function before calling it in chronological order. That is, this will work:

def say_something():
    what_to_say = "Hi"
    now_say_it(what_to_say)

def now_say_it(content):
    print(content)

say_something()

or, this as well, will work:

def now_say_it(content):
    print(content)

def say_something():
    what_to_say = "Hi"
    now_say_it(what_to_say)


say_something()

but this wont work:

def say_something():
    what_to_say = "Hi"
    now_say_it(what_to_say)

say_something()

def now_say_it(content):
    print(content)
Anshul
  • 1,413
  • 2
  • 6
  • 14
  • Basically, any function that is getting called, has to be declared/defined (def function_name(): ... ) first in the order of execution. As also recommended by other members here - if your goal is to move forward with learning to code, the best thing for you to do is to play around and identify and explore it yourself. – Anshul May 21 '20 at 10:46
  • so idea of this note is just to teach you correct way of writing in this language.before you call something you must already have ready this calling thing.but if it will be below for program it does not matter it will find but for human logic it is better to have above.something like that –  May 21 '20 at 10:57
0

Python is interpreted. Which means it reads the code line by line. The only statement of yours that is executable is this

say_something()

The other statements are only function definitions.

By the time the code comes to this point, the the two functions are already interpreted and exist. Hence it works.

You should try playing around this in a python shell. That will give you a better idea.

leoOrion
  • 1,833
  • 2
  • 26
  • 52