Dart Programming 102: Illegal Functions in Dart

Hello Learners, today we’re diving into some illegal functions in Dart that might surprise you! Before learning about them, they may seem tricky, but once you understand how to create functions in Dart, you’ll feel like a coding gangster in the app development world. 😂 Now, let’s get back to the main topic.

Illegal Functions in Dart

Introduction

Illegal Functions in Dart

Each week, there are tasks that you repeat over and over: eat breakfast, brush your teeth, write your name, read books about Dart, and so on. Each of those tasks can be divided up into smaller tasks. Brushing your teeth, for example, includes putting toothpaste on the brush, brushing each
tooth and rinsing your mouth out with water. The same idea exists in computer programming. A function
is one small task, or sometimes a collection of several smaller, related tasks that you can use in conjunction with other functions to accomplish a larger task.

Functions in Dart

A function in Dart is a piece of code that performs an action. It consists of:

  • A return type (can be void if nothing is returned)
  • A name (identifier)
  • A set of parameters (optional)
  • A body (the code that runs when the function is called)

Functions Basics

You can think of functions like machines; they take something you provide to them (the input), and produce something different (the output).

Illegal Functions in Dart

There are many examples of this in daily life. With an apple juicer, you put in apples and you get out apple juice. The input is apples; the output is juice. A dishwasher is another example. The input is dirty dishes, and the output is clean dishes. Blenders, coffee makers, microwaves and ovens are all like real-world functions that accept an input and produce an output.

Don’t repeat yourself

Assume you have a small, useful piece of code that you’ve repeated in multiple places throughout your program:

Illegal Functions in Dart

Now, that code works rather well, but repeating that code in multiple spots presents at least two problems. The first problem is that you’re duplicating effort by having this code in multiple places in your program. The second, and more troubling problem, is that if you need to change the logic in
that bit of code later on, you’ll have to track down all of those instances of the code and change them in the same way.

That creates a high possibility that you’ll make a mistake somewhere, or even miss hanging one of the instances because you didn’t see it. Over time, this problem has led to some sound advice for
writing clean code: don’t repeat yourself, abbreviated as DRY.

This term was originally coined in the book The Pragmatic Programmer by Andrew Hunt and David Thomas. Writing DRY code will help you prevent many bugs from creeping into your programs. Functions are one of the main solutions to the duplication
problem in the example above. Instead of repeating blocks of code in multiple places, you can simply package that code into a function and call that function from wherever you need to.

Anatomy of a Dart function

In Dart, a function consists of a return type, a name, a parameter list in parentheses and a body enclosed in braces.

Here is a short summary of the labeled parts of the function:

  • Return type: This comes first; it tells you immediately what the type will be of the function output. This particular function will return a String, but your functions can return any type you like. If the function won’t return anything, that is, if it performs some work but doesn’t produce an output value, you can use void as return type.
  • Function name: You can name functions almost anything you like, but you should follow the lowerCamelCase naming convention.
  • Parameters: Parameters are the input to the function; they go inside the parentheses after the function name. This example has only one parameter, but if you had more than one, you would separate them with commas. For each parameter, you write the type first, followed by the name. Just as with variable names, you should use lowerCamelCase for your parameter names.
  • Return value: This is the function’s output, and it should match the return type. In the example above, the function returns a String value by using the return keyword. If the return type is void, though, then you don’t return anything.

The return type, function name and parameters are collectively known as the function signature. The code
between the braces is known as the function body. This is what the function above looks like in the context of a program:

What have we here? Not one function, but two? Yes, main is also a function, and one you’ve seen many times already. It’s the function that every Dart program starts with. Since main doesn’t return a value, the return type of main must be void.

Although main can take parameters, there aren’t any in this case, so there’s only a pair of empty parentheses that follow the function name.

Notice that the compliment function is outside of main. Dart supports top-level functions, which are functions that aren’t inside a class or another function. Conversely, you may nest one function inside another. And when a function is inside a class, it’s called a method.

You call a function by writing its name, and providing the argument, which is the value you provide inside the parentheses as the parameter to the function. In this case, you’re calling the compliment function and passing in an argument of 12. Run the code now and you’ll see the following result:

12 is a very nice number.

Synchronous vs Asynchronous Functions

  • Synchronous function
    • Executes code line-by-line, and once it completes, the next function is run. It does not involve waiting or handling operations that take time (e.g., network requests).
  • Asynchronous function
    • A function that doesn’t block the program from running while waiting for a task to complete (like fetching data from a server). Asynchronous functions use async and await keywords.

Function Declarations

A function declaration is just a way to tell the computer what a function (a small piece of code that does something specific) will do.

  • A library function: is one that’s available at the top level, meaning it can be used anywhere in your code (it’s declared at the outermost level).
  • A local function: is one that’s declared inside another function, so it can only be used within that particular function.

Here’s a breakdown of what happens in a function declaration:

Suppose you started your own company called XYZ, and everywhere in the world, if you meet a client, you only need to mention your company name. The client will immediately know what to do next, how to reach you, and how to contact you further. Similarly, a function declaration works in the same way. If you define the code inside the function globally, it becomes accessible from anywhere. Otherwise, you need to define it within a specific scope for it to be usable.

  1. The function’s name: This is what you call the function when you want to use it.
  2. The function’s body: This is the code inside the function that does the actual work. Sometimes functions don’t need a body, like getters that just return values.

Scope is about where you can access the function:

  • Library function’s scope: A library function can be used anywhere in the file.
  • Local function’s scope: A local function can only be used in the function where it’s defined.

Example of Library Function

Example of Local function

Function Parameter

Basically, there are two types of parameters: formal parameters and actual parameters. Let’s explore each of them one by one.

When you create a function, and if your function needs input, you create a formal parameter. Formal parameters are placeholders or variables that a function uses to accept input when it is called.

These parameters help the function know what data it should work with. When you define a function, you specify these formal parameters so that when the function is called, the real values (called actual parameters or arguments) can be passed in.

Dart, you can pass four types of formal parameters to functions:

Positional Parameters

These are the most basic type of parameters, where the position or order of arguments matters when you call the function. The arguments must be passed in the same order as the parameters are defined.

Named Parameters

These allow you to pass arguments by explicitly mentioning the parameter’s name. This makes it easier to know what each argument refers to, and the order does not matter.

Optional Parameters

These parameters don’t need to be provided when calling the function. You can make them optional by providing default values. There are two types of optional parameters in Dart:

  • Optional Positional Parameters: The parameters are optional and positional, and are enclosed in square brackets [].
  • Optional Named Parameters: These are optional and can be specified by name.

Example of optional positional:

Example of optional named:

Generic Functions

These functions can work with different data types without having to rewrite the function for each type. You define the function using type parameters (e.g., <T>), which allow the function to work with any type of data.

Required formal parameters

this explanation primarily focuses on required formal parameters in the context of classes. In Dart, these formal parameters are often used when defining constructors and methods within a class.

Required formal parameters are parameters that must be provided when calling a method or constructor. In a class constructor, they are typically used to initialize class fields (like this.name and this.age).

Thank you for taking the time to read! Feel free to explore more of our content or leave a comment below.

Mastering Variables for Efficient Coding

or

Leave a Reply

Your email address will not be published. Required fields are marked *