Tip:
Highlight text to annotate it
X
Hello. This is my fourth tutorial on C++ programming and today we'll write functions as I promised
in the first tutorial and I try to explain it more precisely
Start by include iostream and before I forget using namespacce std and
make the main funcion, This is a very special function as I said
in the first tutorial, and the code block after it. Just make an
ordinary function. An integer return type add name and it requires
two integer parameter an a and a b. And it will return the sum of them, so
a+b just as simple. And in the main function just call it.
add(5 comma 4) so the parameters you have to separate with comma.
And it's a good habit, return with 0. Altought it will not do anything, so we should
store it in a variable for example. int i=add(5,4) and now cout the i variable
and make a new line after it. Save it and get a terminal from somewhere
and compile it. OK g++ first.cpp the output will be fourth
OK, no error, great. And run it.
As you can see as we expected it writes out 9.
Altought we could do it without variable i, by just cout that.
This work as well. Define another function. Now we will use a
data type that I didn't mantion because it's not really a data type
It's called void. It's mean there is no return type. void write() with no parameters like
the main function and we just cout a few thing. Like hello world,
ok.
Every time we call this write function anything will be executen inside the code block.
OK write it out. Like that. OK get a terminal, compile it and run it.
OK as you can see it's work well. So a function can be a huge help, so you don't have to write
this hundred times. Of course you can do much more with functions.
OK, and that's for now, later we will continue this.