C-SHARP - 4.10 Procedural programming

Procedural programming is a paradigm that splits a problem into small reusable pieces called functions (also named procedures or methods). Until now we wrote every instruction inside the Main method, which works for tiny scripts but quickly becomes unmaintainable. Real applications include many features; we must split the code into methods, each responsible for a single task. This is procedural programming, and it pairs well with the loops and conditions we already know.

Take a concrete example: compute the average of a class. With a hard-coded approach we declare an array of grades, loop over it to sum the values, divide by the length and display the result. If we now need to do the same for 28 students, copy-pasting the block 28 times is unthinkable. We extract the logic into a reusable method CalculerMoyenne that takes an int[] array as a parameter and returns the average as an int.

static int CalculerMoyenne(int[] notes)
{
    int moyenne = 0;
    for (int i = 0; i < notes.Length; i++)
    {
        moyenne = moyenne + notes[i];
    }
    moyenne = moyenne / notes.Length;
    return moyenne;
}

static void Main()
{
    int[] eleve1 = { 10, 12, 20, 6 };
    int[] eleve2 = { 14, 8, 16, 10 };
    int moyenne1 = CalculerMoyenne(eleve1);
    int moyenne2 = CalculerMoyenne(eleve2);
    Console.WriteLine("Moyenne 1 : " + moyenne1);
    Console.WriteLine("Moyenne 2 : " + moyenne2);
}

Global vs local variables

  • Variables declared inside a method are local: they only exist inside that method.
  • A global variable declared at class level is shared across methods but creates dependencies and bugs.
  • Prefer methods that return a result and avoid altering the caller's state.

A method that returns a value declares a return type (here int) and uses return. A method that returns nothing uses void. Avoid global variables: it is far better to have independent functions that take parameters and return results — your code will be easier to test, reuse and reason about. We will keep practising this style throughout the course.

Summary

Procedural programming is a programming paradigm that breaks programs into smaller, reusable procedures or functions. Rather than writing all code in a main method, this approach decomposes problems into smaller parts, with each function handling a specific task. The lesson demonstrates this concept through a C# example of calculating student grade averages, showing how functions eliminate code duplication and make programs scalable for real-world applications serving hundreds or thousands of users.

Key points

  • Procedural programming divides programs into procedures, functions, or methods (all equivalent terms) that encapsulate specific logic
  • Problems are decomposed into small, manageable parts, with dedicated functions handling each component
  • Functions enable code reusability—write logic once and call it with different inputs instead of duplicating code
  • Practical example: using arrays and loops in C# to calculate grade averages, then refactoring into reusable functions
  • Procedural programming forms the essential foundation before advancing to object-oriented programming (OOP)
  • Functions prevent maintainability nightmares—avoiding the need to rewrite the same code thousands of times in large applications

FAQ

What is the main benefit of procedural programming?

Procedural programming eliminates code duplication by creating reusable functions. Instead of rewriting logic for each use case (e.g., calculating averages for thousands of students), you write the function once and call it with different inputs, saving time and reducing maintenance burden.

How does procedural programming differ from object-oriented programming?

Procedural programming breaks programs into functions based on procedures to execute step-by-step. Object-oriented programming, by contrast, organizes code around objects. Procedural programming is typically learned first as a foundational concept before advancing to OOP.

Are procedures, methods, and functions different concepts?

No—procedures, methods, and functions are interchangeable terms that all describe the same thing: subprograms designed to perform specific, often repetitive operations. The terms are used based on context and programming language convention, but they refer to identical concepts.