Total Beginner Stuck on Function Definition

This is the place for queries that don't fit in any of the other categories.

Total Beginner Stuck on Function Definition

Postby Peanut » Thu Feb 28, 2013 10:49 pm

Firstly, Hello!

Aside from a couple of days playing around now, I am a total beginner with Python, and have never learned any other programming language before.

I am working my way though How to Think Like a Computer Scientist but am stuck with a problem on Function Definition.

This is what the part of the book says:

The first couple of functions we are going to write have no parameters, so the syntax looks like this:

Code: Select all
def new_line():
    print          # a print statement with no arguments prints a new line


This function is named new_line. The empty parentheses indicate that it has no parameters. Its body contains only a single statement, which outputs a newline character. (That’s what happens when you use a print command without any arguments.)

Defining a new function does not make the function run. To do that we need a function call. Function calls contain the name of the function being executed followed by a list of values, called arguments, which are assigned to the parameters in the function definition. Our first examples have an empty parameter list, so the function calls do not take any arguments. Notice, however, that the parentheses are required in the function call:

Code: Select all
print "First Line."
new_line()
print "Second Line."


The output of this program is:


Code: Select all
First line.

Second line.




However, I am entering
Code: Select all
>>> def new_line():
   print "First Line."
   new_line()
   print "Second Line."

   
>>>

And nothing is happening...

Then if I enter

Code: Select all
>>>new_line()


It produces over 900 lines of "First Line." Then gives an error about maximum recursion depth...

Please help!

I am really enjoying learning so far, but know so little that it is confusing.

Thanks,

Peanut
Peanut
 
Posts: 3
Joined: Thu Feb 28, 2013 10:32 pm

Re: Total Beginner Stuck on Function Definition

Postby Yoriz » Thu Feb 28, 2013 11:09 pm

Hi,
The reason nothing is happening when you do just
Code: Select all
>>> def new_line():
   print "First Line."
   new_line()
   print "Second Line."

   
>>>

is because you have defined the function but not called the function.
Then when you enter
Code: Select all
>>>new_line()

that is calling your function but you also have a
Code: Select all
new_line()

inside the function itself.
This means you call the function which calls the same function which calls the same function ....... which loops and loops until python decides your calling the same function inside of a function too many times giving you the error.
Indentation is important you have all your code inside the function when really you want the call to new_line() not to be inside the function.
Compare the books code
Code: Select all
def new_line():
    print          # a print statement with no arguments prints a new line

print "First Line."
new_line()
print "Second Line."

to your code
Code: Select all
>>> def new_line():
   print "First Line."
   new_line()
   print "Second Line."

>>>new_line()


See this tutorial about indentation
Edit: actually that tutorial doesn't give you much info in your case, by the way the error your getting 'recursion' is what its called when a function calls its self, sometimes this can be wanted behaviour but it needs a way to stop it endlessly looping inside itself, but dont worry about them till later on in your learning.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1049
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Total Beginner Stuck on Function Definition

Postby Peanut » Thu Feb 28, 2013 11:26 pm

Hi Yoriz,

Thank you for the quick answer. It is good to know there are people with the patience to answer such basic issues as mine. Unfortunately I expect most of my posts will be questions for a while, until I know enough to be able to actually discuss the subject.

Peanut
Peanut
 
Posts: 3
Joined: Thu Feb 28, 2013 10:32 pm

Re: Total Beginner Stuck on Function Definition

Postby Yoriz » Thu Feb 28, 2013 11:28 pm

Hey,
Its no problem, i hope it makes a bit more sense now, feel free to come back and ask more questions :D
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1049
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Mekire and 1 guest