Definition and invocation of a function

Source: Internet
Author: User

Blog Park debut, reproduced please indicate the source, thank you for your support. http://www.cnblogs.com/xuema/

Definition and invocation of a function

The syntax for defining a function in Typescript is:

    1. function function_name(arg:number,arg1:number,....):return_type {
    2. the code function to execute;
    3. return data;
    4. }

Where  function  is the keyword that declares the function, functionname is the name of the custom function, arg is the argument list, _return_type is the return value type of the function, and code is the one to execute when the function is called. Use the return keyword to return data, which is the data to be returned, enclosed in "{}". The invocation of the function is simple, the following code:

    1. function Add(x: number, y: number): number { // Define a function that returns a number type
    2. return x+y;
    3. }
    4. Add(5,6); //Call function

Of course, there can be no return value.

anonymous functions

An anonymous function is a function that has no name but a principal and does not require a return type, and its return value type is inferred from the return statement within the body of the function. The following code:

  1. var myadd = function(x:number, y:number) { //define anonymous functions
  2. return x+y;
  3. };
  4. Myadd(3,4); //Call anonymous function

Optional and default parameters

Optional parameter: After the parameter name, a question mark is added before the colon, indicating that the parameter is optional. The following code:

  1. function buildname(firstName: string, lastName?: string ) { //lastname is an optional parameter
  2. if (lastName)
  3. return firstName + "" + lastName;
  4. Else
  5. return firstName;
  6. }
  7. var result1 = buildname("Bob"); //Call Bob correctly
  8. var result2 = buildname("Bob", "Adams"); //Call Bob Adams correctly

Default parameter: Given a value directly after the parameter name, if the value is not passed in, it will be assigned to the default value. The following code:

  1. function buildname(firstName: string, lastName = "Smith") {
  2. return firstName + "" + lastName;
  3. }
  4. var result1 = buildname("Bob"); //Does not pass in the second parameter, it is assigned the default Smith, and the result is: Bob Smith
  5. var result2 = buildname("Bob", "Adams"); //Result: Bob Adams

Note: Optional parameters and default parameters must be at the end of the parameter list.

Free interactive exercises and more introductory learning content here (Hui Zhi network): http://www.hu bwiz.com/class/55b724ab3ad79a1b05dcc26c Remove the space in the middle of the domain name

Definition and invocation of a function

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.