Function

Also known as: procedure, subroutine, method, def

definition

A named, reusable block of code that performs a specific task and can optionally accept inputs and return a result. Functions are the primary abstraction in programming; nearly all software is organized around them.

๐Ÿ’ก Intuition

Like a recipe: you name it, write it once, and call it whenever you need it.

Core Idea

Functions break programs into manageable pieces, enable reuse, and reduce repetition.

๐Ÿ”ฌ Example

def square(x): return x * x โ€” calling square(5) returns 25 without rewriting the logic.

๐ŸŽฏ Why It Matters

Functions are the primary abstraction in programming; nearly all software is organized around them.

โš ๏ธ Common Confusion

Defining a function doesn't run it โ€” you must call it. Parameters (defined) differ from arguments (passed in).

Related Concepts

How Function Connects to Other Ideas

To understand function, you should first be comfortable with function programming, return values and modular design. Once you have a solid grasp of function, you can move on to recursion and event handler.

Go Deeper

Frequently Asked Questions

What is Function in CS Thinking?

A named, reusable block of code that performs a specific task and can optionally accept inputs and return a result.

Why is Function important?

Functions are the primary abstraction in programming; nearly all software is organized around them.

What do students usually get wrong about Function?

Defining a function doesn't run it โ€” you must call it. Parameters (defined) differ from arguments (passed in).

What should I learn before Function?

Before studying Function, you should understand: function programming, return values, modular design.