0
0
Rubyprogramming~10 mins

Pure functions concept in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pure functions concept
Call function with input
Compute output using input only
Return output
No side effects
Same input => Same output every time
A pure function takes input and returns output only based on that input, without changing anything outside or depending on outside state.
Execution Sample
Ruby
def add(a, b)
  a + b
end

result = add(2, 3)
puts result
This code defines a pure function 'add' that returns the sum of two numbers and prints the result.
Execution Table
StepActionInputComputationOutputSide Effects
1Call add functiona=2, b=3Calculate 2 + 35None
2Return from adda=2, b=3Return 55None
3Print resultresult=5Output 5 to console5Console output
4End---No side effects in function
💡 Function ends after returning output; no side effects occurred inside the function.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
a-2222
b-3333
result--555
Key Moments - 3 Insights
Why does the function always return the same output for the same inputs?
Because the function uses only its input parameters to compute the output, as shown in execution_table step 1 and 2, it does not depend on or change anything outside.
Does the function change any variables outside itself?
No, the function has no side effects. The variable_tracker shows only internal variables and the result variable outside is assigned after the function returns.
What counts as a side effect in this example?
Printing to the console is a side effect, but it happens outside the function after it returns, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of the function at step 2?
A5
B2
C3
Dnil
💡 Hint
Check the 'Output' column at step 2 in the execution_table.
At which step does the function produce a side effect?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Side Effects' column in the execution_table.
If the function used a global variable inside, how would the output behavior change?
AOutput would always be the same for same inputs
BFunction would have no output
COutput might change even with same inputs
DFunction would stop working
💡 Hint
Pure functions depend only on inputs; using global variables breaks this, as explained in key_moments.
Concept Snapshot
Pure functions:
- Always return same output for same inputs
- No side effects (no changing outside state)
- Output depends only on input parameters
- Easy to test and predict
- Example: def add(a,b); a+b; end
Full Transcript
This visual execution shows how a pure function works in Ruby. The function 'add' takes two inputs and returns their sum. It does not change anything outside itself, so it has no side effects. The execution table traces each step: calling the function, computing the sum, returning the result, and printing it. Variables 'a' and 'b' keep their values, and 'result' stores the output. Key moments clarify why the output is always the same for the same inputs and what counts as side effects. The quiz tests understanding of output, side effects, and the impact of using external variables. Pure functions are simple, predictable, and reliable building blocks in programming.