Dart language "005" function

Source: Internet
Author: User

In the previous tutorial, we have been exposed to a number of functions, such as the print function. Functions in Dart are very similar to functions in other common languages such as C, C #, and Java. Such as

var y = f (x);

Represents the calling function f and passing the argument x, initializing the variable y with its return value.


Now compare the system's introduction to the DART function.


First, the definition

1.1 General Mode

The format of the definition function is:

Pseudo-code return value type function name (argument list) {function Body}

The format of the parameter list is

Pseudo code t_1 p_1, T_2 p_2, ..., t_i p_i, ..., T_n p_n

The t_i represents the type of the argument, and the p_i represents the name of the parameter. If there are no parameters, the argument list is empty.

If the function does not return a value, the return value type is void.

Example 1//accepts 2 int, the function that returns int Func1 (int a, int b) {return a + B;} Example 2//accepts 1 string, does not return a value for the function void Func2 (String a) {print (a);} Example 3//a function that returns a string without arguments, String Func3 () {return "Dart leads the Future";}

Like many common languages, the keyword return represents the return of a function.

1.2 Simple Way

If the function f has a return value (at which time F must have a return statement), and the return statement is the entire function body, you can define the function with = =:

int Func1 () = 1;int Func2 (int x) = FUNC1 () + x + 5;

Over here

= R;

Equivalent to

{return r;}

If the function has no return value, and the function body has only one statement, the function can also be defined with = =:

void Func3 () = Print ("Dart terrific");

Over here

= R;

Equivalent to

{r;}


Second, optional parameters

2.1 Optional Parameters for parameter names

If we want to write a function Printcomplex, it takes the real part of the complex and the imaginary part as the parameter, if does not provide the imaginary part, then thinks that the imaginary part is zero, how to do? DART provides optional parameters.

void Printcomplex (num A, {num b:0}) {print ("${a} + ${b}i");}

Parameters enclosed in {} are optional, and a colon (:) is used to specify the default value for the parameter, and the default value for parameter B of Printcomplex is 0.

The function can be called in the following way:

Printcomplex (1.2); Pass 1 Parameters Printcomplex (b:90.24); Pass 2 parameters

Passing values to optional parameters must specify a name, not directly Printcomplex (1, 2) as in C + +;.

One more example:

void Main () {Func (100, 200); Func (1, 2, d:20); Provides d, does not provide C Func (2, 4, c:10); Provides C, does not provide D Func ( -1,-2, C: -3, D:-4);} void Func (int a, int b, {int c:20, int d:25}) {print ("a = ${a}, b = ${b}, c = ${c}, d = ${d}");}


2.2 Optional Parameters for parameter positions

If you include an optional parameter with [], these optional parameters are optional for the position of the parameter.

void Func (int a, int b, [Int c = +, int d = +]) {print ("a = ${a}, b = ${b}, c = ${c}, d = ${d}");}

When you call Func, you can pass 2 or 3 or 4 parameters.

void Main () {Func (20, 21);  Func (1, 2, 10); Func (100, 42, 9078,-1);}

More generally, set the function

Pseudo code void Func (t_1 p_1, T_2 p_2, ..., t_m p_m, [r_1 q_1 = v_1, r_2 q_2 = v_2, ..., r_n q_n = V_n]) {//function body}

You can pass the M, m+1, m+2 、...、 m+n parameters to Func.

The provided actual attendees are passed in from left to right .

Dart language "005" 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.