Dart is a true object-oriented language, so even functions are objects and have a type, Function. This means that functions can be assigned to variables or passed as arguments to other functions. You can also call an instance of a Dart class as if it were a function. For details, see Callable classes.

In dart, we face with functions. Other programming languages like C,Java,Python contains functions.

In simple words, functions are basically a block of statements. Functions are designed to getting same work done by calling it each time.

Why do we use functions in dart?

  • Reusable code – functions are basically a block of statements. When we calling a function, then we also calls that statements which defines under that function.
  • Less space – when we calling any function, then that function does the same job each and every time whatever statements declares within that function. So technically it takes less space in program.
  • Save time – time matters for every programmer and developers. Using a function it saves a lot of time.

Let’s discuss about function declaration and calling it…

Creating a function

Remember in every code in dart, we need to use main() function. Why? Because the statements which will executes under the main() function. Along with interpreted compiler also checks for main() function and then started execution of it.

Syntax –

datatype function(parameters){
Body of function;
}

Calling a function

We calls any function inside main() function. For calling a function, we need to declare by its name and followed by pair of parenthesis.

Syntax –

datatype function(parameters){
body of function;
}
// calling a function
function (arguments);

Let’s see some coding examples…

Code 1:

void main(){
  // creating a function
  void simple(){
    print(‘simple function with no return-type’);
  }
  // calling a function
  simple();
}
output

Let’s also understand simple concepts of declare, define and calling a function.

Note: you can declare a function and define function’s body outside of main() function.

Code 2:

// declaring a function[outside of main() function]
String? func(){
}
 
void main(){
  // defining a function
  func(){
    print(“calling function’s body”);
  }
  // function call
  func();
 
  print(‘main function body!’);
}
output

Let’s make a function which will do addition of two double numbers.

Code 3:

double sum(double x,double y){
  double sum=x+y;
  print(‘addition is $sum’);
  return sum;
}
void main(){
sum(10.2,30.4);
}
output

From the upper code, you see sum(double x,double y) , function name->’sum’ and parameters -> x,y and arguments -> 10.2,30.4 .

** Don’t worry if you not familiar with parameters and arguments, you will learn about this in next article.

Remember: we call any method ‘n’ number of times.

Let’s see another code below…

Code 4:

void run(){
  print(‘we are microcodes!’);
  print(‘geeks for coders!’);
}
void main(){
  // calling run() function 3 times
  run();
  run();
  run();
}
output