🎓 Prepared by students from Boğaziçi University

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.

Short answer

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.

How a Function Call Works
  1. 1
    Define
    Write the function once with a name and parameters
  2. 2
    Call
    Invoke the function by name, passing arguments
  3. 3
    Execute
    The function body runs using the passed-in values
  4. 4
    Return
    The function sends a result back to the caller
  5. 5
    Use result
    The calling code uses the returned value
01

Try it: interactive calculator

Function output (Fahrenheit)
68°F
= 20*9/5+32
02

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°F

A 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
03

Flashcards

04

Quick quiz

Q1.A function double(n) returns n*2. What does double(9) return?

Correct answer: C. double(9) computes 9*2 = 18.

Q2.What is the term for the value a function sends back to its caller?

Correct answer: C. The return value is what the function outputs after running.

Q3.Why are functions useful in programming?

Correct answer: B. Functions let you reuse the same logic anywhere it's needed, without duplicating code.

Q4.In toFahrenheit(c) { return c*9/5+32 }, what is c?

Correct answer: B. c is the parameter that holds whatever Celsius value is passed in when the function is called.
📄Download this topic as a printable worksheet (PDF)Summary + 10 questions + answer key — print it, share it in class.
Study better with Bounlu apps
Notek
Notek

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.

Get it free
Notek 1Notek 2Notek 3Notek 4Notek 5
05

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.

06

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.

Related topics