Dart functions Tutorial With Examples

7
7287
dart functions with example tutorial
function in dart example

Dart functions or methods are code building blocks that can be reused. Functions are easy to maintain and can be used multiple times if required. Functions divide the functionality of a program into parts means the code is easy to maintain. Function code is executed when the function is called. In this tutorial, We will learn about Dart functions.

The syntax of a function looks like this.

  
reutenType functionName(parameters) {
    //Code block 
    return value;
  }

As the syntax is clear. Let’s take a demo example. Whenever findArea function get called it will print the Area.

  
void findArea(int length, int breath) {
    print(length * breath);
  }

Dart functions properties

  
reutenType functionName(parameters) {
    //Code block 
    return value;
  }
  • Remember the statement ‘Everything in Dart is Object’! So do functions. Functions are also objects in Dart. Which means functions can be assigned to a variable or passed as a parameter to other functions.
  • All functions in Dart return value means each and every function have a return time even if you don’t call it explicitly. If no return value is specified it will return null. Both the functions return null value
  
void main() {
  findArea(int length, int breath) {
    //By default return null
  }
}

  
void main() {
  int findArea(int length, int breath) {
    //It will also return null as the return statement is missing 
  }
}
  • Function return type in Dart is also optional but it highly recommended to specify a return type. It is better to follow the code convention and declare a return type. Both of the functions are the same
  
 void findArea(int length, int breath) {
    print(length * breath);
  }
  
 findArea(int length, int breath) {
    print(length * breath);
  }

Dart Function Example

There are two functions findPerimeter and findArea. Both of the functions are called from the main function and the first findPerimeter function is not returning anything just print the Perimeter within the function. The second function findArea is returning an integer value to the main function and then printing the print in main.

  
 void main() {
  findPerimeter(9, 6);
  var rectArea = findArea(10, 6);
  print('The area is $rectArea');
}

void findPerimeter(int length, int breath) {
  var perimeter = 2 * (length * breath);
  print('The perimeter is $perimeter');
}

int findArea(int length, int breath) {
  return length * breath;
}

This is pretty much all to understand the basics of a Dart function or method. We will explore functions in details in Dart Classes.

7 COMMENTS

  1. My Dear Brother, words are not enough to thank you. God Almighty elevate your status in this world and Hereafter. Thank you for making learning dart and flutter easy to a beginner like me.

  2. var length= int.parse(stdin.readLineSync().toString());
    int length= int.parse(stdin.readLineSync().toString());
    this is working write but
    var length= stdin.readLineSync();int length= stdin.readLineSync();
    is shown the following error I dont under stand why?
    String? readLineSync({Encoding encoding = systemEncoding, bool retainNewlines = false})
    dart:io

    Reads a line from stdin.

    Blocks until a full line is available.

    Lines my be terminated by either or . On Windows, in cases where the [stdioType] of stdin is [StdioType.terminal], the terminator may also be a single .

    Input bytes are converted to a string by [encoding]. If [encoding] is omitted, it defaults to [systemEncoding].

    • this error shows in vscode only,when I work on replit.com following code workig well.
      var length= stdin.readLineSync();int length= stdin.readLineSync();

LEAVE A REPLY

Please enter your comment!
Please enter your name here