Web Development With HHVM and Hack 7: Functions

Mike Abelar
3 min readFeb 28, 2020

--

In the last tutorial, we covered dictionaries: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-6-dictionaries-f951c9a557f7. In this tutorial, we will cover functions in Hack.

What Are Functions?

Functions allow us to take code that serves a particular purpose and put that code in a dedicated block of code.

Examples of Functions

Let us consider the example of adding two numbers. Say that we have the following code (located in a file called functions.hack):

<<__EntryPoint>>
function main(): noreturn {
// code here
$sum = 5 + 4;
print($sum);
print("\n");
exit(0);
}

When we run the code (with hhvm functions.hack ), the code will print “9”. Currently, we add 5 and 4 and store the result in a variable called sum . The line of code for adding 5 and 4 serves the purpose of adding two numbers. Let’s see what happens when we take the same code and use it with a function:

function add_two_numbers(int $num_1, int $num_2) : int {
return $num_1 + $num_2;
}
<<__EntryPoint>>
function main(): noreturn {
// code here
$sum = add_two_numbers(5, 4);
print($sum);
print("\n");
exit(0);
}

Let’s first look at the declaration of the new function we made:

function add_two_numbers(int $num_1, int $num_2) : int {
return $num_1 + $num_2;
}

We can declare a new function by starting out with the function keyword followed by the name of the function: add_two_numbers .

We will notice that the function name is followed by parentheses and variables:

add_two_numbers(int $num_1, int $num_2)

These variables are called parameters. They serve as the inputs to our function. You will notice that each variable is proceeded by int . int stands for integer, which is the type of the variable. Types of parameters should be specified when the function is declared. Other types of variables can include floats, strings or booleans. We then separate the parameters by commas.

Now, let’s look at what follows the parentheses:

function add_two_numbers(int $num_1, int $num_2) : int

The : int specifies the return type of the function. The return of a function is the output that it produces, which is assignable to a variable. In this case, we return an integer. We will look at examples of other functions that have a different return type. You may also notice that our main function has the following declaration:

function main(): noreturn

This function has noreturn as its return type. This means that the function will not return anything.

Next, let’s take a look at the body of the function:

return $num_1 + $num_2;

The code will return the result of $num_1 and $num_2 . The return type of this expression, int, matches the return type of function. Remember, an integer plus and integer results in an integer. We return a value from the function using the return keyword. Note: no code should go after the return keyword. The return keyword marks the end of the function. A function is completed once it returns a value.

Other Examples Of Functions:

The following code concatenates two strings:

function concat_two_strings(string $string_1, string $string_2) : string {
return $string_1 . $string_2;
}
<<__EntryPoint>>
function main(): noreturn {
// code here
$combined_string = concat_two_strings("Hello ", "World");
print($combined_string);
print("\n");
exit(0);
}

The code prints: Hello World

Note: the use of string type for both parameters and return type.

The following code uses a function which prints a message twice:

function print_twice(string $string_to_print) : void {
print($string_to_print . "\n");
print($string_to_print . "\n");
}
<<__EntryPoint>>
function main(): noreturn {
// code here
print_twice("Hi!");
exit(0);
}

Note the return type void for the print_twice function. We can only use the noreturn return type when we include an exit(); function call at the end of the function, like in main . We use void if we do not return anything and do not call exit() .

The following code uses a function that takes no parameters and prints a message:

function print_a_message() : void {
print("Hey!\n");
}
<<__EntryPoint>>
function main(): noreturn {
// code here
print_a_message();
exit(0);
}

In this example, we see that print_a_message does not take in any parameters. Therefore, when we call the function, we can just leave the parentheses blank.

In the next tutorial, we will cover more loops: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-8-loops-dbe86a85df4

--

--