Please enable JavaScript to use CodeHS

CodeHS Glossary


Define a Function JavaScript

Defining a function means to write a function with all of the code inside it. Defining a function **does not** run any of the code inside that function. It only teaches the computer what that function means. You can actually run that code by "[calling][1]" the function. ---------- **Example** The way you define a function is Javascript is: function nameOfMyFunction() { // function body: code goes here } The function definition starts with the word `function` followed by the name of the function. At the end of the name, be sure to include the `()` to complete it. All of the code for the function goes in between the `{` and `}` That section is called the "function body." ---------- **Defining vs. Calling** Defining a function is different from "[calling a function][2]." When you "call" a function, you just write the name of the function followed by `();` like this: nameOfFunctionToRun(); This will actually run the code inside that function, whereas defining the function **does not** run the code. It just defines the name. ---------- **Teaching Karel a New Word** In Karel, you can think of defining a function as teaching Karel a new word. You can teach Karel any new word you want using and of the other words that Karel already know, including other words you have taught Karel. [1]: http://codehs.com/glossary/term/12 [2]: http://codehs.com/glossary/term/12