Skip to content
RCSI USA Tech Notes

Plain-spoken notes on learning to code, desk gadgets, apps, streaming and the software that quietly runs work, home and travel.

New on

Navigate

Education4 min read

What Is a Function in Programming? Parameters, Return Values and Methods

A function is a named, reusable block of code. Learn how parameters and return values work, how a function differs from a method and which slips to avoid.

Code, coding and computer

A function is a named block of code that does one job and can be used again whenever that job comes up. You write the instructions once, give them a name, and from then on a single line that "calls" the name runs the whole block. Most functions also accept input, called parameters, and hand back a result, called a return value.

Think of the button on a coffee machine labelled "flat white". Nobody presses a dozen separate controls for grinding, tamping, steaming and pouring every time. The sequence was set up once, and the button triggers it. A function is that button for a program.

The parts of a function

PartWhat it doesIn a "convert temperature" function
NameHow the rest of the program refers to itcelsius_to_fahrenheit
ParametersPlaceholders for the input it expectscelsius
BodyThe instructions that run when it is calledMultiply by 9, divide by 5, add 32
Return valueThe result handed back to the callerThe temperature in Fahrenheit

When the program later runs celsius_to_fahrenheit(20), the value 20 is fed into the parameter, the body does its arithmetic, and 68 comes back to wherever the call was made. The caller can store that result in a variable, print it or pass it straight into another function.

Parameters vs arguments

These two words are often used interchangeably, but the distinction is simple once seen. A parameter is the name in the function's definition, a slot waiting to be filled. An argument is the actual value supplied when the function is called. In the example above, celsius is the parameter and 20 is the argument. Many languages also allow default values, so a parameter can be left out and still receive something sensible.

Returning a value vs printing it

This is the single most common point of confusion for new programmers. Printing shows something on the screen for a human to read. Returning hands a value back to the program so the code can keep working with it. A function that only prints its answer looks correct while you watch it, yet the rest of the program receives nothing useful. In Python, for example, a function without a return statement quietly hands back a special "nothing" value, which then causes puzzling errors further along.

A good rule: let functions return results, and decide separately where and how those results get displayed.

Why functions matter

  • No repetition. If the same five lines appear in three places, a fix has to be made three times. Put them in a function and the fix happens once.
  • Readable code. A line such as send_receipt(order) explains itself. The details live inside the function for anyone who needs them.
  • Easier testing. A small function with clear input and output can be checked on its own with a handful of example values.
  • Teamwork. One person can write the function while another uses it, as long as both agree on the name, the parameters and what comes back.

Functions also grow naturally out of planning. When an outline written in pseudocode contains a step like "calculate the shipping cost", that step is a strong candidate for its own function.

Function vs method

A method is a function that belongs to an object or a class. The behaviour is the same, a named block of code with inputs and an output, but a method is called through the thing it belongs to and can work with that thing's data.

  • A standalone function: len(name) in Python, which measures the length of whatever it is given.
  • A method: name.upper(), which belongs to text values and returns an upper-case copy of that particular piece of text.

In some languages, such as Java, almost everything lives inside a class, so nearly every function is technically a method. In others, both kinds sit side by side. For a beginner, the practical takeaway is that the ideas of parameters and return values apply equally to both.

Built-in functions and your own

Every language ships with ready-made functions for common jobs: printing, rounding numbers, measuring text, reading files. Libraries add many more. Learning to read their documentation, especially the description of parameters and return values, is a skill in itself. Writing your own functions comes next, and it is where programs start to feel organised rather than like one long list of commands.

Slips to watch for

  1. Doing too much in one function. If a name needs the word "and", such as load_and_clean_and_save, it probably wants splitting.
  2. Forgetting to call it. Defining a function does not run it. Something has to call it, with brackets.
  3. Mixing up the order of arguments. Passing width and height the wrong way round produces believable but wrong results.
  4. Relying on outside variables. A function that quietly reads or changes values defined elsewhere is harder to test. Passing what it needs as parameters keeps it predictable.

Quick answers

Can a function call another function?

Yes, and that is how larger programs are built: small functions combined by slightly bigger ones. A function can even call itself, a technique called recursion, which is worth meeting once the basics feel comfortable.

Does a function always need parameters?

No. Some functions need no input at all, such as one that returns the current date. Others take several. What matters is that the inputs are clearly named and the output is predictable.

How do I get better at writing them?

Practise by rewriting old exercises. Find repeated lines, move them into a function and give it a precise name. The steady, hands-on routine behind how students practise writing code applies here too: type it, break it, fix it.

Also in Education