Basic concepts of functions and the use of recursion

Source: Internet
Author: User

Function

1.//basic functions, learn to call functions:

how ();//Call function, anywhere can call    //output a sentence    function show ()    {         alert ("This is a simple function");     }    Show ();//Call function, anywhere can call

2.//functions with parameters

Function Show (a)//a is called the formal parameter, the formal argument {    alert (a);}    Show ("parameters");//arguments, actual parameters

3.//functions with multiple parameters

function sum (A, B)//The name of the formal parameter can be arbitrarily {    alert (a+b);}    SUM (1+2);//Set parameters for A and B, respectively

4.//function with return value

function sum (A, b) {    return a+b;//return value a plus a    a=sum (3,4);//Set Parameter    alert (a);//js the function is often used where the event// Other language functions are used in the word encapsulation function

Several important functions:

1. Random number generator: Math.random ()
2.random () returns a random number between 0 and 1
3.substr ********
Returns a substring of a string in which the incoming parameter is the starting position and length
4.replace *******
Replace String, the first argument represents the replaced string, the second argument represents the replacement string
5.split ******
Make a string into a string array by dividing the string into substrings
6.length attribute *******
Returns the length of a string, the length of which is the number of characters it contains

Recursion!
1. There are 200 peaches in the park, the monkeys eat 10 a day, pick out two bad throw away, ask 6 days after the remaining number of peaches? (first with a loop)

var sl=200//total number of peaches for (var i=0;i<6; i++) {    sl=sl-10-2;     }    alert (SL);

2. There are a bunch of peaches in the park, the monkeys eat half a day, pick a bad throw away, the 6th day found that there are 1 peaches left, asked how many peaches? (Made with a loop)

var sl=0;for (i=0;i<6; i++) {    sl= (sl+1)* *}    alert (SL);

3. Use the function to find the above topic (recursion)
Function: Returns the number of peaches in the nth day
Use the meaning of the function itself to make itself a variable in the equation or arithmetic, participate in the operation!

var sl;//defines the number of peaches as Slfunction Shuliang (n) {        ///when n equals 6 peaches is 1    if (n==6)    {        sl=1;    }    else {//the number of days//number of day        = (number of next day + 1) * *        sl= (Shuliang (n+1) +1) * *;    }        return SL; Returns the number of the day}    alert (Shuliang (0));

Basic concepts of functions and the use of recursion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.