0
0
Blockchain / Solidityprogramming~10 mins

Function declaration and syntax in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function declaration and syntax
Start
Declare function with name and parameters
Define function body with statements
Function ready to be called
Call function with arguments
Execute function body
Return value or end
End
This flow shows how a function is declared, then called, and how it runs its code and returns a result.
Execution Sample
Blockchain / Solidity
function greet(name) {
  return "Hello, " + name + "!";
}

let message = greet("Alice");
console.log(message);
This code declares a function greet that takes a name and returns a greeting message, then calls it with "Alice" and prints the result.
Execution Table
StepActionEvaluationResult
1Declare function greet with parameter nameFunction greet createdFunction ready to be called
2Call greet with argument "Alice"name = "Alice"Function execution starts
3Execute return statement"Hello, " + "Alice" + "!""Hello, Alice!"
4Assign return value to messagemessage = "Hello, Alice!"message holds "Hello, Alice!"
5Print messageconsole.log(message)Output: Hello, Alice!
6End of programNo more statementsProgram ends
💡 Program ends after printing the greeting message.
Variable Tracker
VariableStartAfter CallAfter ReturnFinal
nameundefined"Alice""Alice"undefined (local to function)
messageundefinedundefined"Hello, Alice!""Hello, Alice!"
Key Moments - 3 Insights
Why does the variable 'name' disappear after the function finishes?
Because 'name' is a parameter local to the function, it only exists during the function call (see execution_table step 3). After the function returns, 'name' is no longer accessible.
What happens if we call the function without parentheses?
Without parentheses, the function is not called but referenced as a value. The code in the function body does not run (see execution_table step 2 where parentheses trigger the call).
Why do we use 'return' inside the function?
'return' sends a value back to where the function was called (see execution_table step 3). Without return, the function returns undefined by default.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'message' after step 4?
Aundefined
B"Alice"
C"Hello, Alice!"
Dnull
💡 Hint
Check the 'Result' column in step 4 where 'message' is assigned the return value.
At which step does the function greet actually run its code?
AStep 3
BStep 2
CStep 1
DStep 5
💡 Hint
Look at the 'Action' and 'Evaluation' columns where the return statement is executed.
If we remove the 'return' statement, what will 'message' hold after the function call?
A"Hello, Alice!"
Bundefined
C"Alice"
DError
💡 Hint
Recall that without 'return', functions return undefined by default (see key_moments about return).
Concept Snapshot
Function declaration syntax:
function name(parameters) {
  // code
  return value;
}

- Declare function with name and parameters
- Use return to send back a value
- Call function with parentheses and arguments
- Local variables exist only during call
Full Transcript
This lesson shows how to declare a function in blockchain programming. First, you write the function keyword, then the function name and parameters inside parentheses. Inside curly braces, you write the code the function runs. The return statement sends a value back to where the function was called. When you call the function with parentheses and arguments, the code inside runs, and the return value can be saved in a variable. Variables inside the function only exist while it runs. This example shows a greet function that takes a name and returns a greeting message. The program calls greet with "Alice" and prints the result "Hello, Alice!".