YouTip LogoYouTip

Dart Functions

\n\n\n\nDart functions | Tutorial\n\n\n\n

Dart Functions

\n\n

A function is the basic unit of organizing code. It encapsulates a piece of reusable logic that can be called when needed.

\n\n

Dart's function system is very flexible, supporting named parameters, optional parameters, arrow functions, and higher-order functions.

\n\n
\n\n

Function Definition and Return Values

\n\n

A function consists of a return type, function name, parameter list, and function body.

\n\n

Example

\n\n
\n// Define a simple function\n\n// String is the return type, greet is the function name, (String name) is the parameter list\n\n String greet(String name){\n\nreturn'Hello, $name! Welcome to TUTORIAL.';\n\n}\n\n// Functions without return value use void\n\nvoid printWelcome(){\n\n print('=== TUTORIAL Dart Tutorial ===');\n\n}\n\nvoid main(){\n\n printWelcome();\n\n// Call the function and receive the return value\n\n String message = greet('Xiao Ming');\n\n print(message);\n\n}\n
\n\n

Output

\n\n
\n=== TUTORIAL Dart Tutorial ===\nHello, Xiao Ming! Welcome to TUTORIAL.\n
\n\n

Return Value

\n\n

Every function has a return value.

\n\n

If there is no explicit return, the function implicitly returns null (but in null safety this causes type mismatch, so void is usually used to indicate no return value).

\n\n

Example

\n\n
\n// Function that returns int type\n\nint add(int a,int b){\n\nreturn a + b;\n\n}\n\n// Use arrow syntax to simplify single-expression functions\n\nint multiply(int a,int b)=> a * b;\n\n// void indicates no meaningful return value\n\nvoid log(String msg){\n\n print(' $msg');\n\n}\n\nvoid main(){\n\n print('3 + 5 = ${add(3, 5)}');\n\n print('3 Γ— 5 = ${multiply(3, 5)}');\n\n log('Function learning completed');\n\n}\n
\n\n

Output

\n\n
\n3 + 5 = 8\n3 Γ— 5 = 15\n Function learning completed\n
\n\n
\n\n

Named Parameters and Optional Parameters

\n\n

Dart's parameters are divided into two types: required parameters and optional parameters.

\n\n

Optional parameters are further divided into named parameters and positional parameters.

\n\n

Named Parameters

\n\n

Named parameters are wrapped in curly braces {}, and are passed by parameter name when calling, with arbitrary order.

\n\n

Example

\n\n
\n// Parameters in curly braces {} are named parameters, optional by default\n// Use required keyword to mark as required\n\n String createUser({\n\n required String name,// Required named parameter\n\nint age =0,// Optional, default value is 0\n\n String? email,// Optional, can be null\n\nbool isVip =false,// Optional, default value is false\n\n}){\n\nvar info ='Username: $name, Age: $age';\n\nif(email !=null){\n\n info +=', Email: $email';\n\n}\n\nif(isVip){\n\n info +=' ';\n\n}\n\nreturn info;\n\n}\n\nvoid main(){\n\n// Named parameters: pass by parameter name, order doesn't matter\n\n print(createUser(name:'tutorial', age:10));\n\n print(createUser(name:'admin', email:'admin@example.com', isVip:true));\n\n// Required parameters cannot be omitted\n\n// createUser(); // Error: missing required parameter name\n\n}\n
\n\n

Output

\n\n
\nUsername: tutorial, Age: 10\nUsername: admin, Email: admin@example.com \n
\n\n

Positional Parameters

\n\n

Positional parameters are wrapped in square brackets [], and are passed in order when calling.

\n\n

Example

\n\n
\n// Parameters in square brackets [] are optional positional parameters\n\n String buildUrl(String host,[String path ='/',int port =80]){\n\nreturn'http://$host:$port$path';\n\n}\n\nvoid main(){\n\n// Only pass required parameters, optional parameters use default values\n\n print(buildUrl('example.com'));\n\n// Pass 2 parameters\n\n print(buildUrl('example.com','/dart'));\n\n// Pass 3 parameters\n\n print(buildUrl('localhost','/api',8080));\n\n}\n
\n\n

Output

\n\n
\nhttps://example.com:80/\nhttps://example.com:80/dart\nhttp://localhost:8080/api\n
\n\n

Named Parameters vs Positional Parameters: How to Choose

\n\n\n\n<
← Dart InheritanceDart Control Flow β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.

ScenarioRecommendedReason