QuantumC

Functions

Back to Home

A function is a block of code you can call over and over again. They don’t add new power to the language (mostly), but they are very useful for a core paradigm of programming: Having one source of truth, also known as having code that is DRY(Don’t Repeat Yourself). The reason having DRY/OST code is that you only have to worry about one thing. Let’s say your refactoring your code(changing it to allow somthing new). It is way easier to change(or not forget to change) let’s say, the way it adds with code like this:

//function that adds

int main() {
    //call add
    //call add
    //call add
}

Than with code like this

int main() {
    //code that adds1
    //code that adds2
    //code that adds3
}

You define a function with the syntax return-type name(optional-arg-list) { code } The argument list is a comma-separated list of type name pairs, and can be empty. So a function that takes 2 integers as argument and returns them added together will look like

int add(int a, int b) {
    return a + b;
}

Couple of clarifications: