Coding Education

5 min read

What Is a Function in Programming? Definition & Examples

Hafiz Rahman

Hafiz Rahman

Lead Coding Instructor at Algonova Malaysia

What Is a Function in Programming? Definition & Examples

In programming, a function is a named, reusable block of code that performs one specific task and runs only when you "call" it. Unlike a function in mathematics — where an input like x always maps to a single output — a programming function is a set of instructions you write once and reuse anywhere. It may return a value, change something on the screen, or take no input and produce no visible result at all.

How a Function Works

A function has three parts: a name, an optional set of inputs (called parameters), and a block of instructions to run. You define it once, then "call" it by name whenever you need it. In Scratch, a child can build a custom block called "jump" that moves a sprite up and back down; every time they drop in the "jump" block, the same steps run — no need to rebuild them. In Python the same idea looks like def jump(): followed by the instructions, and you run it simply by typing jump().

Programmers rely on functions because they follow the DRY principle — "Don't Repeat Yourself." Writing the jump logic once and calling it ten times keeps code short, easier to read, and simpler to fix: change the function in one place and every call updates automatically. Even a small game can call the same function hundreds of times.

Why It Matters for Kids

A function is one of the first "big ideas" a child meets in coding, and it builds real thinking skills — breaking a problem into smaller, named steps is the same logic behind KSSR problem-solving and wider STEM learning. Learning to write a function teaches a child to plan before they build. Kids explore this hands-on in Algonova coding classes, and our guide on what coding is for kids gives more context.

Want to see your child write their first function? Book a free trial class and watch them build one live.