UX Design and Development course

A function with arguments

Playing with functions - a mini program that can be re-used

First we need to declare a 'function' and give it a name. The () are what hold the parameters of the function.

function myFunction(myArgument, nextArgument) {
  // This is called the 'Decloration'
  console.log('Oh snap! I LOVE ' + myArgument + ' and '  + nextArgument + '</br>');
  // Simple alert box with text
}

Another way of writing this is by using a recursive function

A function that calls itself is called a recursive function. In some ways, recursion is analogous to a loop. Both execute the same code multiple times, and both require a condition (to avoid an infinite loop, or rather, infinite recursion in this case). For example, the following loop:

var myFunction = function foo(myArgument, nextArgument) {
  // This is called the 'Decloration'
  console.log('Oh snap! I LOVE ' + myArgument + ' and '  + nextArgument + '</br>');
  // Simple alert box with text
};

Call your function and pass in the arguments:

myFunction('Bacon', 'Cheese'); // calls the function automatically into action
myFunction('Star Wars', 'Darth Vader');
myFunction('Beer', 'Code');

Play with standard function code

JS Bin

Play with recursive code

JS Bin