functions

So far, we have written only one function for our programs. That function is main. We start our program with main and all our code goes between the curly brackets for main.

However, most programs are many many times larger than the programs we wrote. For example, our programs have been typically 20 to 30 lines of code max. Most applications are over a million lines of code. Trying to code all of it in main would make it impossible to understand or fix.

In programming we often look at modularity. That is we break down a problem into smaller pieces. How a program is broken down depends on the language. In C, a program is broken down into functions.

Here are two principles of software engineering that explain why we want to use functions

  • DRY (Don't Repeat Yourself) principle
  • KIS (Keep it Simple) principle

DRY (Don't Repeat Yourself)

When you write a large program, it is inevitable that you will have errors or need to modify a piece of logic. If you separate your code into functions that operate independently of each other,

results matching ""

    No results matching ""