Functions

subroutines, functions, methods, subprograms

An important idea in programming is called decomposition. Decomposition is the process of breaking down a problem into smaller pieces (subroutines) which are easier to deal with.

Decomposition is synonymous with the idea of dividing and conquering. Take something challenging, break it down and deal with the smaller components one at a time.

Here is a summary of the game we are making. Scratch the Cat will ask the user three questions. Scratch asks for the user’s age, favorite food, and favorite color. After the user answers the questions Scratch the Cat will restate their answers while visually responding by changing his fur color, in response to the user’s favorite color, his size, in response to the user’s age, and making food appear on the screen that represents the user’s favorite food.

As a reminder, while it is possible for the user to give nonsensical answers to these questions, our program will make no effort to correct for this. We have assumed that the user will only answer with one of three possible colors, three possible foods, and a number for their age. When the user gives invalid responses, our program may behave strangely.

Our program can be decomposed, or broken down, into five simple steps. The order of these steps may vary according to your preference.

  1. Ask questions and accept answers
  2. Restate answers
  3. Change fur color
  4. Change size
  5. Make food appear

It is very common in most programs to have an additional step at the beginning which will set the appearance of the program and the values of variables to some default state.

  1. Set initial values and appearance

Scratch, the programming language, allows you to create your own custom blocks. These blocks can be named anything you want and can complete any task you desire. All of the blocks built into Scratch actually contain their own, sometimes complex, code. In the case of the Play Sound block, there is a huge amount of complexity ultimately required to get sound to come out of your speakers. Having that complexity hidden, works to our advantage in many cases.

Let’s setup our custom blocks to accomplish the 5 chunks identified earlier. When finished, our program will appear very simple and the process of creating a solution will be much easier.

Custom blocks in Scratch are very similar to the idea of subroutines in other programming languages. Subroutines may also be referred to as methods, sub programs, functions, or routines. These all mean essentially the same thing. We are taking a small chunk of the program and focusing on the solution to only that one chunk. When finished, we can call this subroutine by name as often as we need.

The advantages of breaking a program into subroutines include:

  • Decomposing (breaking down) a complex programming task into simpler steps
  • Reducing the frequency of duplicate code within a program
  • Enabling the reuse of code throughout the current program and even across other programs
  • Dividing a large programming task among various programmers (people)
  • Hiding complex programming from the person calling the subroutine

Now watch this video. You may need to login first.

Complete program. ver. 0.3
This is what the completed program looks like. ver. 0.3

Need More? Here is an example Scratch program with subroutines and variables. See how it works.