Function
Function
A piece of program code that bundles a specific task into one unit, ready to be called and reused whenever needed.
In Simple Terms
A function is a piece of code that bundles up a task you use over and over again in a program. For example, there are functions built for all kinds of purposes, like showing text on screen or adding up a set of numbers. By calling these whenever you need them, you avoid having to write the same instructions again and again. They show up in every kind of program, from smartphone apps to game development.
Behind the Name
In everyday English, "function" means a role or purpose — what something is meant to do. Programming reuses that idea literally: a function is a piece of code with a defined job. The concept also traces back to mathematics, where a function is a rule that takes an input and produces an output through a fixed process — exactly how a function in code works too.
Take a Closer Look!
A function is a piece of code that bundles up a specific task in a program so it can be called and reused whenever it's needed.
When building a program, you often end up doing the same calculation or task over and over, and writing out the same instructions every single time is tedious and easy to get wrong.
That's where functions come in — you set up a commonly used task as a function ahead of time.
When you pass data into a function, it runs that data through a set process and hands back a result.
A good example is a function that takes two numbers and returns their sum.
Broadly speaking, there are two types: functions that come built into a programming language, and functions you write yourself.
Behind the scenes, all kinds of functions are constantly being called — whether it's displaying a button on a smartphone app's screen or pulling data out of a database.