You are looking at lesson 5. Your place is still lesson 1, task 1: Algorithm.
The parts of a function, calling it, void, parameters as copies, prototypes and the scope of variables.
The main of topic 3's program grew to 60 lines: input, checks, a switch, statistics, all in one place. Code like that is hard to read and even harder to fix.
A function is a named piece of code: it gets values, does something with them and returns a result. You have already used printf and read_int. Now you write your own.
int round_to_int(double x) {
return (int)(x + 0.5);
}
| Part | Here | What it means |
|---|---|---|
| return type | int |
the type of the result the function returns |
| name | round_to_int |
how you call it |
| parameters | (double x) |
what the function gets; each with its own type |
| body | { ... } |
what it does |
return |
return (int)(x + 0.5); |
ends the function and returns the result |
A call is the name with the arguments in parentheses:
int p = round_to_int(8.75);
The program jumps into round_to_int, x gets 8.75, return returns 9, and 9 is stored in p.
void print_in_words(int grade) { ... }
int read_grade(void) { ... }
void in place of the return type: the function returns nothing, it only does something (e.g. prints). return; is optional here.(void) in place of the parameters: the function gets nothing. Write (void), not (): in C empty parentheses mean "parameters not stated", and the compiler would not check what you pass.A function gets a copy of the argument. Whatever it does with its parameter doesn't touch the caller's variable:
void double_it(int x) {
x = x * 2;
}
int main(void) {
int a = 5;
double_it(a);
printf("%d\n", a); /* 5 */
}
For a function to change the caller's variable you need pointers (topic 8). For now, return the result with return.
A variable declared inside a function is local: it appears when the function is called and disappears when it ends. Other functions can't see it. So two functions can each have a variable sum, and they are two different variables.
The compiler reads the file from top to bottom. When main calls round_to_int, the compiler must already know that the function exists and which parameters it has. Two ways:
main (you do it this way in this topic);main write only the prototype, the first line with a ;, and the whole function below:int round_to_int(double x); /* the prototype */
int main(void) { ... round_to_int(8.75) ... }
int round_to_int(double x) { ... }
You'll need prototypes in topic 7, when the program splits into several files.
read_grade, print_in_words.main no longer fits on the screen.Names: an action gets a verb (read_, print_), a yes/no question gets is_ (is_valid).
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.