0
0
Blockchain / Solidityprogramming~10 mins

Return values in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Return values
Function called
Execute function code
Return value encountered
Send value back to caller
Caller receives value
Use returned value
When a function runs, it does its work and then sends a value back to where it was called from.
Execution Sample
Blockchain / Solidity
function getBalance() {
  return 100;
}

let balance = getBalance();
console.log(balance);
This code defines a function that returns 100, then stores and prints that value.
Execution Table
StepActionEvaluationResult
1Call getBalance()Function startsExecution enters getBalance
2Execute return 100Return value 100Function ends, returns 100
3Assign returned valuebalance = 100Variable balance now 100
4Print balanceconsole.log(100)Output: 100
5EndNo more codeProgram stops
💡 Function returns value 100, which is assigned and printed; program ends.
Variable Tracker
VariableStartAfter getBalance callFinal
balanceundefined100100
Key Moments - 2 Insights
Why does the function stop running after return?
Because return sends the value back and immediately ends the function, as shown in step 2 of the execution_table.
What happens to the returned value?
It goes back to the caller and can be stored or used, like balance = 100 in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value returned by getBalance() at step 2?
Aundefined
B100
Cbalance
Dnull
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 2 in the execution_table.
At which step does the variable balance get its value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column for when balance is assigned in the execution_table.
If the return statement was removed, what would happen to balance?
Abalance would be undefined
Bbalance would be 100
Cbalance would be null
Dbalance would be NaN
💡 Hint
Without return, the function returns undefined by default; see variable_tracker for initial balance.
Concept Snapshot
Return values let a function send data back to where it was called.
Use 'return value;' inside the function.
After return, function stops running.
Caller can save or use the returned value.
Without return, functions return undefined by default.
Full Transcript
This visual trace shows how return values work in blockchain programming. When the function getBalance is called, it runs and hits the return statement which sends back the value 100. The function then stops running immediately. The caller receives this value and stores it in the variable balance. Finally, balance is printed to the console showing 100. Key points are that return ends the function and sends a value back, and the caller can use that value. If return is missing, the function returns undefined. This helps beginners see step-by-step how return values flow in code.