Python Forum
It seems you have to define functions at the top
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
It seems you have to define functions at the top
#1
I wrote a program that defines two functions: one function squares an integer and the other function cubes it.

When I put the function definitions at the bottom of my code I got an error saying function square_number is not defined. So I cut it out and placed it at the top of my code window.

Then the program runs fine.

So in Python then does a guy have to put his function definitions at the very top of the code?

Reason I'm a little confused is because when I wrote this program in Java, I placed the function definitions at the bottom and the code worked there.
def number_squared (x):
    return x * x

def number_cubed(x):
    return x * x * x

j =  3
numberSquared =  0
numberCubed =  0

numberSquared = number_squared(j)
numberCubed = number_cubed(j)

print('The value of j is ' + str(j))

print('The value of j after calling square function is ' + str(numberSquared))
print('The value of j after calling cube function is ' + str(numberCubed))
Reply
#2
Names have to be assigned before they can be used. So if you're using them at the top level of your file, yes you must create the subroutine before it can be called.

However, sometimes it can be convenient to wrap what is currently top-level logic into a subroutine. Then that routine can be called from the end of the program after all the other subroutines have been created.

def main():
    j = 3
    numberSquared = 0
    numberCubed = 0

    numberSquared = number_squared(j)
    numberCubed = number_cubed(j)

    print("The value of j is " + str(j))

    print("The value of j after calling square function is " + str(numberSquared))
    print("The value of j after calling cube function is " + str(numberCubed))


def number_squared(x):
    return x * x


def number_cubed(x):
    return x * x * x


if __name__ == "__main__":
    main()
Reply
#3
My book I'm working out of would just call main() at the bottom. I noticed you added some stuff I don't know what it means.

if_name == " _main_";

I don't have a clue as to the why or what that is.
Reply
#4
What does if __name__ == "__main__": do?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
It looks a bit like noise at first, but it's a fairly standard idiom/boilerplate you'll see a lot.

When you run the program as a script directly (like python myscript) then it doesn't do anything different. Having main() at the bottom would do the same.

But sometimes you want to reuse some code. That's easily done by importing the script. This line prevents the code within from running during the import. It's a minor thing and you don't have to have it.
Reply
#6
(May-09-2023, 06:26 AM)357mag Wrote: My book I'm working out of would just call main() at the bottom. I noticed you added some stuff I don't know what it means.

if_name == " _main_";

I don't have a clue as to the why or what that is.


I've been wondering about this as well, but no one has answered.
buran write May-10-2023, 06:32 AM:
Hidden spam link removed
Reply
#7
Yes, buran answered that question
Reply
#8
One other point with function definitions - where you define does matter, as a function can be overridden.
def foo():
    print("original")

foo()

def foo():
    print("Override");

foo()
Output:
original Override
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to define two functions run simultaneously within a function? Alberto 4 4,073 Feb-06-2018, 10:08 PM
Last Post: Alberto

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020