Functions
Functions package reusable code with inputs and outputs.
About this topic
A function is a named, reusable block of code that performs a specific task. It can take inputs called parameters (the values passed in are arguments) and can give back an output using return. Functions let you break a large program into small pieces, avoid repetition and make code easier to test.
Defining a function is separate from calling it. For example, a function area(width, height) returns width times height, and calling area(3, 4) gives 12. Variables created inside a function are local, so they exist only while it runs, while global variables can be seen everywhere, although relying on them often causes bugs. Built-in functions such as print, len and round are already provided.
Good functions do one thing and have clear names. Functions let teams share work, since each person can write a piece without knowing the details of others. A common misconception is that print and return are the same; print shows a value to the user, while return hands the value back to the code that called it. Another is forgetting to call the function after defining it.
Key ideas
- Parameters and return values
- Scope
- Don't repeat yourself
Quick check
Use cases: where Functions helps
- Homework: review the key idea behind Functions before you start a problem set.
- Exam revision: use the quick check to test whether you can recall Functions without notes.
- Visual learning: play with the interactive lab to see how Functions changes when you adjust its inputs.
- Teaching a friend: explain Functions in your own words, then compare your version with the key ideas.
Computing ideas run phones, search engines, games and the wider internet.
Frequently asked questions
What is Functions?
Functions package reusable code with inputs and outputs.
What will I learn in Functions?
Parameters and return values. Scope. Don't repeat yourself.
Is Functions part of Computer Science?
Yes. It is one of 80 Computer Science topics on LearnViz HighSchoolEdu, with 3 key ideas, a live animation and a quick check.
Does the Functions lab work on a phone or tablet?
Yes. The animation resizes to fit your screen and supports touch, so you can drag, tap and slide with your finger.
How should I study Functions?
Read the summary and key ideas first, play with the interactive lab to see the idea in motion, then answer the quick check. If you get a question wrong, go back to the key idea it tests.
Do I need an account, and is my progress saved?
No account is needed. Marking a topic as learned is saved in your own browser, so it stays on that device and is not sent to a server.