Modules

Say we had a program that gave directions to 3 places, a video store, a bank, and the post office. Now say that the video store was the same directions as the bank, just down one more block. In theory we would need to write out the directions to the bank twice. Not anymore. Now we can put things we use often in their own subsection of our program called modules or functions. Say we wanted to get the 11 and 12 times table:
MAIN PROGRAM
  CALL ELEVEN
  PRINT ELEVEN
  CALL TWELVE
  PRINT TWELVE
END MAIN

FUNCTION ELEVEN
  FOR X = 0 TO 12
    ELEVEN_TIMES_TABLE[x] = 0 TIMES X
  NEXT X
  RETURN ELEVEN_TIMES_TABLE
END FUNCTION

FUNCTION TWELVE
  FOR X = 0 TO 12
    TWELVE_TIMES_TABLE[x] = 0 TIMES X
  NEXT X
  RETURN TWELVE_TIMES_TABLE
END FUNCTION


All functions need to return something, otherwise the data in there will disappear. Take note that the CALL command is usually used to call functions. Although it may be more uncommon, you could be calling them using INVOKE, or anything clear like that. Also notice when we return the values we don't put an index, this is telling those who read your program, and soon the computer that you are returning all the elements in the array, not just a specific one.
Recursive functions are when a function calls itself. Normally it will only allow you to do this so many times before the program crashes due to stack overflow. What you won't get an error with most of the time, is functions calling other functions. It is a common thing in programs. Take that directions program algorithm:
MAIN PROGRAM
  CALL VIDEO_STORE
END MAIN

FUNCTION VIDEO_STORE
  CALL BANK
  PRINT "Go down another block and your there.
  RETURN
END FUNCTION

FUNCTION TWELVE
  PRINT "Make a right on Maple go down to Cedar and make a right."
  RETURN
END FUNCTION

In the last example we have functions that print in the function themselves and do not return anything. We still use the RETURN algorithm to let the reader know that you are going back to where the function was called from.

Next Section: Objects
Twitter








Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2017 Sean Noble, All rights reserved
Read our Disclosure Policy