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

Copyright © 2018 TeacherGaming. All rights reserved.

Lesson Preview

with

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

**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!**

- 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.

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**:

- Khan Academy: Algorithms
- WhatIs.com: Algorithm
- Encyclopaedia Britannica: Algorithm
- Dictionary.com: Algorithm
- Wikipedia: Algorithm

**WATCH**:

- "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)

**READ:**

- Math is Fun: What is a Function?
- Intuitive Calculus: Understanding Mathematical Functions
- University of Utah School of Computing: Functions
- Kids Codecs: Functions in Programming (for younger readers)
- The Wolfram Functions Site (hundreds of mathematical functions, a good source of examples)

**WATCH:**

Image Source: *Function x^2*, Wikimedia Commons.

**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
```

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.

**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.**

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

- 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.

- What would it mean for code if there were no functions?

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?

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*?

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.