Functions Returning Values
Part of the Fundamentals section of Coddy's Dart journey — lesson 68 of 94.
Functions returning values allow your code to calculate a result and send it back to where the function was called. Unlike void functions, these functions specify a return type.
Create a function that returns an integer:
int add(int a, int b) {
return a + b;
}
void main() {
int sum = add(5, 3);
print('Sum: $sum');
}After executing the above code, the output will be:
Sum: 8The function calculates the sum and returns it with the return keyword. The returned value is stored in the sum variable.
Challenge
BeginnerIn this challenge, you'll practice creating a function that returns a value. Functions can calculate a result and return it to be used elsewhere in your code.
Complete the calculateSquare function below. It should:
- Take an integer parameter
number - Calculate the square of that number (multiply it by itself)
- Return the result
The expected output is:
The square of 5 is: 25Cheat sheet
Functions can return values by specifying a return type and using the return keyword:
int add(int a, int b) {
return a + b;
}
void main() {
int sum = add(5, 3);
print('Sum: $sum'); // Output: Sum: 8
}The returned value can be stored in a variable and used elsewhere in your code.
Try it yourself
void main() {
// This number is already defined for you
int number = 5;
// Call the calculateSquare function and store the result
int result = calculateSquare(number);
// This will display the result
print('The square of $number is: $result');
}
// TODO: Complete this function to return the square of the number
int calculateSquare(int number) {
// Your code here - calculate the square and return it
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code11Functions Basics
What are Functions?Defining a Basic FunctionCalling a FunctionFunctions with ParametersFunctions Returning ValuesThe 'void' KeywordArrow SyntaxRecap - Simple Functions3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number