Return statement in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

There are certain cases where we want to return a value from a given function so that we can use it later. These return values make use of a return keyword which in turn allows a function to return values.

It should be noted that the return statement is optional, if not specified the function returns null.

Also, only one return statement is allowed in a function.

Syntax

return <expression/value>;

Syntax of a Dart function with a return value

returnType funcName(){
   // statement(s)
   return value;
}

In the above syntax, the funcName is replaced with the name of our function. The returnType represents the type of data/expression we are returning from the function.

Example

Let's take an example where we make use of the function which returns a value in a dart program.

Consider the example shown below −

 Live Demo

void main(){
   var x = 10;
   var y = callFunction(x);
   print(y);
}

int callFunction(var x){
   return x + 10;
}

In the above example, we are returning a value of type int from a function named callFunction and then we use that value inside the main function.

Output

20
raja
Published on 24-May-2021 11:51:21
Advertisements