Beginner Topic 12 of 54 Programming Fundamentals
Functions
Reusable named blocks of code that accept inputs and may return a value.
How it works
A function packages reusable logic behind a name. Calling it pushes a stack frame holding parameters, local variables and a return address; returning pops the frame and hands back a result to the caller. Functions let you break a large problem into small pieces, hide implementation details behind a signature and test each piece on its own.
Step-by-step walkthrough
- The caller invokes add(2, 3).
- The function runs with its own local variables.
- It computes 5.
- The result returns to the caller.
- The caller continues with the value 5 and the function's locals vanish.
Where it is used
Splitting a program into small testable parts such as calculate_tax(amount), reusing the same logic from many places and naming a step so that the calling code reads like a description of what happens are the main uses. Libraries are collections of functions.
Common pitfall
Functions that do too much, take many parameters or rely on hidden global state are hard to test and reuse. Duplicated code is a sign a function is missing. Aim for one clear job per function, a descriptive name and a short body.
Where it fits in the course
Functions is topic 12 of 54 in Programming Fundamentals, rated Beginner. It follows While Loops. The next topic is Parameters and Arguments.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Programming Fundamentals quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Functions?
Where do people use Functions?
What is the most common mistake with Functions?
What should I learn before this topic?
How can I practice it?
Is Functions hard to learn?
How long does it take to learn Functions?
Which topics are related to Functions?
What should I study after Functions?
Related topics
- While Loops (Programming Fundamentals)
- Parameters and Arguments (Programming Fundamentals)
- For Loops (Programming Fundamentals)
- Return Values (Programming Fundamentals)
- Functions and Relations (Theory & Discrete Math)
- Functions and Graphs (Math for Computing)
- Aggregate Functions (Databases)