Function
A function is a reusable block of code designed to perform a specific task.
It helps organize programs into smaller, manageable parts.
Why functions are important
Functions allow you to:
Avoid repeating code
- Organize logic clearly
- Reuse code multiple times
- Simplify complex programs
Instead of writing the same code again, you can call a function.
How a function works
A function usually has:
- A name
- Optional parameters (inputs)
- A block of code
- A return value (optional)
Basic structure:
To execute it:
Function with parameters
Functions can receive input values.
Calling the function:
Output:
Hello John
Return value
A function can return a result.
Using the result:
Here, result becomes 8.
Why learning functions matters
Functions help you:
- Build structured programs
Improve readability
Reduce errors
- Create modular code
Large programs are built from many small functions.
Simple example
Think of a function like a coffee machine: You press a button (call the function), it performs a task, and gives you a result.
Related terms
- What is Parameter?
- What is Return Value?
- What is Scope?
Source
Simplified from general programming documentation and Wikipedia.