ConceptStep · learning platform for computer science

Functions: decompose the problem

C programming · Lesson

C · lesson 12 / 27

Functions: decompose the problem

Introduce a separate function and observe what happens when main calls it.

1 · Observe and predict

The concept

A call temporarily suspends main, creates a new function frame and returns to the caller with return.

Mental model:

main → somma(x,y) → somma(a,b) frame → return → main

2 · Algorithm view

Observe the equivalent algorithm

For functions the flowchart shows the algorithmic effect. The Call Stack Explorer below shows what really happens between main, parameters, frames and return.

Loading flowchart…
3 · Call Stack Explorer

Follow the call frame by frame

Move through the steps: frames show which variables exist at that moment and the source highlights the involved lines.

1 / 4

main prepares the arguments

x and y belong to main's stack frame.

main
x4 y6
4 · Flowchart ↔ C · Run in Page

Lab 1 — Run the example program

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.

First function Start the flowchart to synchronize the code
                        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}
Ready. Make a prediction before running.

                    
                

Lab 2 — Coding challenge: write your code — Square with a function

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.

5 · Guided practice

Check your prediction

With x = 4 and y = 6, what value does somma return?

Challenge 1 / 5 · Understanding

Progress: 0 / 5 · correct: 0

Your competency map

What you need to know for Functions: decompose the problem

ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.

Current topic Strong skill Review due Needs reinforcement To be mastered Optional support

Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.

GUIDED LEARNING PATHS

Want to choose the next skill to develop?