Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

tutorial functions

Tianshu Huang edited this page Oct 3, 2018 · 1 revision

Functions

Functions are exactly what they sound like: they take something in, and spit something out. However, in C, as in most programming languages, they can have any number (including 0) inputs, and do some stuff unrelated to the output. Functions have to have only one output, which can be null.

Parts of a function

\\ return type
\\ |  function name
\\ |   |  parameter type
\\ |   |     | parameter name
\\ |   |     |  |      |
\\ v   v     v  v      v
int example(int x, int y) {
    printf("%d\n", x);
    printf("%d\n", y);
    return x + y;
}

In order to specify a function without any inputs, put void inside the parentheses, or simply leave the area inside the parentheses blank. In order to specify a function without any return value, put void as the return type. For example,

void anotherExample() {
    printf("This function does absolutely nothing\n");
    return;
}

In order to call a function, type the function, followed by its parameters in order inside parentheses. The returned value then takes the place of the function in your code.

anotherExample();             // Function with side effects but no return values.
                              // If a function with a return value is called, the
                              // returned value does not necessarily have to be stored.
int z = example(1, 2) + 1;    // example(1, 2) -> 3.
                              // This line then becomes int z = 3 + 1.
Clone this wiki locally