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

asked May 21, 2020 at 9:54
1

2 Answers 2

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)
answered May 21, 2020 at 9:57
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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
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.

answered May 21, 2020 at 10:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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