0
0
Typescriptprogramming~10 mins

Void type for functions in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Void type for functions
Function declared with void return type
Function called
Function runs code inside
No value returned
Execution continues without return value
This flow shows how a function with void return type runs code but does not return any value.
Execution Sample
Typescript
function greet(): void {
  console.log("Hello!");
}
greet();
This code defines a function that prints a message and returns nothing (void).
Execution Table
StepActionEvaluationResult
1Function greet declared with void return typeNo return value expectedFunction ready to be called
2greet() calledFunction starts runningInside greet function
3console.log("Hello!") executedPrints messageOutput: Hello!
4Function ends without returnNo value returnedReturn value is void (undefined)
5Execution continues after greet()No value to useProgram continues normally
💡 Function ends without returning a value because its return type is void
Variable Tracker
VariableStartAfter greet() callFinal
greetfunction declaredfunction runningfunction ended (void return)
Key Moments - 2 Insights
Why does the function not return any value even though it runs code?
Because the function's return type is void, it means it does not return any value. The execution_table row 4 shows the function ends without returning.
Can we assign the result of a void function to a variable?
No, because the function returns void (undefined), assigning its result to a variable will give undefined. See execution_table row 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is printed to the console?
A"Hello!"
Bundefined
Cvoid
DNo output
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does the function end without returning a value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step mentioning 'Function ends without return' in execution_table.
If we tried to assign greet() to a variable, what would the variable hold?
AThe string "Hello!"
Bvoid (no value)
Cundefined
DAn error
💡 Hint
Refer to variable_tracker and execution_table step 4 about return value.
Concept Snapshot
Void type for functions in TypeScript:
- Declared as function name(): void
- Means function returns no value
- Function can run code but no return statement needed
- Calling such function returns undefined
- Useful for functions that only cause side effects
Full Transcript
This visual execution trace shows how a TypeScript function with void return type works. The function greet is declared with void, meaning it does not return any value. When greet() is called, it runs the code inside, printing "Hello!" to the console. The function ends without returning anything, so its return value is void (undefined). Execution continues normally after the function call. Variables track the function state from declaration to end. Common confusions include why no value is returned and what happens if you assign the function call result to a variable. The quizzes test understanding of output, function end step, and return value assignment.