Defining Javascript Functions
Overview
- Prerequisite Topics
- What is a Function?
- What is an Input?
- What is an output_value?
return
statement- contrasting
return
andprint
- Using a Function
What is a Function?
- A function is a block of reusable instruction(s).
- A function relates an
input
to anoutput_value
. - The intent of a function is to re-execute the same lines of in different areas of a program.
- Functions provide better modularity for an application
Function vernacular
- The function signature is the first line of a function definition.
- A function signature is denoted by the keyword
function
, followed by the name of the function, followed by parenthesis()
, followed by a{
. - A code block begins with an open curly bracket,
{
, and encapsulates several lines of code, followed by a closing curly bracket,}
- A function definition is a function signature and its respective code block.
- A function call is the execution of a function.
// this is outside the scope of the of the function code-block
function myFunctionName() { // this is the function signature
output_value = "Hello world"; // this is the beginning of the function code-block
print(output_value);// this is the ending of the function code-block
} // this is outside the scope of the of the function code-block
Function Parameters
- Input parameters are denoted by the parenthesis
()
- In simple examples, an input is typically thought to be a numeric datatype or textual datatype
- though, any type is valid as an input.
// this is outside the scope of the of the function code-block
function myFunctionName(input_value) { // this is the function signature
output_value = "Hello " + input_value; // this is the beginning of the function code-block
print(output_value); // this is the ending of the function code-block
} // this is outside the scope of the of the function code-block
return
- The
return
statement exits a function, optionally passing back an expression to the caller.- The expression passed back to the caller is named
- Omitting a
return
statement is the same as explicitlyreturn
ingNone
// this is outside the scope of the of the function code-block
function myFunctionName(input_value){ // this is the function signature
output_value = "Hello " + input_value; // this is the beginning of the function code-block
return output_value; // this is the ending of the function code-block
} // this is outside the scope of the of the function code-block
return vs print
- In contrast to
print
statements,return
statements allow a system’s state change to be observable in a programmatic way - In contrast to
return
statements,print
statements allow a system’s state change to be observable in a human-readable
sing a function
- To make use of a function, you must make a call to the function.
- Calling a function only requires cognizance of the function signature: the name and parameters of the function.
- You do not need to know how a function is defined to make a call to a function
- Below is an example of a function definition followed by a call to the function.
function greet(username) {
let output = "Hello " + username
console.log(output);
}
greet("Leon");
greet("Hunter");
- Script Output
Hello Leon
Hello Hunter