0
0
Cprogramming~10 mins

Function declaration and definition - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function declaration and definition
Start
Function Declaration
Function Definition
Function Call
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 main() {
    int result = add(3, 4);
    return 0;
}

int add(int a, int b) {  // Definition
    return a + b;
}
This code declares and defines a function 'add' that sums two numbers, then calls it in main.
Execution Table
StepActionEvaluationResult
1Declare function 'add'int add(int a, int b);No code run, just tells compiler about 'add'
2Define function 'add'int add(int a, int b) { return a + b; }Function body stored for later use
3Call 'add(3, 4)' in mainadd(3, 4)Function starts with a=3, b=4
4Execute 'return a + b;'3 + 47
5Assign result = 7int result = 7;Variable 'result' now holds 7
6End mainProgram endsProgram finishes successfully
💡 Program ends after main finishes execution
Variable Tracker
VariableStartAfter CallFinal
aundefined3undefined after function ends
bundefined4undefined after function ends
resultundefinedundefined7
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 called before the full definition appears, as shown in step 1 of the execution_table.
What happens when the function is called?
The program jumps to the function definition, runs its code with given arguments (step 3 and 4), then returns the result back to where it was called.
Why does variable 'a' and 'b' become undefined after the function ends?
Because 'a' and 'b' are local to the function 'add', they exist only during the function call and disappear after it returns, as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 5?
Aundefined
B3
C7
D4
💡 Hint
Check the 'Assign result = 7' action in step 5 of execution_table
At which step does the function 'add' actually run its code?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look for 'Execute return a + b;' in execution_table step 4
If we remove the function declaration, what will happen?
ACompiler will give an error when calling 'add'
BFunction will run twice
CProgram will compile and run fine
DResult will be zero
💡 Hint
Declaration in step 1 tells compiler about 'add' before use
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.
Local variables exist only during function execution.
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 existence. Then, the function is defined with its code to add two numbers. When 'add' is called in main with arguments 3 and 4, the program runs the function body, calculates the sum, returns 7, and assigns it to 'result'. Local variables 'a' and 'b' exist only during the function call. The program then finishes execution.