Question
# header comments # the main function def main(): sayHi() sayBye() # function to say hi def sayHi(): print('Hi') # function to say bye def
# header comments # the main function def main(): sayHi() sayBye() # function to say hi def sayHi(): print('Hi') # function to say bye def sayBye(): print('Bye') # call main main()
At first glance, you might think that this script will have a problem. Why? (Rhetorical question) Because it looks like we're going to call the functions sayHi and sayBye before they are defined.
However, this works! Remember that defining a function doesn't perform the work that's described in the body of the function. It just bundles that work all together. So, when are those functions called? When main is called. And, that happens at the end of the script file. In other words, by the time that main iscalled, the sayHi and sayBye functions are defined. Everything works.
If we use the main function, we can define our functions in any order we'd like within the script. The only requirement is that main is called as the last thing within the script. Typically, this is the bottom line of the script. why the call to main is the last line of the script, rather than having one or more blank lines after the call to main?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started