This commit is contained in:
Miloslav Ciz 2024-11-22 17:05:15 +01:00
parent 481775ca4b
commit 5e0ba22bba
10 changed files with 1877 additions and 1871 deletions

View file

@ -459,7 +459,7 @@ The output is:
16
```
The function power takes two parameters: `x` and `n`, and returns `x` raised to the `n`s power. Note that unlike the first function we saw here the return type is `int` because this function does return a value. **Notice the command `return`** -- it is a special command that causes the function to terminate and return a specific value. In functions that return a value (their return type is not `void`) there has to be a `return` command. In function that return nothing there may or may not be one, and if there is, it has no value after it (`return;`);
The function power takes two parameters: `x` and `n`, and returns `x` raised to the `n`s power. Note that unlike the first function we saw here the return type is `int` because this function does return a value. **Notice the command `return`** -- it is a special command that causes the function to terminate and return a specific value. In functions that return a value (their return type is not `void`) there has to be a `return` command. In a function that returns nothing there may or may not be one, and if there is, it has no value after it (`return;`);
Let's focus on how we invoke the function -- in programming we say we **call the function**. The function call in our code is `power(2,i)`. If a function returns a value (return type is not `void`), its function call can be used in any expression, i.e. almost anywhere where we can use a variable or a numerical value -- just imagine the function computes a return value and this value is **substituted to the place where we call the function**. For example we can imagine the expression `power(3,1) + power(3,0)` as simply `3 + 1`.
@ -625,17 +625,19 @@ int main(void)
In C programs you may encounter a **switch** statement -- it is a control structure similar to a branch `if` which can have more than two branches. It looks like this:
```
switch (x)
{
case 0: puts("X is zero. Don't divide by it."); break;
case 69: puts("X is 69, haha."); break;
case 42: puts("X is 42, the answer to everything."); break;
default: printf("I don't know anything about X."); break;
}
switch (x)
{
case 0: puts("X is zero. Don't divide by it."); break;
case 69: puts("X is 69, haha."); break;
case 42: puts("X is 42, the answer to everything."); break;
default: printf("I don't know anything about X."); break;
}
```
Switch can only compare exact values, it can't e.g. check if a value is greater than something. Each branch starts with the keyword `case`, then the match value follows, then there is a colon (`:`) and the branch commands follow. IMPORTANT: there has to be the `break;` statement at the end of each case branch (we won't go into details). A special branch is the one starting with the word `default` that is executed if no case label was matched.
NOTE: Why does switch statement exist and why is it so weird? It may seem a little arbitrary and weird, especially when with several if statements we can achieve the same things and can use more general conditions. Switch statement isn't just syntactic sugar, it normally gets translated to different instructions and using switch for checking many conditions at once will be typically much faster than using if statements. Basically switch internally creates a table that will allow the program to jump instantly to the correct label, avoiding checking conditions one by one.
Let's also mention some additional data types we can use in programs:
- `char`: A single text character such as *'a'*, *'G'* or *'_'*. We can assign characters as `char c = 'a';` (single characters are enclosed in apostrophes similarly to how text strings are inside quotes). We can read a character as `c = getchar();` and print it as `putchar(c);`. Special characters that can be used are `\n` (newline) or `\t` (tab). Characters are in fact small numbers (usually with 256 possible values) and can be used basically anywhere a number can be used (for example we can compare characters, e.g. `if (c < 'b') ...`). Later we'll see characters are basic building blocks of text strings.