Module 3~9 min
Introduction to Functions
Functions let you package a set of instructions so they can be reused wherever needed, instead of rewriting them every time.
What You'll Learn
- What a function is
- Why functions are useful
- A simple example of calling a function
What a Function Is
A function is a named, reusable block of code that performs a specific task. Once defined, it can be "called" -- run -- as many times as needed, from anywhere in the program.
Why Functions Help
Functions make code more organized and easier to maintain: fixing a bug inside a function fixes it everywhere that function is used, instead of needing to fix the same mistake in many places.
Example
A function named `calculateTotal` might be defined once, then called every time a total needs to be calculated throughout a program.
Key Concepts
FunctionReusabilityCalling a Function
Key Takeaways
- A function is a named, reusable block of code
- Functions can be called (run) from anywhere they're needed
- Functions make code easier to organize and maintain
Knowledge Check
What is a key benefit of using functions?