main prepares the arguments
x and y belong to main's stack frame.
x4
y6
C programming · Lesson
Introduce a separate function and observe what happens when main calls it.
A call temporarily suspends main, creates a new function frame and returns to the caller with return.
main → somma(x,y) → somma(a,b) frame → return → main
For functions the flowchart shows the algorithmic effect. The Call Stack Explorer below shows what really happens between main, parameters, frames and return.
Move through the steps: frames show which variables exist at that moment and the source highlights the involved lines.
x and y belong to main's stack frame.
x4
y6
The call creates a new frame: a and b receive copies of the passed values.
a4
b6
x4
y6
return a + b produces 10 and ends somma's frame.
a4
b6
x4
y6
The returned value is assigned to risultato.
x4
y6
risultato10
Run C executes only the example shown on the left, using stdin on the right. The coding challenge below has a separate editor and test button.
1#include <stdio.h>
2
3int somma(int a, int b)
4{
5 return a + b;
6}
7
8int main(void)
9{
10 int x;
11 int y;
12 scanf("%d", &x);
13 scanf("%d", &y);
14 int risultato = somma(x, y);
15 printf("%d\n", risultato);
16 return 0;
17}
Write quadrato(int x), read an integer and print its square.
This is a different program from lab 1. Complete the starter code: Check all test cases compiles this editor and tests every input, including boundary cases.
With x = 4 and y = 6, what value does somma return?
Challenge 1 / 5 · Understanding
Progress: 0 / 5 · correct: 0
ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.
Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.