Concept Flow - Calling functions
Define function
Call function
Execute function body
Return to caller
Continue main script
First, you define a function, then you call it. The script jumps to the function, runs its commands, then returns to continue.
greet() {
echo "Hello, friend!"
}
greet| Step | Action | Evaluation | Output |
|---|---|---|---|
| 1 | Define function greet | Function stored | |
| 2 | Call greet | Jump to greet function | |
| 3 | Execute echo inside greet | Print message | Hello, friend! |
| 4 | Return from greet | Back to main script | |
| 5 | End of script | No more commands |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|---|
| greet (function) | undefined | defined | called | executed | returned | defined |
Define a function with name() { commands; }
Call it by writing its name alone.
Calling jumps to function, runs commands, then returns.
Functions do not run when defined, only when called.
Use functions to reuse code and organize scripts.