COSma Coding

COSma Coding

COSma Learning

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.

```
function sayHello(name) {
return `Hello, ${name}!`;
}
var message = sayHello("John");
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 \(x\) and \(y\) are related by the formula \(y = 2\cdot x\). Given *any* value of \(x\), there is only one corresponding value of \(y\): the value of \(y\) must be 2 times \(x\). Therefore we say that \(y\) is a function of \(x\), and we could actually "name" this function. If we named this function \(f\) then we could say that \[f(x) = 2\cdot x \quad \text{where} \quad y = f(x) \] Then, any time we know a value of \(x\), we can "give" it to the function \(f\) and \(f\) will return the corresponding value of \(y\). If \(x = 3.5\) then we would write \(f(3.5)\) to represent the value of \(y\) when \(x=3.5\), and hence \(f(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 \(g\) as \[ g(x) = x^2. \] If we know a value of \(x\), we can give that value to \(g\) and then \(g\) will square it and give us the squared value back. So, \(g(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) = 2\cdot x \) from above. The code below shows how we can define a programming function `f`

to represent this.

```
// Define the function f
function f(x) {
return 2*x;
}
// Now let's use f
print( f(3.5) );
var a = 7.4;
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.\] This function takes a value of \(x\), 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.

```
function h(x) {
var double = 2*x; // Double x
var plusFive = double + 5; // Add 5 to double
// Return the value
return plusFive;
}
print( h(2) );
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) = \frac{x}{3}\), which takes a value of \(x\) and returns \(x\) divided by 3. Once you have defined the function, check to make sure that your function gives you \(f(12) = 4\).

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

Activity: Defining a useful function

Suppose John is always 1.15 times as tall as Taylor. Define a math function \(h\) that inputs Taylor's height in inches, \(x\), and outputs John's height in inches, \(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.

`print("Your code here");`