Functions

In programming, functions are like blocks of code that are executed when they are asked to do so. As a great example, the print() command we have been using is a function -- when it is called, whatever is inside of the parentheses of print() is printed to the output.

Functions are perhaps the most important idea in both mathematics and programming! We will be using functions constantly throughout this site, and you will eventually be defining and using your own functions! When you use a function, you write the function's name, followed by parentheses with optional "arguments" inside of them. In the statement print("Hello world!");, print is the name of the function being executed and we say that "Hello World!" is the argument of the function.

The editor below shows some functions being used. Try adjusting the arguments of some of the function calls. If you change the name of the functions, you will probably get an error!

// Using the sin() function
print( sin(3) );

// Using the gcd() function
var x = gcd(125, 15);
print(x);

Defining functions

This site has some built-in functions for you to use, but there might be times when you want to define your own function. Here's an example: suppose that, for a bunch of numbers, you need to double the number and then add 5. You could define a function to do this, and then whenever you want to perform this task - you can just call this function. The editor below shows this actual function.

function f(x) {
return 2*x + 5;
}

print( f(2) );
print( f(3) );
print( f(7) );

Let's make sense of the code in the editor above. In lines (1) - (3) we are defining a function named f. In this case, x is a parameter of f. Whenever we use f, we give it a value for x (such as the f(2) in line 5). In line (2), we are returning a value from the function f - this is the value we get back whenever we call f.

If we look at line (5), we are calling the function f and using 2 as an argument. When f is called, the parameter/variable x is set to 2, and then whatever is inside the curly braces {} of the function is called. In this case, x = 2 and the function will return 2*2 + 5 = 9.

In the editor above, what happens if we change the "2*x + 5" to "3*x" in line (2)? Think about it, and then try it out!

Let's look at one more example of defining and using a function. Let's define a function named sayHello that takes a name as an argument, and then prints out "Hello, [name]!". Check it out!

function sayHello(name){
print(Hello, \${name}!);
return;
}

sayHello("Grant");
sayHello("Alan");

Notice that in the code above, the function sayHello() prints a message, but it doesn't return a value! So, whenever you call sayHello(), the function will print a line and then stop.

Activities

Activity: Doubling Numbers

Define a function named doubleNumber that takes a number x as its argument and returns the value value that is two times as large as x. Then, use this function to print the double of 3, 4.5, and 8.44.

print("Your code here");