Programming Functions

In math and in programming, functions are perhaps the most important concept. However, "functions" in programming are slightly different than "functions" in math. Recall that in programming, functions are like named blocks of code that can be called, and we can pass "arguments" to functions to customize the behavior. The code below shows an example of a function in JavaScript.

  1. function sayHello(name) {
  2. return `Hello, ${name}!`;
  3. }
  4. var message = sayHello("John");
  5. print(message);

Lines (1) - (3) define the function sayHello, which takes in a name parameter. The function will then use the value of name and return a string that says Hello, ${name}!`. Line (5) uses the sayHello function to create a message, and then line (6) prints the message. In this case, the sayHello function takes name as an input (or parameter) and then returns a value.

Math Functions

In math, functions behave a little differently and are actually a little less flexible. In math, a function is a relationship between two quantities, where the value of one quantity uniquely determines the value of the other quantity.

As an example, suppose xx and yy are related by the formula y=2xy = 2\cdot x. Given any value of xx, there is only one corresponding value of yy: the value of yy must be 2 times xx. Therefore we say that yy is a function of xx, and we could actually "name" this function. If we named this function ff then we could say that f(x)=2xwherey=f(x)f(x) = 2\cdot x \quad \text{where} \quad y = f(x) Then, any time we know a value of xx, we can "give" it to the function ff and ff will return the corresponding value of yy. If x=3.5x = 3.5 then we would write f(3.5)f(3.5) to represent the value of yy when x=3.5x=3.5, and hence f(3.5)=7f(3.5) = 7.

This should seem kind of similar to programming functions - you give the math function a value, and then the function gives you a value back. As another example, we could define a function gg as g(x)=x2. g(x) = x^2. If we know a value of xx, we can give that value to gg and then gg will square it and give us the squared value back. So, g(2)=4g(2) = 4.

Putting these together

Programming functions are more flexible than math functions, but this means that we can usually take a math function and use a programming function to represent it. Consider the function f(x)=2x f(x) = 2\cdot x from above. The code below shows how we can define a programming function f to represent this.

  1. // Define the function f
  2. function f(x) {
  3. return 2*x;
  4. }
  5. // Now let's use f
  6. print( f(3.5) );
  7. var a = 7.4;
  8. print( f(a) );

In lines (2) - (4) above, we define the function f. It takes a value of x and then returns 2*x. Make sure you understand what lines (7) - (10) are doing!


Programming functions are more flexible than math functions, which allows us to do more with them. One example of this is, we can write multiple steps within a programming function. For example, consider the math function h(x)=2x+5. h(x) = 2x + 5. This function takes a value of xx, doubles it, adds 5 to that, and then gives us the value back (this is based on the order of operations!). We can define a programming function for h as follows.

  1. function h(x) {
  2. var double = 2*x; // Double x
  3. var plusFive = double + 5; // Add 5 to double
  4. // Return the value
  5. return plusFive;
  6. }
  7. print( h(2) );
  8. print( h(3.5) );

In line (1) we accept a value of x. Line (2) then doubles that value of x and puts that value in a variable, and then line (3) adds 5 to that value. Line (5) then returns this final value.

As we'll see in later pages, this flexibility of programming functions gives us a ton of power!

Activities

Activity: Defining a function given a formula

Use the editor below to define a programming function to represent the math function f(x)=x3 f(x) = \frac{x}{3}, which takes a value of xx and returns xx divided by 3. Once you have defined the function, check to make sure that your function gives you f(12)=4f(12) = 4.

  1. print("Your code here.");

Activity: Defining a useful function

Suppose John is always 1.15 times as tall as Taylor. Define a math function hh that inputs Taylor's height in inches, xx, and outputs John's height in inches, f(x)f(x). Once you have done this, use the editor below to make a programming version of this function. Then, use this function to determine how tall John is when Taylor is 63.42 inches tall.

  1. print("Your code here");