...

>

08 - Functions

08 - Functions

JavaScript Functions

Creating Functions

Functions are primary building blocks of JavaScript. They allow us to write programs in a more efficient and scalable manner. Functions help us to manage complexity by containing and grouping operations under a single executable name. We already know how to call functions by using the p5.js predefined functions such as ellipse or background. We even created our own functions as p5.js forces us to put our code into two function declarations, setup and draw. If we wanted to create our own functions, we would be following the same convention that we have been using for the creation, or declaration, of these functions.

To create (or to declare) a new function, we would start off by using the function keyword and then give the function a name of our choosing that ideally describes the behaviour or purpose of the function.

	function functionName() {
		// function body
	}

Next, to the function name we would open brackets. If we want to build a function that works with user input, we can define parameters inside the brackets that would act as placeholder variable names for the future user input. We will see how this works in a bit.

Then we have curly braces. ...

Access this course and 1900+ top-rated courses and projects.