JS - Functions (Variables, Parameters, Values)

0 votes
490 views
added Jul 10, 2018 in Javascript by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//Function is useful when you want to perform a similar action in various places of the script.
//You can learn the terminology of each keyword here: https://javascript.info/article/function-basics/function_basics.png

//function declaration
function myFirstFunction() {
  let firstSon = 'Adam', //local variables, declared in the function
      daughter = 'Fatimah',
      secondSon = 'Musa',
      myKids = `${firstSon}, ${daughter} and ${secondSon}`;

  console.log( `My first function dedicated to ${myKids}`);
}
myFirstFunction(); //The call myFirstFunction() executes the code of the function.  
myFirstFunction(); //Yes, I'm showing the function twice, that's why it shows twice

6 Responses

0 votes
responded Jul 10, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//outer variables
let myWife = 'Nadiah' || 'nadiah';

function hitoMyWife() {
  let sayHi = `hi ${myWife}`;
  console.log(sayHi);
}

hitoMyWife(); 
0 votes
responded Jul 10, 2018 by LC Marshal Captain (25,790 points)
//The function has full access to the outer variable. It can modify it as well.
let myTruck = 'Everest';

function showMytruck() {
  myTruck = 'Escape'; // (1) changed the outer variable
  let truckMessage = `Hey ${myTruck}`;
}
console.log(`${myTruck} as a default value of variable`); // Default variable, before function call
showMytruck(); // function call
console.log(`${myTruck} as a new value, after the function kicks in`); 
0 votes
responded Jul 10, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//We can pass random data to functions using parameters/function arguments 
//In the example below, the function has two parameters: who and dialog.
function adamMeetFriend(who, dialog) { // arguments: who, dialog
  console.log(who + ': ' + dialog);
}

adamMeetFriend('Adam', 'Hey mate!'); // Adam: Hey mate! (*)
adamMeetFriend('Adam', "How's it going?"); // Adam: How's it going? (**)
0 votes
responded Jul 10, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//Declare variable for parameter 
function showMessage(whoParam, dialog) {
  whoVar = whoParam // make "who" look nicer
  console.log( whoVar + ': ' + dialog );
}

//Declare a new variable that used by showMessage function 
let whoVar = 'Musa',
    dialog = 'Long time no see!';

showMessage(whoVar, dialog); // *Musa*: Long time no see!
// the value of "who" is the same, the function modified a local copy
console.log( whoVar ); // Musa
0 votes
responded Jul 10, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//A function can return a value back into the calling code as the result.
//The simplest example would be a function that sums 3 values:
function myKidsAges(adam, musa, fatimah) {
  return adam + musa + fatimah;
}

let result = myKidsAges(5, 1, 3);
console.log( `All age combined for my kids made the number of ${result} `); // 9
0 votes
responded Jul 10, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//There possibly many occurrences of return in a single function. e.g
function checkAge(yourAge) {
  if (yourAge > 18) {
    return true;
 } else {
    return confirm("Ask your parent's permission?");
    // return false;
  }
}

let yourAge = prompt('How old are you?', 5);

if ( checkAge(yourAge) ) {
  alert( 'Access granted, Enjoy!' );
} else {
  alert( 'Access denied, Sorry' );
}
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...