Quick Index
Introduction


Video


Here is a video that goes along with this page.

Overview


A drill to learn and/or refresh on function and functional programming basics.

Objectives



Preparing


Before doing this drill browse the theory and work through the examples in this chapter, especially Function Basics.

Requirements


The requirements for this drill are:


Deliverables


Your deliverables for this drill:


Provided Code


In this zip file you will find the starter file "function-basics-drill.txt":


Notes on Problems



Problems


Functions may be generally written in simpler abbreviated (Arrow) function format (unless otherwise noted).

Write a function that returns the result of tripling an input number

Function input: number
Function output: input tripled (a number)

Assign the function to a variable named "tripleNumFct".

Example Hand Calculation:

x = 10
tripleNumFct(x) = 3 * 10 = 30
Write a function to cube a number.

Function input: number
Function output: input cubed (a number)

Assign the function to a variable named "cubeFct".

Example Hand Calculation:

x = 3
cubeFct(x) = 3 * 3 * 3 = 27
Write a function that returns the last element in the input array.

Function input: array
Function output: last element in array

Assign the function to a variable named "getLastFct".

Example Hand Calculation:

array = [10, 20, 30, 40]
getLastFct(array) outputs 40

Hints:

  • array.size() returns the size of the array
  • Array indexes are zero-based (start at 0)
Write a multi-line function that returns the max of three numbers.

Function input: three numbers
Function output: a number which is the max of the input numbers

Assign the function to a variable named "maxFct".

Example Hand Calculation:

a = 10
b = 11
c = 3
maxFct(a, b, c) outputs 11
Write a multi-line function that returns the min number in an array

Function input: array of numbers
Function output: a number which is the minimum number in the input array

Assign the function to a variable named "minFct".

Example Hand Calculation:

array = [10, 5, -17, 3];
minFct(array) outputs -17
Write a function that returns the average length of a collection of strings

Function input: array
Function output: average length of the input collection of strings

Assign the function to a variable named "getAveStringLength".

Example Hand Calculation:

array = ['Asha', 'Chin', 'Mo', 'Acheampong'].
getAveStringLength(array) outputs 5
//total length=20, count=4

Hints:

  • aString.length returns the length of a string
Write a multi-line function that returns the factorial of an input number

Function input: number
Function output: a number which is the factorial of the input number

Assign the function to a variable named "factorialFct".

Also see: factorial on mathisfun.com

Example Hand Calculation:

num = 4
factorialFct(num) outputs 24
//4 * 3 * 2 * 1

Submitting