HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to DartVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Dart›Functions

Functions

Function definition, parameters, return, arrow (=>) shorthand syntax, and default values.

Simple enough for a 5-year-old to understand

Define and call functions

A function starts with its return type (e.g., int), followed by its name and parameters.

Example code
1
int square(int x) {
A function that returns an int and takes an int.
FunctionA function that returns an int and takes an int.
2
return x * x;
Returns the result.
DefinitionReturns the result.
3
}
4
void main() {
5
print(square(5));
Writes 25.
Output / InputWrites 25.
6
}
FunctionDefinitionOutput / Input
💡 Hint: return gives the value back; print only displays it on the screen.

About this topic

In Dart, functions are defined with a return type, name, and parameter list. Functions that return nothing have a void return type. Functions whose body consists of a single expression can be shortened with arrow syntax.

Dart's parameter system is flexible: alongside mandatory positional parameters, there are optional positional and named parameters. Named parameters form the characteristic look of Flutter code, as widget constructors receive dozens of settings this way.

With null safety, a nuance emerged regarding named parameters: a non-nullable named parameter must either have a default value or be marked as required. Otherwise, the compiler will not allow it.

What you will learn

  • Defining and calling a function
  • Named and optional parameters
  • The mandatory parameter flag
  • Arrow syntax
  • Passing functions as values

Who is this for?

  • Those looking to avoid code repetition
  • Those wanting to understand Flutter constructor syntax
  • Those getting parameter errors

When to use?

If a piece of code is written a second time, it should be extracted into a function.

If there are more than two parameters, named parameters should be preferred; this makes it clear what each value represents at the call site.

Common mistakes

Wrong

int topla(int a, int b) {
  print(a + b);
}

The function declared it would return an int, but return was not written; the error "The body might complete normally, causing 'null' to be returned" is thrown. Printing to the screen and returning a value are different things.

Correct

int topla(int a, int b) {
  return a + b;
}

Wrong

void selamla({String ad}) {
  print("Hello $ad");
}

A non-nullable named parameter must either take a default value or be marked as required; otherwise, the error "The parameter 'name' can't have a value of 'null'" is thrown. Null safety introduced this rule.

Correct

void selamla({required String ad}) {
  print("Hello $ad");
}

Wrong

void selamla({required String ad}) {
  print("Hello $ad");
}
void main() {
  selamla("Ayse");
}

Named parameters must be provided by their names at the call site; they cannot be passed positionally, resulting in a "Too many positional arguments" error. This is a common error in Flutter code.

Correct

void selamla({required String ad}) {
  print("Hello $ad");
}
void main() {
  selamla(ad: "Ayse");
}

Frequently asked questions

How do named parameters become optional?

By providing a default value or making their type nullable. Unless you write required, named parameters are optional; however, for a non-nullable type, you must provide a default value.

When is arrow syntax used?

When the body consists of a single expression. Instead of curly braces and return, you write an arrow. In Flutter, it significantly simplifies code in event handlers and short transformations.

Can I pass a function to another function?

Yes, in Dart, functions are first-class values. You can assign a function to a variable, pass it as an argument, and return it. Collection methods and event handlers are built upon this feature.

Summary

If a return type is declared, return is mandatory.

Named parameters are provided by name at the call site.

A non-nullable named parameter requires required or a default value.

Single-expression functions are shortened with an arrow.

Functions can be passed as values.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code