DEV Community

Fidel Okumu
Fidel Okumu

Posted on

Functions: Writing Reusable Code

Introduction
As my scripts grew longer, I noticed the same blocks of logic showing up again and again.
Functions lets a user write a piece of logic once and reuse it anywhere, with different data each time.

Defining and Calling Functions
Defining a function only saves its instructions — it doesn't run anything yet:

Calling it is what actually executes the code:

Parameters and Arguments
A parameter is the placeholder name written inside the function definition — an empty slot waiting for a value.
An argument is the real value handed over when the function is actually called.

A function can take multiple parameters

Return Values
Return sends a value back to whoever called the function, so it can be stored and reused elsewhere — unlike print(), which only displays something once.

An important detail: as soon as a function hits a return statement, it exits immediately. Any code written after return inside that function never runs

What I Learned
Functions reframed how I think about writing code — instead of asking "what should this line do," I started asking "what small reusable task does this represent?, and what inputs does it need?." Understanding parameters versus arguments, and that variables inside a function have their own separate scope

Top comments (0)