A function is a block of code that performs a specific task and can be reused. It helps organize code and makes it easier to read and maintain.
Click to reveal answer
beginner
How do you write a simple function with arrow syntax in Dart?
Use the arrow (=>) followed by the expression. For example: int add(int a, int b) => a + b; This means the function returns the result of a + b.
Click to reveal answer
beginner
What is the difference between a normal function and an arrow function in Dart?
A normal function uses curly braces and a return statement. An arrow function is a shorter way to write a function that returns a single expression without curly braces or return keyword.
Click to reveal answer
beginner
Can arrow functions have multiple statements in Dart?
No, arrow functions can only have one expression. For multiple statements, use normal function syntax with curly braces.
Click to reveal answer
beginner
Example: Convert this normal function to arrow syntax: int square(int x) { return x * x; }
Using arrow syntax: int square(int x) => x * x; This is shorter and does the same thing.
Click to reveal answer
Which symbol is used for arrow syntax in Dart functions?
A->
B=>
C::
D==>
✗ Incorrect
The arrow syntax uses the symbol => to return a single expression from a function.
What does this arrow function return? int triple(int n) => n * 3;
An * 3
B3
Cn + 3
DError
✗ Incorrect
The function returns the value of n multiplied by 3.
Can arrow functions contain multiple lines of code?
ANo, only one expression
BOnly if they return void
CYes, if wrapped in curly braces
DYes, always
✗ Incorrect
Arrow functions can only have one expression. For multiple lines, use normal function syntax.
Which is a valid arrow function in Dart?
Avoid greet() { print('Hello'); }
Bvoid greet() => print('Hello'); print('Bye');
Cvoid greet() => { print('Hello'); }
Dvoid greet() => print('Hello');
✗ Incorrect
Option D is a valid arrow function with a single expression. Option C uses curly braces incorrectly. Option B has two expressions.
Why use arrow syntax in Flutter functions?
ATo write longer functions
BTo avoid using return statements in all functions
CTo make code shorter and clearer for simple returns
DTo declare variables
✗ Incorrect
Arrow syntax helps write shorter and clearer code when the function returns a single expression.
Explain how to write a function using arrow syntax in Dart and when it is appropriate to use it.
Think about simple functions that just return a value.
You got /4 concepts.
Describe the difference between a normal function and an arrow function in Flutter with an example.
Compare how you write a function with and without arrow syntax.
You got /4 concepts.
Practice
(1/5)
1. What does the arrow syntax => in a Flutter function do?
easy
A. It marks a function as private.
B. It declares a function without any return value.
C. It creates a function that returns the expression after the arrow.
D. It defines a function that runs asynchronously.
Solution
Step 1: Understand arrow syntax meaning
The arrow => is a shortcut for functions that return a single expression.
Step 2: Compare with normal function
Instead of writing curly braces and return, arrow syntax returns the expression directly.
Final Answer:
It creates a function that returns the expression after the arrow. -> Option C
Quick Check:
Arrow syntax = single expression return [OK]
Hint: Arrow syntax means single expression return [OK]
Common Mistakes:
Thinking arrow means no return value
Confusing arrow with async keyword
Assuming arrow marks function private
2. Which of the following is the correct arrow function syntax in Flutter to return the sum of two integers a and b?
easy
A. int sum(int a, int b) => a + b;
B. int sum(int a, int b) { a + b; }
C. int sum(int a, int b) => { return a + b; };
D. int sum(int a, int b) => return a + b;
Solution
Step 1: Check arrow function syntax
Arrow functions use => followed by an expression without braces or return keyword.
Step 2: Validate each option
int sum(int a, int b) => a + b; correctly uses arrow syntax with expression a + b. The other options misuse braces or return keyword.
Final Answer:
int sum(int a, int b) => a + b; -> Option A
Quick Check:
Arrow function = expression after => without braces [OK]
Hint: Arrow functions omit braces and return keyword [OK]
Common Mistakes:
Adding braces {} with arrow syntax
Using return keyword after arrow
Missing semicolon at end
3. What is the output of this Flutter Dart code?
int multiply(int x, int y) => x * y;
void main() {
print(multiply(3, 4));
}
medium
A. Error
B. 7
C. 34
D. 12
Solution
Step 1: Understand the multiply function
The function returns the product of x and y using arrow syntax.
Step 2: Calculate multiply(3, 4)
3 * 4 equals 12, so the print statement outputs 12.
Final Answer:
12 -> Option D
Quick Check:
3 * 4 = 12 [OK]
Hint: Multiply returns product directly with arrow syntax [OK]
Common Mistakes:
Adding numbers instead of multiplying
Confusing output as string concatenation
Thinking arrow syntax causes error
4. Identify the error in this Flutter Dart function using arrow syntax:
int add(int a, int b) => {
return a + b;
};
medium
A. Missing semicolon after return statement.
B. Arrow functions cannot have braces and return statements.
C. Function must be async to use arrow syntax.
D. Function parameters must be optional.
Solution
Step 1: Check arrow function syntax rules
Arrow syntax functions must have a single expression without braces or return keyword.
Step 2: Analyze the given code
The code uses braces and return inside an arrow function, which is invalid syntax.
Final Answer:
Arrow functions cannot have braces and return statements. -> Option B
Quick Check:
Arrow syntax = single expression, no braces [OK]
Hint: Arrow syntax disallows braces and return keyword [OK]
Common Mistakes:
Adding braces {} in arrow functions
Using return keyword after arrow
Thinking arrow requires async keyword
5. You want to write a Flutter function using arrow syntax that returns the length of a string only if it is not empty; otherwise, it returns zero. Which is the correct function?
hard
A. int lengthOrZero(String s) => s.isNotEmpty ? s.length : 0;
B. int lengthOrZero(String s) => if (s.isNotEmpty) s.length else 0;
C. int lengthOrZero(String s) => { return s.length; };
D. int lengthOrZero(String s) => s.length > 0;
Solution
Step 1: Use arrow syntax with conditional expression
Arrow functions can return a single expression, here a ternary operator checks if string is not empty.
Step 2: Validate each option
int lengthOrZero(String s) => s.isNotEmpty ? s.length : 0; correctly uses ternary with s.isNotEmpty. int lengthOrZero(String s) => if (s.isNotEmpty) s.length else 0; uses invalid if-else syntax. int lengthOrZero(String s) => { return s.length; }; uses braces and return which is invalid for arrow. int lengthOrZero(String s) => s.length > 0; returns a boolean instead of int.
Final Answer:
int lengthOrZero(String s) => s.isNotEmpty ? s.length : 0; -> Option A
Quick Check:
Arrow + ternary = conditional return [OK]
Hint: Use ternary inside arrow for conditional return [OK]
Common Mistakes:
Using if-else instead of ternary in arrow
Adding braces and return in arrow function
Returning boolean (length > 0) instead of int
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.