text: The first thing that one must know with functions is that they are not essential. One can still write their
  program without any functions, however when debugging all that unsorted code can be very confusing, especially
  for beginners.
  One should use functions in their programs because they allow one to break each part of one's program into
  managable chunks.
  The syntax for a function is as follows:
  - Code: Select all
  def NAME(PARAMETERS): # Don't forget that colon, alot of people do. :)
  """Purpose of Function""" # Indent 4 spaces like everything else.
  STATEMENTS
  The first thing that we can see is the 'def'. This tells python that this is a function, and will skip over it
  untill it is called.
  The next thing here is the name of the function. This should be clear, and memorable. I highly recommend that
  one never has a function name that is the same as the name of a global variable. One can, but it is considered
  to be bad form, and is very confusing for one as the programmer.
  Next we have the parameters of the function. It is customary for parameters to be single letters that
  represent what needs to go into your function. The values of these parameters are specified when the function
  is called
  This is optional, but it is a very good idea to state the purpose of one's function in triple quotation marks,
  as when programming you can see what the function is supposed to do. You may be wondering why we did not simply
  leave a comment with '#'. We use the quotes because the purpose of the function can be summoned, whereas a
  standard comment has no effect at all.
  Next we have the statements. These will manipulate the values and change them. You know, just print commands
  and stuff.
  Lets move on to some examples, and some common mistakes.
  - Code: Select all
  def squarenum(n):
  """Return the square of any number"""
  return n ** 2
  print(squarenum(5))
  This type of function is called a fruitful function, because the function returns a value. Now alot of people
  make this mistake in functions like these, so read carefully.
  Alot of novices will do this:
  - Code: Select all
  def squarenum(n):
  """Return the square of any number"""
  result = n ** 2
  print(result)
  This will return an error. Why? Because the 'result' is what is called a local variable. It is removed when the
  function ends, so when this person tries to print 'result' they get an error saying it was not defined. The way
  that will work is shown first, where I print the function where I used the return command.
  Another common error is when people do not use the names of parameters in the body of their functions, but use
  the names of a global input variable, which was earlier defined as a parameter.
  Here is an example:
  - Code: Select all
  def squarenum(n):
  """Return the square of any number"""
  return number ** 2 # Another dumb mistake. :P
  number = int(input("Please put a number"))
  print(squarenum(5))
  So that was my tutorial on functions. I hope this will help you make and improve your own programs!