This is the 15th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Thank you for meeting me. Hi, I’m Ken

Author: Please call me Ken link: juejin.cn/user/109118… Source: gold mining copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.

🌊🌈

Part of the content and pictures of this article are from the Internet. If you have any questions, please contact me (there is an official account in the introduction of the homepage).

This blog temporarily suitable for just contact JS and long time do not see want to review partners

🌊🌈 About:

📚4.1_ Initial functions

📙4.1.1_ function use

functionThe function name () {
// The function body code
}
Copy the code

Function is the keyword to declare a function. All lowercase letters must be used.

When a function is declared, the code inside it is not executed, only when the function is called. The syntax for calling a function is “function name ()”.

Case study:

// Declare the function
function sayHello() {
console.log ('Hello');
}

// Call the function
sayHello();
Copy the code

📗4.1.2_ parameter to the function

The parameters of a function are divided into parameters and arguments.

When you declare a function, you can add arguments, called parameters, in parentheses after the function name.

When a function is called, it also needs to pass parameters called arguments.

The parameters of a function are formal arguments, because when the function is declared, the function has not yet been called, and it is not clear what values will be passed. The arguments are the actual arguments when the function is called. Its value is determined.

Case study:

functionThe function name (parameter1The parameter2.) {
// The function body code} Function name (argument1That argument2,...). ;Copy the code

JavaScript function arguments are very flexible, allowing a function to have a different number of parameters and arguments. When the number of arguments exceeds the number of parameters, the function executes normally. Any arguments that are not received are ignored unless they are obtained in a different way (e.g. arguments). When the number of arguments is less than the number of parameters, the extra parameter is like a declared, unassigned variable whose value is undefined.

function getSum (num1, num2) {
console.log (num1, num2);
}
getSum (1.2.3); // if the number of arguments is greater than the number of parameters, the output is 1,2
getSum (1); // The number of arguments is less than the number of parameters, the output is 1,undefined
Copy the code

📘 return value of the 4.1.3_ function

For example, if a person goes to a restaurant to eat, we think of the chef of the restaurant as a function, and the customer tells the cook what to cook through the parameters of the function. When the cook has prepared the meal, it should eventually be passed on to the customer. But the functions we’ve written in the past all output the result directly, which is like a cook eating his own food without returning the result of the function execution to the caller.

Format:

functionThe function name () {
returnThe value to return;// Return a value to the caller
}
Copy the code

Case study:

function getResult() {
return Awesome!;
}

// Receive the return value through the variable
var result = getResult();
console.log (result);   // Output: 666
// Output the return value of the function directly
console.log ( getResult() );// Output: 666

console.log (getResult);// Output result: output function (see figure below)
Copy the code

Example:

function getResult() {
console.log (1);
// This function has no return
}

getResult(); // Output: 1
console.log ( getResult() ); // Output: 1 undefined
Copy the code

📚4.2_ Function return value case

📙4.2.1_ Use the function to find the maximum value of any two numbers

function getMax (num1, num2) {

if (num1 > num2) {
return num1;
} else {
returnnum2; }}console.log ( getMax(1.3));// Output: 3
Copy the code

or

function getMax (num1, num2) {
return num1 > num2 ? num1 : num2;
}
console.log ( getMax(1.3));// Output :3
Copy the code

📗4.2.2_ Use functions to maximize any array

To review how to create an array:

【 stay up late fierce heart ten thousand words blog 】 schoolgirl asked me how to get started with Javascript, after much questioning, I finally decided to contribute their own Javascript introduction notes (3)

function getArrMax(arr) {
var max = arr[0];

for(var i = 1; i <= arr.length; i++) {
if(arr[i] > max) { max = arr[i]; }}return max;
}

var arr = getArrMax([5.2.99.101.67.77]);
console.log(arr);  // Output: 101
Copy the code

or

function getArrMax(arr){
var max = arr[0];

for(var i = 1; i <= arr.length-1; i++) {
if(arr[i] > max){ max = arr[i]; }}return max;
}
console.log( getArrMax( [5.2.99.101.67.77]));// Output :101
Copy the code

📘4.2.3_ Prematurely terminates functions with return

Code after the return statement is not executed. For example, the getMax() function determines whether both arguments are numeric, and returns NaN ahead of time as long as one of them is not.

function getMax(num1,num2){
if(typeofnum1 ! ='number' || typeofnum2 ! ='number') {return NaN;
}
return num1 > num2 ? num1 : num2;
}
console.log( getMax(1.'3'));// Output result :NaN
Copy the code

NaN(Not a Number) is a type of value of the numerical data type in computer science that represents an undefined or unrepresentable value. Null, undefined, and number are the basic data types.

📒4.2.4_ Returns an array with return

When using a return in a function, it is important to note that return can only return one value. Even if multiple values are separated by multiple commas, only the last value is returned.

function fn(num1, num2){
return num1, num2;
}
console.log( fn(1.2));// Output :2
Copy the code

In development, when you need to return multiple values, you can do this with an array, that is, you want to return multiple values in an array, returned as a whole.

function getResult(num1, num2){
return [num1 + num2, num1 - num2, num1 * num2, num1 / num2];
}
console.log( getResult(1.2));// 输出结果: (4)[ 3, -1, 2, 0.5 ]
Copy the code

That’s the end of today’s introductory study

Peace

🌊🌈

Akken’s HTML, CSS guide for getting started (1)_HTML basics akken’s HTML, CSS guide for getting started (2)_HTML page elements and attributes Akken’s HTML, CSS guide for getting started (3)_ Text style attributes Aken’s HTML, CSS getting started guide (four)_CSS3 selector Aken’s HTML, CSS getting Started guide (five)_CSS box model Aken’s HTML, CSS getting Started guide (six)_CSS box model Aken’s HTML, CSS getting Started guide (seven)_CSS box model Akken’s HTML, CSS guide for getting started (eight)_CSS box model akken’s HTML, CSS guide for getting Started (nine)_ Floating and positioning Akken’s HTML, CSS guide for getting started (ten)_ Floating and positioning Akken’s HTML, CSS Guide for getting started (eleven)_ Floating and positioning Ken’s HTML, CSS guide for getting started (12)_ Form application of HTML, CSS guide for getting started (13)_ form application of HTML, CSS guide for getting started (14)_ Form application of HTML, CSS guide for getting started (15)_ Form application Ken’s HTML, CSS guide for getting started (16) _ Multimedia technology

🌊🌈 About postscript:

Thank you for reading, I hope it can help you if there are flaws in the blog, please leave a message in the comment area or add contact information in the personal introduction of the home page

Original is not easy, “like” + “comment” thanks for supporting ❤