What a function is and why you write them

A function is a block of code that does one specific job. You write it once, then use it as many times as you need without rewriting the same lines over and over. Functions let you break a large problem into smaller, named pieces that are easier to understand and fix.

Think of a function like a recipe. You write down the steps once — "mix flour, add eggs, bake at 350 degrees" — and then whenever you need that cake, you follow the recipe instead of inventing the process from scratch each time. In code, you write the function once, and then you call it by name whenever you need that job done.

Functions also make your code safer. If you need to change how something works, you change it in one place — inside the function — and every place that uses that function automatically gets the fix. Without functions, you would have to find and change the same code in dozens of places, and you would probably miss some.

Key Takeaways

  • A function is a named block of code that performs one task and can be reused throughout your program.
  • You define a function once with a name and the steps it takes, then call that function by name whenever you need it to run.
  • Functions can accept input values (called parameters) and return output values (called return values) so they can work with different data each time.
  • Organizing code into functions makes programs easier to read, test, and change without breaking other parts of your code.
  • Most programming languages follow the same basic pattern: define the function, then call it by name with any needed information in parentheses.

The basic structure of a function

Every function has a few required parts. First comes the keyword that tells the language you are defining a function — in JavaScript it is function, in Python it is def. Then comes the name you choose for the function. Then come empty parentheses (or parentheses with parameter names inside). Then comes the body — the actual code that runs when you call the function, usually wrapped in curly braces or indented.

Here is a straightforward JavaScript function that adds two numbers:

function addNumbers(a, b) {   return a + b; }

The word function tells JavaScript this is a function. addNumbers is the name you chose. a and b are the parameters — the input values the function expects. The code inside the braces is what runs. The word return sends a value back out to whoever called the function.

In Python, the same function looks like this:

def addNumbers(a, b):   return a + b

Python uses def instead of function, and indentation instead of braces, but the idea is identical.

Parameters and return values

A parameter is a placeholder for information the function needs. When you define the function, you list the parameter names in the parentheses. When you call the function, you provide the actual values — called arguments — in the same order.

If you call addNumbers(5, 3), the function receives 5 as a and 3 as b, adds them, and sends back 8. If you call addNumbers(100, 50), it receives 100 and 50, adds them, and sends back 150. The function does the same job each time, but with different numbers.

A return value is the result the function sends back. Not every function needs to return something — some functions just perform an action, like printing text to the screen or saving data to a file. But if you want the function to give you an answer, you use the return keyword followed by the value you want to send back.

A function can have zero parameters, one parameter, or many. It can return one value, no value, or (in some languages) multiple values at once. The choice depends on what job the function needs to do.

Calling a function

Once you have defined a function, you call it by writing its name followed by parentheses. If the function expects parameters, you put the values inside the parentheses, separated by commas.

Using the addNumbers function from above, you would call it like this:

let result = addNumbers(10, 20); console.log(result);

The first line calls addNumbers with 10 and 20, stores the returned value (30) in a variable called result, and the second line prints it. You can also call the function without storing the result, or use the result directly in another operation.

The order of the arguments matters. If you call addNumbers(20, 10), you get 30 — the same answer because addition is reversible. But if you had a function that subtracts, subtract(20, 10) gives 10, while subtract(10, 20) gives -10. Always pass arguments in the order the function expects.

Scope and variable visibility

Variables created inside a function exist only inside that function — they are not visible to the rest of your code. This is called scope. If you create a variable named x inside a function, and you also have a variable named x outside the function, they are two separate variables that do not interfere with each other.

This separation is useful because it means you can name variables inside a function without worrying that you will accidentally change a variable with the same name somewhere else in your program. It also means you cannot accidentally use a variable from inside a function in the rest of your code — if you need a value from inside a function, the function must return it.

Variables created outside a function (called global variables) can usually be read from inside a function, but changing them from inside a function is risky and usually avoided. The safest pattern is: pass what the function needs as parameters, and return what the function produces.

Common mistakes when writing functions

One frequent mistake is forgetting the parentheses when you call a function. If you write addNumbers instead of addNumbers(5, 3), you are referring to the function itself, not calling it. The code will not run the function — it will just mention that the function exists.

Another mistake is passing the wrong number of arguments. If a function expects two parameters and you only give it one, the second parameter will be undefined, and the function may crash or produce wrong results. If you give it more arguments than it expects, most languages will ignore the extra ones, but this usually means you made a mistake.

A third mistake is forgetting to return a value when you need one. If you write a function that calculates something but do not include a return statement, the function will run but send back nothing (or a default value like undefined or null). Your code will then try to use that empty value, which causes errors later.

Finally, giving a function a name that does not describe what it does makes your code hard to read. addNumbers is clear. doThing or process is not. A good function name tells the next person reading your code (or you, six months later) what the function actually does.

When to create a function

Create a function when you find yourself writing the same code more than once. If you write five lines of code to validate an email address, and you need to do that in three different places in your program, write a function called validateEmail and call it from those three places instead.

Also create a function when a piece of code is complex enough that it deserves its own name and explanation. Even if you only use it once, breaking it into a named function makes the overall program easier to understand. Someone reading your code can see calculateMonthlyPayment() and when ready know what that section does, without having to read the math inside.

Do not create a function for every tiny operation. A function that does nothing but return a single value or perform a trivial calculation adds clutter rather than clarity. Use judgment: if the function has a clear job and a clear name, it probably belongs. If it is just wrapping one or two lines of code, it probably does not.

Frequently Asked Questions

Can a function call another function?

Yes. A function can call any other function, including itself (which is called recursion). This is how large programs are built — functions call other functions, which call other functions, breaking the work into smaller and smaller pieces until each piece is straightforward enough to understand and test.

What is the difference between a parameter and an argument?

A parameter is the placeholder name you write when you define the function. An argument is the actual value you provide when you call the function. In function add(a, b), a and b are parameters. When you call add(5, 3), the numbers 5 and 3 are arguments.

Do I have to return a value from every function?

No. Some functions perform an action (like printing text or saving a file) and do not need to return anything. Other functions calculate a result and return it. Use a return statement only when the function needs to send a value back to the code that called it.

What happens if I call a function before I define it?

In JavaScript, functions defined with the function keyword are "hoisted" — moved to the top of their scope — so you can call them before they appear in your code. In Python and many other languages, you must define a function before you call it, or you will get an error.

Can I change a parameter value inside a function?

Yes, but it only affects the copy inside the function. If you pass a number or string to a function and change it inside the function, the original variable outside the function is not affected. This is called "pass by value." Objects and arrays behave differently in some languages, so check your language's documentation if you need to modify complex data.