0
0
C++programming~10 mins

Function declaration and definition in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function declaration and definition
Start
Function Declaration
Function Definition
Call Function
Execute Function Body
Return Result
Continue Main Program
End
The program starts by declaring a function, then defining it. When the function is called, its body runs and returns a result, then the main program continues.
Execution Sample
C++
int add(int a, int b);  // Declaration

int add(int a, int b) {  // Definition
    return a + b;
}

int main() {
    int result = add(3, 4);
    return 0;
}
This code declares a function 'add', defines it to return sum of two numbers, then calls it in main.
Execution Table
StepActionEvaluationResult
1Declare function 'add'int add(int a, int b);Function signature known to compiler
2Define function 'add'int add(int a, int b) { return a + b; }Function body ready
3Call add(3, 4) in mainadd(3, 4)Function call initiated
4Execute add bodyreturn 3 + 4;7
5Assign resultint result = 7;Variable 'result' = 7
6End mainreturn 0;Program finished
💡 Program ends after main finishes execution
Variable Tracker
VariableStartAfter Step 5Final
aN/A33
bN/A44
resultUndefined77
Key Moments - 3 Insights
Why do we declare the function before defining it?
Declaration tells the compiler about the function's name and parameters so it can be used before its full code is seen, as shown in Step 1 of the execution_table.
What happens when the function is called?
The program jumps to the function's code, runs it with given inputs, and returns a value, as seen in Steps 3 and 4.
Why do variables 'a' and 'b' have values only during function execution?
Because 'a' and 'b' are parameters local to the function, they get values only when the function runs, shown in variable_tracker after Step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of add(3, 4) at Step 4?
A3
B7
C4
DUndefined
💡 Hint
Check Step 4 in execution_table where the function returns the sum.
At which step does the program know about the function signature?
AStep 3
BStep 2
CStep 1
DStep 5
💡 Hint
Look at Step 1 where the function is declared.
If we remove the function declaration, what will happen when calling add(3, 4)?
ACompilation error
BFunction returns 0
CProgram runs normally
DRuntime error
💡 Hint
Without declaration, compiler doesn't know the function signature before call (see Step 1 importance).
Concept Snapshot
Function declaration tells the compiler about a function's name and parameters.
Function definition provides the actual code.
Call the function by its name with arguments.
Function runs, returns a value, then program continues.
Declaration is needed if function is called before definition.
Full Transcript
This visual trace shows how a function is declared and defined in C++. First, the function 'add' is declared to inform the compiler about its name and parameters. Then, the function is defined with its body that returns the sum of two integers. In the main function, 'add' is called with arguments 3 and 4. The program jumps to the function, executes the return statement, and returns 7. This value is assigned to the variable 'result'. Finally, the program ends. Variables 'a' and 'b' are local to the function and get their values only during the function call. The declaration is important so the compiler knows about the function before it is used.