Lesson Preview

Teach Algorithms and Functions
with AlgoBot

This is a printable preview of one of our lessons. Subscribe to gain access to this and 180+ other lessons using 35+ games, and track your students' progression in real time! Learn More

This is a printable preview of one of our lessons. Subscribe for more great content! Learn More

Theory

Algorithms and Functions

Today's lesson takes a closer look at functions and how they are used in math and programming!

Teacher Resources

  • Theory: This lesson takes a closer look at functions and continues the topic of algorithms.
  • Play: Students continue playing AlgoBot.
  • Share & Discuss: These tasks feature functions and using them in algorithms.

If your students need to use pen and paper to design their algorithms, download the JPG below to print a handy worksheet!

Algorithms

  • An algorithm (noun) is a set of steps to accomplish some manner of task. (source)
  • The concept of algorithm has existed for centuries. Use of the concept can be ascribed to Greek mathematicians. (source)
  • A good example of an algorithm in real life is the case of giving instructions on how to open a door to someone who has never seen a door. Walk up to the door, lift your hand and grab the device protruding from the door (handle), pull down on the device, push (or pull) the door.

Teacher Resources

In other words: "A computer program can be viewed as an elaborate algorithm. In mathematics and computer science, an algorithm usually means a small procedure that solves a recurrent problem." (source)

READ:

WATCH:

Functions in Math and Programming

  • "Every programming language lets you create blocks of code that, when called, perform tasks. Imagine a dog that does the same trick only when asked. Except you do not need dog treats to make your code perform. In programming, these code blocks are called functions." (source)
  • In other words, in programming, "Functions are "self contained" modules of code that accomplish a specific task. Functions usually "take in" data, process it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be "called" from the inside of other functions." (source)
  • An example of a really simple function is "multiple by 2" (expressed as f(x) = 2 * X in mathematics)

Visual representation of a function.

Teacher Resources

Example of Code (C++)

Input:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double number, squareRoot;
    cout << "Enter a number: ";
    cin >> number;

    // sqrt() is a library function to calculate square root
    squareRoot = sqrt(number);
    cout << "Square root of " << number << " = " << squareRoot;
    return 0;
}

Output:

Enter a number: 26
Square root of 26 = 5.09902

Teacher Resources

This example (source) is of using a built-in function in C++ programming (a programming language).

Here, the above code provides the square root of a number that is entered into the command line.

Play

Lesson Goal

Continue playing AlgoBot from last time.

Complete levels up to level 2-9. If you manage to complete level 2-9, try looking for the most efficient solution in a level you've already played.

Gameplay screenshot

Teacher Resources

If your students are having trouble with a specific puzzle, click here to download a PDF which includes all the solutions to the game!

Tips

  • Having trouble determining what's best to include in a function? Try looking for the section of the level that recurs most. Your code can be far more efficient if the most common sequence is placed within the function.
  • If you have trouble figuring out a level, try completing it in small segments. First determine the five first steps and run the program, then the next ones, and so on.

Share & Discuss

Share & Discuss

  • What would it mean for code if there were no functions?
Show Notes

This would mean that every time there was a recurring set of commands in the code, you would need to write it down and not be able to recall it (as you would with a function) or use the old go to command. This would make code far more difficult to read, even though the program would basically work the same.

  • Is it possible to complete levels in AlgoBot without using the functions? Why? Why not?
Show Notes

In many cases the answer is no. This is because AlgoBot has a limited number of commands it can follow (and no go to command), so you will simply run out of space if you attempt to complete levels without the functions.

  • How do you determine what is the best algorithm for a function in AlgoBot?
Show Notes

Usually, this is a fairly straightforward process: the sequence of code that repeats the most is the best candidate to be used as a function. Determining functions becomes more difficult later on when you have up to three separate functions available.