← Back to Programming Fundamentals
10

Functions, Blocks, and Return: Reuse Logic Without Copying Code

Learn to group one task into a function, pass data into it with parameters, and return a useful result with `return` so your code becomes easier to reuse and explain.

📘 Theory

Why Functions Matter So Much

A function avoids repeated logic and forces you to think of one task as one reusable unit.

1

If you calculate a total many times, it makes little sense to rewrite the same structure over and over.

2

A function lets you encapsulate that task, give it a useful name, and call it whenever you need it with different data.

The Basic Structure of a Function

At this stage you do not need advanced variations. You need to understand name, parameters, block, and return value.

1

`calculateTotal` is the name of the function. `price` and `shipping` are parameters: they stand for the data that comes in. `return total` sends the result back so the rest of the program can use it.

2

This introduces a very important idea: separating the logic of the calculation from the moment you use it.

Parameters: The Logic Stays, the Data Changes

Parameters let you reuse the same idea with different inputs.

What It Means to Return a Value

Returning a value means handing the result of the function back to the rest of the program.

1

Without `return`, a function may still do something, but it does not hand back a reusable result in the same way.

2

With `return`, you can save the result in a variable, use it inside another expression, or show it in the console later.

🧪 Learn by doing

Example Guided Example: Calculate the Total of a Purchase Group a repeatable operation into a clear function with a returned result.

🏁 Challenges

Challenge Challenge: Create a Function That Calculates a Double Write a function with one numeric parameter and return twice the received value.

🧰 Resources

Test

Check your knowledge with a test about Programming Fundamentals.

Test for Programming Fundamentals

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com