Declaring Functions and Return Types
Define a function and return a value.
Declaring Functions and Return Types is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Functions Exist
A function is a named block of code you can run again and again. It lets you write logic once and reuse it everywhere. 🧩
The Basic Shape
Every Dart function has a return type, a name, parentheses for inputs, and a body in braces. That shape stays the same as functions grow.
int double(int x) {
return x * 2;
}Calling a Function
You run a function by writing its name followed by parentheses. This is called a call, and any values inside go to the parameters.
var result = double(5); // 10The return Keyword
The return keyword sends a value back to whoever called the function. Execution stops the moment return runs.
int add(int a, int b) {
return a + b;
}Declaring the Return Type
The word before the function name is its return type. Writing it clearly tells readers and the compiler exactly what comes back.
String greet() {
return "Hi there";
}Returning Nothing With void
When a function does work but returns no value, mark its type as void. It runs for its side effect, like printing.
void sayHi() {
print("Hi!");
}Parameters Are Inputs
The names inside the parentheses are parameters. They act like local variables filled with the values you pass in.
int square(int n) {
return n * n;
}Multiple Parameters
Separate several inputs with commas. Each parameter gets its own type, and order matters when you call the function.
double area(double w, double h) {
return w * h;
}Type Inference on Return
If you omit the return type, Dart infers it from what you return. Still, naming the return type keeps your code clearer and safer.
addOne(int x) {
return x + 1; // inferred int
}Returning a Computed Value
You can return the result of an expression directly. Dart evaluates it first, then sends that single value back to the caller.
bool isEven(int n) {
return n % 2 == 0;
}Functions Stay DRY
Wrapping logic in a function keeps your program DRY: don't repeat yourself. Fix it once, and every caller benefits instantly. ✨
Quick Check
Let's test what you learned about return types.
Recap
You can now declare functions with a clear return type, pass parameters, and send values back with return. That is the heart of reusable code. 🎉
Frequently asked questions
Is the “Declaring Functions and Return Types” lesson free?
Yes — the full text of “Declaring Functions and Return Types” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Declaring Functions and Return Types”?
Define a function and return a value. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declaring Functions and Return Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Functions and Return Types
- Positional vs Named Parameters
- Default Values and required
- Functions as First-Class Values