• Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Getting Started with Dart
    • Dart Variables
    • Dart Operators
    • Dart Comments
  • Dart Data Types
    • Dart Strings
    • Dart Numbers
    • Dart Booleans
    • Dart Lists
    • Dart Maps
  • Control Flow
    • Dart if...else Statements
    • Dart switch Statement
    • Dart for Loop
    • Dart for...in Loop
    • Dart while and do...while Loops
    • break and continue Statements
  • Dart Functions
  • Dart Classes
  • Dart Objects
  • Dart Constructors
  • Dart Exceptions

Dart Functions

155 views 0

Written by Ajaya Shrestha
January 12, 2022

In this chapter, we will learn about the dart functions, and it’s implementation with the help of examples.


A function is a block of code that helps to perform a specific task.

For example, we need to find the sum of two values. For that, we can create a function and later on use the same function if we need to perform a similar operation.

Defining/Declaring a Function

The syntax for defining a function is:

function_name() {  
   // statements      
}

A function is defined using the function name followed by parentheses ().

The body of function is written within curly braces {}.

For example,

// defining a function
sayHi() {
    print(‘Hello.’);
}

Calling a Function

In order to use a function, we need to call that function.

The syntax to call a function is:

function_name();

In the above program, we have defined the function.

Now to use that function, we can call that as:

void main() { 
   sayHi(); 
}  
sayHi() {
    print('Hello');
}

Output

Hello

Here, we are calling the function sayHi(); inside the main() function.

In this way, we can call the dart functions.


Function Parameters

We can also use a function with parameters. The syntax of function with parameters is:

function_name(data_type param) {  
   // statements      
}

The data_type represents the data type of the parameter that will be passed in the function call.

For example,

void main() { 
   sayHi('Simon');
   sayHi('Jenny');
   sayHi('Paul');
}  
sayHi(String name) {
    print('Hello ' + name);
}

Output

Hello Simon
Hello Jenny
Hello Paul

In the above example, the sayHi() function takes a name parameter with string data type.

When calling the function, we are passing the string value Simon on the first function all. Similarly we are passing Jenny and Paul to the function as well.


Example: Add Two Numbers

void main() { 
   add(5, 8);
   add(9, 9);
}  
add(int num1, int num2) {
  int result = num1 + num2;
  print('The sum is $result');
}

Output

The sum is 13
The sum is 18

In the above program, the add() function takes two parameters of integer value. The two values are added and the program prints the sum.

When calling the add() function, two integer values are passed as arguments to the function.


Dart Functions Return

We can also use the return statement to return the value to the function call. For example,

void main() { 
  var greet = sayHi();
  print(greet);
}  
sayHi() {
    String result = 'Hello ';
    return result;
}

Output

Hello

In the above program, the return statement returns the value of the result variable. And the greet variable calls that function and prints the value.

Let’s see another example of the dart functions along with the return statement with the parameters.

void main() { 
  var greet = sayHi('Simon');
  print(greet);
}  
sayHi(String name) {
    String result = 'Hello ' + name;
    return result;
}

Output

Hello Simon

In the above program, the result variable stores the string value and the return statement returns the value.

The greet variable stores the returned value and prints the output. 


Why Use Dart Functions

  • The function helps in code reusability. Same function can be used multiple times throughout the program.
  • Function helps in code readability.
  • Huge programs can be divided into small individual functions.

Was this helpful?

Yes  No
Related Articles
  • Dart Constructors
  • Dart Exceptions
  • Dart Objects
  • Dart Classes
  • Control Flow
  • break and continue Statements
Previously
break and continue Statements
Up Next
Dart Classes
flutter-dev-experts




FLUTTERGUIDE
FlutterGuide is an easy learning guide platform to help you build apps for any screen. The leading Dart/Flutter guides for getting started and become pro.
Guides
  • Getting started with Dart 22
  • Private Documentation 1
Categories
  • Firebase
  • Dart
  • iOS
  • API
  • Bloc
  • Flutter
  • Widgets
  • Mobile
  • State Management
  • Design
  • Animations
  • Flutter Designs
About
About Us
Guides
Blogs
FAQs
Contact
  • Privacy Policy
  • Terms & Conditions
  • Copyright 2022 Fawesome Apps. All Rights Reserved