Guess it's time to take the blog and get to talk about functions in c + +. A function is a piece of code outside the main, with a name and can be 'called' from another part of our program.
Imagine you are training your dog to be the dead man. Every time you say 'play dead', the dog is pulled back on the floor, close your eyes and stick out your tongue. In programming, the content of the function would be thrown back, close your eyes and stick out his tongue, the name of the functionwould play dead, and every time we play dead we are calling the function.
Our role would be something like:
Haha, well, well. Seriously going to do a classic Hello World but using a function:
You see, this function is very stupid. It benefits us all and makes us write more code than necessary, but it allows you to see how a function works: when calling the function, the compiler jumps to the function, executes it and when it ends, back to the point from where he jumped.
If you could read his mind to a compiler, this would:
Line 1. Include iostream library. OK
Line 2. Use the standard namespace. OK
Line 3. Oh! a function called hello, remember it.
Line 4. Wrench. Part of the function. Disregard
Line 5. Print "Hello World". Part of the function. Disregard
Line 6. Wrench. Part of the function. Disregard
Line 7. Finally the main!
Line 8. Wrench. Enter
Line 9. This function is known to me ... oh yeah I remember. Run lines 4, 5 and 6
Line 10. Wrench. Leave
Now let a function that we do is useful:
First request a name, save it in a variable and send it to the function to display it.
Do not worry, if they had never seen such a function may not understand the syntax, but the goal of this post is to know what is a function and what it can use. In the next post I will explain the syntax of the four types of functions in c++.
0 comments:
Post a Comment