Function Structure

The syntax of a function is actually quite simple (much simpler than a for loop), as shown in Listing 8.1. The syntax is different for each language, but the building blocks are mostly the same. A function is declared to be a function using a specific keyword, such as function, def, or sub. A function is given a name, which should describe the task the function performs. The function’s parameters are stated and given names. Finally, a function has a group of instructions called a function body. If the function returns a value, the body contains a return statement.

Listing 8.1 The Function Syntax in JavaScript

function countToTen( /* parameters go here */ ) {  /* code goes here, in the function ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.