Assignment 1

Barry Molina

Functions

Functions are reusable chunks of code that can take input and produce output.

They allow you to tackle complex problems using functional decomposition.

PHP has many built-in functions. For example, str_repeat()repeats a string a certain number of times.

$laugh = 'ha';
$funny = str_repeat($laugh, 2);
$very_funny = str_repeat($laugh, 3);
$hilarious = str_repeat($laugh, 10);

echo "$funny that's funny.";
echo "$very_funny that's very funny.";
echo "$hilarious that's hilarious!";

Output:

haha that's funny.
hahaha that's very funny.
hahahahahahahahahaha that's hilarious!

You can also create your own functions

function make_me_laugh() {
  echo 'What do you call a boomerang that won\'t come back?';
  echo 'A stick.';
}

make_me_laugh();

Output:

What do you call a boomerang that won't come back?

A stick.
functions.php source code screenshot