DONATE  
MENU
HTML
AJAX
PHP
MYSQL
JSON
CSS
JAVASCRIPT

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Functions » Functions' Parameters
Share Tweet WhatsApp

Functions' Parameters

Functions' parameters afford us the opportunity to pass values to functions for execution when calling such functions. Since functions can be invoked to do what they were defined for just anytime, most of the time, we won't be needing it to use same values in doing what it does. For example, if we create a function to add two numbers, it will continue to give the same result anytime it is called. This is where parameters come in. With parameters, we can then have an opportunity to pass different values to the function to add anytime we want to use it. So, parameters are variables declared in a function parentheses which can be used inside the function. We assign values to these parameters(variables) when calling the function.
//DEFINING A FUNCTION WITH PARAMETERS
function addNum(num1, num2){ //num1 and num2 are parameters
}

//CALLING THE FUNCTION WITH VALUES FOR THE PARAMETERS
addNum(4, 5); //will add 4 and 5
addNum(10, 2); //will add 10 and 2
<script>
function add(num1, num2){
alert(num1+num2);
}

add(2, 4);
add(4, 1);
</script>

Copy PRACTICE IT

From the above, you can conclude that having parameters in a function is more like creating a function that will not only do what it was created for but does it just the way you want.
<script>
//DEFINING FUNCTION
function stack(level){
alert("stack "+level);
}

//CALLING FUNCTION
stack("beginner");
stack("developer");

</script>

Copy PRACTICE IT

A function can have as much parameters as you want. Default Values You can assign default values to parameters especially those, the function depends on for proper execution. This will ensure when no argument is assign to a parameter during invocation, it uses its default value instead of being undefined and producing unexpected results.
<script>
function addNum(x,y){
x = (x === undefined? 0 : x);
y = (y === undefined? 0 : y);

alert(x+y);
}

addNum(); //No arguments
addNum(3,4); //Arguments available
</script>

Copy PRACTICE IT

This is same as,
<script>
function addNum(x,y){
if (x === undefined){
x = 0;
}
if (y === undefined){
y = 0;
}

alert(x+y);
}

addNum(); //No arguments
addNum(3,4); //Arguments available
</script>

Copy PRACTICE IT

In the above, we assigned 0 to a parameter if it is undefined or no argument was assigned to it. SUMMARY
  1. The parameters of a function are the variables declared in the parentheses (()) of a function whose values can be assign when calling the function.
  2. Parameter(s) enables us to get different results from a function whenever we invoke it and assign different values to the parameter(s).
  3. A function can have as much parameters as you want.
  4. You may decide to assign default values to a parameter especially important ones so that when no argument is assign to them, the function uses the default values.
Take a short quiz on Functions' Parameters.
Ask a question or tell us if Functions' Parameters is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode