×

# Functions & Procedures

## Exam Questions

Exam questions on this topic

## Introduction

As we saw in the slides, a function in mathematics is a 'number machine' which takes some inputs, and gives an output. For example, ||f(x) = x^2|| takes an input ||x|| and outputs the square of this number, ||x^2||.
We can also have multiple inputs, for example, ||f(x,y) = 10x + y|| would multiply the first argument by 10 and add the second argument, so ||f(3,5) = 10\times3+5=35||.

Functions in programming (which are a type of subroutine) have the same underlying structure:

function squareIt(x) {
return x*x
}


Above, the function has an appropriate name which need not be 'f' (in this case squareIt) and an input variable (in this case x). The parameters (i.e. inputs) of the function go between the normal brackets.
The return statement says what we're going to output from the function. There could have been other statements before the return statement, making calculations for example related to what we're going to output. Recall that a procedure is a subroutine that does not return anything. We needn't have any parameters either!

The purpose of functions and procedures is to 'name' blocks of code that can be reused by multiple parts of your program. In the tasks below, we will practise using the syntax of functions in JavaScript, and clean up code so that repeated code only appears once.

## Mini-Task #1: Basic Function Syntax

Write an appropriate return statement for this function to have the effect of doubling the input.

Reset to Original Code     Fullscreen

Write a function that outputs the square of the maximum of the two numbers.

Reset to Original Code     Fullscreen

## Mini-Task #3: Calling an Existing Function

You have now been supplied a function factorial(n), which determines the factorial of a number, e.g. "4 factorial" ||=4! = 4\times3\times2\times1 = 24||. You should not modify this function. Write the code in meanOfFactorials(n) that takes an input n, and returns the mean average of ||1!|| up to ||n!||. You may wish to use a for loop.

Reset to Original Code     Fullscreen

## Mini-Task #4: Splitting Off Code Into Functions

Here is some code to list all the prime numbers up to the supplied input n. The code for determining whether a particular number is prime is quite useful, and you would like to make this code available for other functions to call. Separate the code for determining whether a number is prime into a separate function isPrime(n), and use your new function within the existing function listPrimesUpTo(n).

Reset to Original Code     Fullscreen

A perfect number is a number whose factors (including 1, but not including itself) add up to itself. For example, 6 is perfect number because its factors (including itself) are 1, 2 and 3, and ||1 + 2 + 3 = 6||. First write two functions, sumOfFactors(n), which finds the sum of factors of an integer n, followed by a function isPerfect(n), which uses this. Finally, update perfectDigitSums() (which takes no arguments) which lists the sums of the digits of each perfect number up to 10,000. For example, if your perfect numbers found happened to be 20 and 512 (which they aren't!), then you program should output [2,8], as ||2+0=2|| and ||5+1+2=8||.

Reset to Original Code     Fullscreen

## Mini-Task #6: Recursive Function Execution

It's possible to call a function from within itself. For example, recall that the factorial function is such that [m]4! = 4 \times 3 \times 2 \times 1[/m]. But we could also write [m]4! = 4 \times 3![/m]. Therefore in general, a 'recursive' definition of the factorial function is ||n! = n(n-1)!||, because the factorial function is defined in terms of itself, except with a smaller input.

We eventually need a point where the function stops referring to itself: ||4! = 4 \times 3! = 4 \times 3 \times 2! = 4 \times 3 \times 2 \times 1!||. The function would keep referring to itself for ever unless we had a 'base case', in this case we need to specify that ||1! = 1||.

Write a function factorial(n) which determines ||n!||, that makes use of recursive functions (i.e. refers to itself), and does not use any loops.

Reset to Original Code     Fullscreen

## Super-Hard Mini-Task #7: Recursive Function Execution Challenge - Arrangements

We want to code a function arrangements(ls) which inputs an array ls and outputs all possible orderings of those items. You can presume the items in the array are all different. For example arrangements([1, 2, 3]) would output [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]].

Use recursive function execution to achieve this. One strategy might be to consider the possible first item of the arrangement (using a for loop), and then putting this on the front of each possible arrangements of the remaining letters (using your function). You should consider the base case, i.e. what we output when we only have one letter left to consider.

Reset to Original Code     Fullscreen