DEV Community

Raphael Kihiro
Raphael Kihiro

Posted on

Functions in dart

1. Introduction

Functions are a fundamental construct in programming languages, enabling, code reusability, and improved maintainability. This in this segment I will dive into the definition, structure, and invocation of functions in Dart, highlighting their syntax and purpose within program design.

1.1 Function Definition.
A function in Dart is defined as a reusable block of code designed to execute a specific operation. The general syntax of a function is as follows:

In this example:

  • greet denotes the function name, which serves as the identifier for invocation.
  • The parentheses() may contain parameters,
  • The curly braces {} enclose the function body,

For a function to be executed it has to be invoked(called);

To execute the Dart file, run the commanddart run(file name), in the terminal:

When executed, this program prints:

2. Structure of a Function

2.1 The main() Function in Dart

In Dart, every standalone program must have a main() function, that serves as the entry point of the program. Without this function, Dart cannot determine where to start execution, leading to a runtime error.

When you attempt to run a Dart program without defining a main() function, the following error appears:

2.1.1 Syntax of the main() Function
The main() function has a simple and well-defined syntax. It’s also important to note that themain()function returns void, meaning it does not have a return value.

2.1.2 Example of the main() Function Calling Another Function
Here is an example demonstrating on how the main() function interacts with other functions:

The greet() function is defined to return a string value('hello dart learner').The main() function calls greet() using print(greet());.
And the expected output is;

2.2 Functions as First-Class Objects in Dart

In Dart, functions are first-class objects, meaning they can be treated just like any other variable or object in the language. This gives functions in Dart the following characteristics;

  • Be stored in a variable,
  • Be passed to another function as an argument, and
  • Be returned from another function.

2.2.1 Assigning a Function to a Variable
A function can be assigned to a variable, allowing it to be called later using that variable name.

The image above show a random function sayHello().
Continuation on the same

Here, the function sayHello() is assigned to a variable greetFunc. This allows you to call the function later using the variable name instead of the original function name.
When you call greetFunc(), the output will be:

2.2.2 Passing a Function to Another Function

Functions in Dart can also be passed as arguments to other functions.
This is particularly useful for callbacks, event handling, and customizing behavior dynamically.

Here’s a visual illustration of passing a function as an argument:

The expected output;

2.2.3. Functions with a return value
A function can also return a value after performing its task.
If a function is declared as void, it means it does not return anything.

Here is the output when the function is void;

In the example below, we use the type bool to indicate that the function returns a Boolean value (true or false).

When arguments are passed into the functionisEven() remember we use the print() to have the output displayed on the terminal

The expected output is;

2.3 Parameters in Functions
Parameters act as inputs that a function receives during invocation.

In the illustration above, String name inside the parentheses () is a parameter. That acts as a placeholder for the argument (the actual value) passed during the function call.
Parameters allow the function to process variable data instead of being limited to fixed values.

Expected output:

There several types of parameters used in functions:

2.3.1 Positional Parameters
Positional parameters are the most basic type of function parameters.
They must be passed in the same order they are defined in the function declaration.

When calling the function the order matters.

In the code above, notice the commented-out section — it shows an error caused by incorrect parameter order and if executed this is the output is.

But if the parameters are correctly order the output is;

2.3.2 Named Parameters { }

Named parameters are enclosed in curly braces {} and allow arguments to be specified by name.
This enhances code readability and flexibility, especially when dealing with many parameters.

Named parameters are optional by default, but they can be made required using therequired keyword.

They can also have default values using the = operator.

The image below shows illustrations on the named parameters on different functions being invoked(read the comments they can help guide you)

And the expected outcome respectively;

3. Optional Positional Parameters [ ]

Optional positional parameters allow you to make some function parameters optional by enclosing them in square brackets [ ].
If the caller doesn’t provide a value for these parameters, they take on a default value (if one is specified) or become null by default.

The arguements passed in the function.

Excepted output;

3. Function Scope in Dart

Scope defines where variables and functions can be accessed within a program. Variables can be defined as global, local, or nested, depending on where they are declared.

3.1 Global Scope
A variable declared outside any function or class has global scope, meaning it can be accessed from anywhere within the same file. As shown in the image below.

The variable String greetingwhich is defined outside the main function (inside the global scope)can be accessed in the scope of the main() function using print(greeting), and the expected output is:

Reason being the variable greeting is defined globally and can be accessed within any function in the same file.

** 3.2 Local scope**
A local variable is declared inside a function or block. It can only be accessed within that specific function. Here is an illustration on the same.

The image above shows a sayHello() function with a variable String message defined inside it's scope.

When we try to access the variable inside the main() function using the print(message) we get an error

The error occurs because I am attempting to access a variable that is outside the scope of the main()function. But, when we call the function itself inside the main function the output is:

3.3 Lexical Scope
Dart uses lexical (or static) scope, which means that variable visibility is determined by where the variables and functions are written in the code, not by where they are called at runtime.
Each function can access variables defined in its enclosing scope.


The innerFunction() function can access the variables both in the scope of the main() and the outerFunction() String name and int agerespectively
This is because innerFunction() is lexically enclosed within main() and the outerFunction(), giving it access to all variables defined in it outer/enclosed scope.
Illustrating Dart’s lexical scoping rule, where a function can “see” variables in its surrounding scopes, but not vice versa.
expected output is:

3. Reflection.

Throughout this exploration of functions in Dart, several key lessons emerge that deepen both conceptual understanding and practical application in programming. Functions allow developers to write cleaner, modular code that is easy to maintain and debug. The main() Function is the Entry Point of Every Dart Program where execution begins. Moreover, functions in Dart are First-Class Objects meaning they can be stored in variables, passed as arguments, or even returned from other functions.
In essence, understanding how functions work from their structure, invocation, and scope to their role in code organization is fundamental to becoming a proficient developer.
Understanding these principles equips one with not only to write better code but also to design systems that are scalable, efficient, and easy to maintain.

Top comments (0)