What is a Function in Programming?
A function is a named, reusable block of code that performs a specific task — it can take inputs (parameters), do some work, and return an output. Functions let programmers break a large problem into smaller, testable pieces instead of repeating the same logic everywhere.
A function is a self-contained block of code that you can call by name, optionally passing it input values, and which can send a result back to the code that called it.
- 1↓DefineWrite the function once with a name and parameters
- 2↓CallInvoke the function by name, passing arguments
- 3↓ExecuteThe function body runs using the passed-in values
- 4↓ReturnThe function sends a result back to the caller
- 5Use resultThe calling code uses the returned value
Try it: interactive calculator
Step-by-step worked examples
Write a function that converts Celsius to Fahrenheit and call it with 20°C.
Define: function toFahrenheit(c) { return c*9/5+32 }
Call: toFahrenheit(20)
Execute: 20*9/5+32 = 36+32
Return: 68 — so 20°C equals 68°FA function add(a, b) returns a + b. What does add(7, 5) return?
Call add(7, 5) passes a=7, b=5 Execute the body: a + b = 7 + 5 Return 12 The function call add(7,5) evaluates to 12
A function square(n) returns n*n. What is square(6)?
Call square(6) passes n=6 Execute: n*n = 6*6 Return 36 square(6) evaluates to 36
Flashcards
Quick quiz
Q1.A function double(n) returns n*2. What does double(9) return?
Q2.What is the term for the value a function sends back to its caller?
Q3.Why are functions useful in programming?
Q4.In toFahrenheit(c) { return c*9/5+32 }, what is c?
The full card deck, worked steps and AI-tutor support for “What is a Function in Programming?” are in Notek — study by hand before your exam.
Common mistakes
Confusing a function's parameter with the argument passed at call time. — Correct: Parameter is the placeholder name in the definition; argument is the real value used in the call.
Forgetting to return a value, then trying to use the function's result. — Correct: If a function should produce output, it must explicitly return it, or the caller gets nothing (undefined/null).
Writing one giant function that does everything. — Correct: Split work into small, focused functions — each doing one clear task — for easier testing and reuse.
Assuming calling a function twice always gives the same result. — Correct: If a function depends on external state (like a counter), calling it twice can give different results.
FAQ
What is a function in programming?
A function is a reusable named block of code that performs a task, optionally takes input parameters, and can return an output value.
What is a function formula?
In general form, output = f(input) — a function maps input arguments to a returned output through its body's logic.
What are examples of functions?
A toFahrenheit(c) function that converts temperature, or an add(a,b) function that returns a sum.
How do you calculate a function's output?
Substitute the arguments passed at the call into the function's parameters, then execute the body's logic to get the return value.




