当前位置:网站首页>Functions (arguments, formal parameters, bubbling)
Functions (arguments, formal parameters, bubbling)
2022-06-12 03:15:00 【No two love letters】
One 、 Use of functions :
1、 Declare functions
function Function name (){
// The body of the function
}
2、 Transfer function
Function does not call , Don't do it yourself .
Function name ();
Such as sayHi();
Two 、 Calculation 1-100 Between numbers and
function getsum() {
var sum = 0;
for (var i = 0; i <= 100; i++) {
sum += i;
}
console.log(sum);
}
getsum();3、 ... and 、 Formal parameters and actual parameters
function Function name ( Shape parameter 1, Shape parameter 2.....){
// The body of the function
}
Function name ( Actual parameters 1, Actual parameters 2....);
Inside the parentheses of the declared function are the formal parameters , Inside the parentheses of the function call are the arguments
A formal parameter is a parameter that receives an argument , A formal parameter is similar to a variable
function cook(aru) {
console.log(aru);
}
cook(' sour and spicy shredded potatoes ');Four 、 The quantity matching problem of arguments and formal parameters
1、 If the number of arguments is the same as the number of formal parameters , Then the normal output results
2、 If the number of arguments is greater than the number of formal parameters , Then the number of formal parameters will be obtained
3、 If the number of arguments is less than the number of formal parameters , More than formal parameters are defined as underfined, The end result is NaN
5、 ... and 、 The return value of the function
return sentence :
function Function name (){
//return The results that need to be returned ;
}
Function name ();
Our function just implements some function , The final result needs to be returned to the caller of the function, the function name () adopt reurun Realized
As long as the function encounters return Just take the results later Return to the caller of the function
function getResult() {
return 666;
}
getResult(); //getResult()=666
console.log(getResult());1、 Find the sum of two numbers
function getSum(num1, num2) {
return num1 + num2;
}
console.log(getSum(1, 2));2、 Compare the size of two values
function getMax(num1, num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
console.log(getMax(1, 3));3、 Compare the size of two values ( Ternary method )
function getMax(num1, num2) {
return num1 > num2 ? num1 : num2;
}
console.log(getMax(1, 3));4、 Find the maximum value of an array
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 re = getArrMax([5, 2, 99, 101, 67, 77]);
console.log(re);In actual development , We often use a variable to Receive the return result of the function
var re = getArrMax([5, 2, 99, 101, 67, 77]);
console.log(re);5、 Use the function to find the maximum value of different arrays at the same time
function getMax() {
var max = arguments[0];
for (var i = 1; i <= arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i]
}
}
return max;
}
console.log(getMax(1, 2, 3));
console.log(getMax(1, 2, 3, 4, 5));
console.log(getMax(1, 2, 34, 444, 5, 100));6、 ... and 、return
1、return Is the terminating function , The following code will not be executed
2.1、return Only one value can be returned , If you enter multiple values , Subject to the last one
2.2 If you want to use return Return multiple values , You can add an array symbol [ ]
function getResult(num1, num2) {
return [num1 + num2, num1 - num2, num1 * num2, num1 / num2];
}
console.log(getResult(4, 2));3 If the function has return, Is returned return Value after ; If the function does not return, Then return to undefined
7、 ... and 、break,continue,return The difference between
break: End the current cycle
continue: Jump out of this cycle , Continue with next cycle
return: You can not only exit the loop , Can also return return Value in statement , It can also end the code in the current function body
8、 ... and 、Arguments function
8.1 It is a built-in object of the current function , It stores all the passed arguments .
8.2 It is a pseudo array , It's not really an array , have length attribute
8.3 Stored by index
8.4 It doesn't have some real array methods :pop(),push() etc.
8.5 You can traverse in an array arguments
8.6 Only functions have argements object
Nine 、 Packaging function
1、 Flip function
function reverse(arr) {
var newArr = [];
for (var i = arr.length - 1; i >= 0; i--) {
newArr[newArr.length] = arr[i];
}
return newArr;
}
var arr1 = reverse([1, 3, 4, 6, 9]);
console.log(arr1);2、 Bubble function
function sort(arr) {
for (var i = 0; i < arr.length - 1; i++) {
for (var j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
var arr1 = sort([1, 4, 2, 9]);
console.log(arr1);3、 Judgement of leap year
function isRunYear(year) {
var flag = flase;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
flag = true;
}
return flag;
}
console.log(isRunYear(2000));4、 Function call ( Judgement of leap year 2 Month has 29 God )
function backDay() {
var year = prompt(' Please enter the year ');
if (isRunYear(year)) { // Call a function with parentheses
alert(' The current year is a leap year 2 Month has 29 God ');
} else {
alert(' The current year is a normal year 2 Month has 28 God ');
}
return backDay();
function isRunYear(year) {
var flag = flase;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
flag = true;
}
return flag;
}Ten 、 Two ways to declare variables
1、 Use function keywords to customize functions ( Name the function )
function fn(){
}
fn();
2、 Function expression
var Variable name =function(){}
var fun=function(aru){
console.log(' I'm a function expression ')
}
边栏推荐
- Kubernetes affinity learning notes
- Interpreting Julia's 2021: step by step towards the mainstream programming language
- Requirements and business model innovation - Requirements 12 - process oriented modeling
- Steamvr--- grab objects
- What is the difference between the gin framework of golang and the various methods of receiving parameters and various bindings?
- Introduce the functions of the new project aleo
- Final summary of addition, deletion, modification and query
- 2020-12-10
- xml
- Comment prévenir les incendies électriques dans les centres commerciaux?
猜你喜欢
![[Bank Research Report] technology enabled retail finance carbon neutral development report (2022) - download link attached](/img/bd/c1905dc601a598474ef37ea1c38f7f.jpg)
[Bank Research Report] technology enabled retail finance carbon neutral development report (2022) - download link attached

laravel 8 选用 jwt 进行接口验证

2020-12-06

架构入门讲解 - 谁动了我的蛋糕

Lighting Basics: optical model

Getting started with RPC

Comment prévenir les incendies électriques dans les centres commerciaux?

Comparison of scores

如何防止商场电气火灾的发生?

Exemple de projet d'applet Wechat - calculatrice de constitution
随机推荐
Kubernetes' learning path. Is there any "easy mode" Q recommendation for container hybrid cloud
oracle之用户和表空间
One article to show you how to understand the harmonyos application on the shelves
How to build urban smart bus travel? Quick code to answer
Exemple de projet d'applet Wechat - calculatrice de constitution
Go recursive infinite classification
Drawcall, batches, setpasscall in unity3d
1186_ Accumulation of embedded hardware knowledge_ Triode and three electrodes
Interpreting 2021 of middleware: after being reshaped by cloud nativity, it is more difficult to select models
ics-07
central limit theorem
Summary -day11
Restful interface design specification [for reference only]
[Business Research Report] Research Report on super automation technology and application (2022) -- download link attached
Solutions to errors in ROM opening by MAME
Steamvr--- grab objects
分数大小的比较
Oracle users and tablespaces
微信小程序项目实例——体质计算器
AI interview bag | Netease mutual entertainment AI Lab artificial intelligence research engineers share on both sides