Skip to content
BENIBIX Learn

Pass by Value

Development🧩 Programming ConceptsFunctions

Also known as Call by Value, Passing by Value

The function gets a copy. Changing the copy leaves the first one alone.

Think of it like

It is like giving a friend a copy of your drawing. They can scribble on it, and yours stays clean.

Example

A score of 5 is passed to a function that adds 1. Inside it is 6, but back outside the score is still 5.

Why it matters

It tells you whether a function can change your data. Guess wrong and a bug hides in a value you thought was safe.

Definition

Pass by value means a function gets a copy of each argument. If the function changes its copy, the caller's value stays the same. C, Java and many other languages pass simple values like numbers this way. It keeps functions from changing things by surprise. The cost is copying, which can be slow for very big data.

Example code

cpp
void addOne(int n) { n = n + 1; }

int score = 5;
addOne(score);
// score is still 5
The function changed its own copy. The caller's score did not move.
On the left, the function changes a copy. The first value still reads 5.

Common mistake

Expecting a change inside the function to show outside. Return the new value instead.

Key takeaways

  1. 1
    A copy goes in. The function never sees the original.
  2. 2
    Original is safe. Changes stay inside the function.
  3. 3
    Copying costs. Big data takes time to copy.

Compare

Learn it properly