Logic and Programming
Functions Explained: Reusable Pieces of Code
Beginner · Concept · 6 min read · Updated October 30, 2026
A kitchen blender does one job. You put food in, press a button, and something comes out. You do not need to know how the motor works, and you can use the same blender for soup today and a smoothie tomorrow. A function in a program is a blender for code. It takes some input, does a fixed job, and hands back a result.
This entry explains how to define a function, how to give it input and get output back, and why that makes programs shorter and easier to fix. The running example is a temperature converter, because it is small, useful and easy to check by hand.
What a function is
A function is a named group of instructions that you can run whenever you want by using its name. Running it is called calling the function. You define the group once and call it as many times as needed.
You have already used functions without defining them. print() shows a value on the screen, len() counts the items in a collection and round() shortens a decimal. These come with Python and are called built-in functions. Writing your own follows the same pattern: a name, a pair of brackets, and possibly some input inside them.
Functions solve three problems at once:
- Repetition. If the same steps appear in five places, a change must be made in five places. In a function, the change is made once.
- Readability. A name such as
celsius_to_fahrenheittells the reader what happens without making them read the arithmetic. - Testing. A small function with a clear input and output is easy to check on its own, a theme that comes back in software testing basics.
Defining and calling a function
In Python, you define a function with the keyword def, followed by the name, brackets and a colon. The steps inside are indented, just like the blocks you see in decisions and loops.
def celsius_to_fahrenheit(celsius):
return celsius * 9 / 5 + 32
print(celsius_to_fahrenheit(0))
print(celsius_to_fahrenheit(100))
print(celsius_to_fahrenheit(37))
The output is:
32.0
212.0
98.6
Check the middle one by hand: 100 times 9 is 900, divided by 5 is 180, plus 32 is 212. The definition does nothing visible by itself. Only the three print lines, which call the function, make it run. Defining a function and calling it are separate steps, and mixing them up is a very common beginner stumble.
JavaScript writes the same function as function celsiusToFahrenheit(celsius) { return celsius * 9 / 5 + 32; }. Newer JavaScript code often uses arrow functions, which are a shorter spelling of the same idea.
Parameters and arguments
The name in the brackets of the definition, celsius, is a parameter. It is a placeholder for the input. The actual value you pass when calling, such as 100, is an argument. People mix these two words up all the time, and nobody will be confused if you do, but the difference is simple: parameters belong to the definition, and arguments belong to the call.
A function can have several parameters, separated by commas, or none at all. A parameter can also have a default value, which is used when the caller does not supply one:
def convert(celsius, decimals=1):
return round(celsius * 9 / 5 + 32, decimals)
print(convert(21.5))
print(convert(21.5, 0))
The first call uses the default of one decimal place: 21.5 times 9 is 193.5, divided by 5 is 38.7, plus 32 is 70.7. The second call passes 0, so the answer rounds to 71.0.
Return values
The return statement sends a result back to whoever called the function and ends the function at that point. The caller can then store the result, print it or pass it to another function.
def describe_temperature(celsius):
if celsius < 10:
return "cold"
elif celsius < 25:
return "mild"
else:
return "hot"
for c in [5, 21, 30]:
print(c, "C is", describe_temperature(c), "and", celsius_to_fahrenheit(c), "F")
This prints:
5 C is cold and 41.0 F
21 C is mild and 69.8 F
30 C is hot and 86.0 F
Notice how this example combines the earlier ideas. A function uses an if and else decision inside, and the call sits inside a loop. Small functions can be stacked like blocks to build larger programs.
Return versus print
Beginners often confuse return with print, because both seem to "show" the answer. They are different:
print |
return |
|
|---|---|---|
| What it does | Writes text to the screen | Hands a value back to the caller |
| Can the program use the value afterward? | No | Yes |
| What the caller receives | None (if there is no return) |
The value itself |
Here is the trap in code:
def show_only(celsius):
print(celsius * 9 / 5 + 32)
result = show_only(100)
print(result)
The first print inside the function shows 212.0. But the function has no return, so result receives None, and the second print shows None. If you plan to use an answer later, return it. Print only when the goal is to display something.
Scope: where variables live
A variable created inside a function exists only while the function runs. This is called local scope. In convert, the names celsius and decimals are visible only inside it. Code outside cannot read them, and that is a feature, not a limit. Because each function keeps its own private names, you can reuse a name like total in ten functions without them interfering with each other.
Variables created outside any function are global. A function can read them, but relying on many globals makes code hard to follow, because anything could change them. Passing values in through parameters and getting results out through return keeps the connections visible.
Writing good functions
Some habits make functions easier to live with:
- Do one job. If you struggle to name a function without using "and", it probably does two things.
- Choose a name that is a verb or a clear noun phrase.
calculate_tipandcelsius_to_fahrenheitsay what they do. - Keep it short. If a function runs past a screenful, look for a piece that deserves its own name.
- Document the surprising parts. Python lets you add a short description in the first line of the body, called a docstring.
- Avoid hidden changes. A function that changes data outside itself in secret is hard to trust.
Common mistakes
- Defining but never calling. The function does nothing until you call it. If nothing happens, check that a call exists.
- Forgetting the brackets on a call. In Python,
celsius_to_fahrenheitwithout brackets refers to the function itself and does not run it. - Using
printwhen you meantreturn. The caller getsNone, which later causes confusing errors such as trying to addNoneto a number. - Passing arguments in the wrong order.
convert(0, 21.5)is not the same asconvert(21.5, 0). Match the order in the definition. - Expecting a local variable to exist outside. Names created inside a function disappear when it finishes. Return the value you need.
- Making one giant function. A long function that does everything is as hard to read as a program with no functions. Split it.
FAQ
What is the difference between a function and a method?
A method is a function that belongs to an object or a type, and you call it with a dot, like "text".upper(). For a beginner, treat it as a function with a different way of being called.
Can a function call another function?
Yes, and it is very common. describe_temperature above could call celsius_to_fahrenheit to include the Fahrenheit value. A function can even call itself, which is called recursion.
Do I need to return something?
No. A function can simply perform an action, such as saving a file, and return nothing. In Python, such a function quietly returns None.
How many parameters should a function have?
As few as the job needs. Three or four is easy to follow. A long list of parameters is a hint that the function does too much or that related values should be grouped together.
Where can I practice?
Take a small calculation you already know, such as a tip or a unit conversion, and wrap it in a function. The Logic and Programming area has more small topics to combine it with.
Sources
- More Control Flow Tools: Defining Functions (opens in a new tab) — Python Software Foundation (docs.python.org). Official tutorial sections on def, default arguments and return values
- Functions (JavaScript Guide) (opens in a new tab) — MDN Web Docs (Mozilla). How JavaScript defines and calls functions
- Built-in Functions (opens in a new tab) — Python Software Foundation (docs.python.org). The ready-made functions such as print, len and round
Next in Logic and Programming: Lists, Arrays and Dictionaries: Storing Collections of Data