Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is the return keyword in Dart?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Sometimes, we’ll need a function to return a value to use later in our program. The return keyword passes this output value for that function.

Syntax

return_type func_name()
{
   //Statement(s)
   return value;
}

- **func_name:** represent the  function's name. 
- **return_type:** represents the function's return type which is the data type. 
- **value:** represents the return. value

> Note: The *value* returned must have the same data type as the function's return type.

Code

The following code shows how to use the return keyword in Dart.

// create sum() function
int sum(a) {
return a+10;
}
void main(){
// declare and assign value
int a = 20;
//invoke sum()
print(sum(a));
}

Explanation

  • Line 2–4: We create a function sum() of int type that takes an argument a, and returns the sum of a + 10.
  • Line 5–10: We create a main() function.
  • Line 7: We declare a variable a, and assign a value to it.
  • Line 9: We pass a to the sum() function and display the result.

RELATED TAGS

dart
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring