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