Functions… Functions are cool. They really are. You know when you have a math or physics exam and you have to do the same procedure in your calculator for each problem again, and again? Isn’t it annoying or tedious? It really is. That happens too in programming. You don’t want your code to end up […]
Hello! This is #Mastery12 about creating functions on c++. Actually, creating a function is pretty simple and easy to learn. First, let´s start defining a function. A function, is basically a group of commands arranged inside by a phrase or word that you choose. There are different kinds of functions. C++ has a list of different functions already made and available to use but you can always create your own too.
How to create a function? Very easy!
The structure of a function consists in…
A type name ( conditions) { and coding that you want the function to proceed with
– A type would be for example: long, double, or integer.
– The name can be almost anything you want, except for the ones already used by c++ to perform another task.
– The values are the things you are declaring.
– and then in between {} you have to type whatever you want the function to do.
Here is an example of a function:
A function is usually written before and outside the main( ). Functions are used to isolate different processes in a program. Here´s an example of a simple program; one that works with a function and one that doesn’t and they both do the same.
There are different types of functions, for example void doesn’t return any value. For functions that do return values there are different types like double, char, bool, string… When using functions that return values, make sure you make the compiler know to what value return like return a; or return x;
To call the function into the main() you can use a cout and type your function.
Here´s an example of a non-void function.
96
Normal
0
21
false
false
false
ES-TRAD
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Tabla normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:Calibri;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-fareast-language:EN-US;}
TIPS:
– Remeber to maintain the same type in the same variables on your function.
– Be careful with the braces and were to put them. Always close the function before typing the main( ).
– Be careful with what values you want your function to return… Example: return a; or return 0;
– Be aware that the function name you create can be used on c++ for another thing like main ( ).
– Remeber that if the program compiles, it does not mean it works so make sure you test your program in the compiler.
WATCH MY VIDEO https://youtu.be/NOLXBXg-yik
If we want to create a function in python we only have to do define the function and the parameters. . Now that we define the function and the parameters now we have to work with it, we put a for, an if, a while or something that we want in the function, for example…
If we want to create a function in python we only have to do define the function and the parameters. . Now that we define the function and the parameters now we have to work with it, we put a for, an if, a while or something that we want in the function, for example…
This post is for mastery points 11 and 12. I created a video explaining how to create a function and then call it later in your code. Believe me it’s really easy. First you should know that a function is a predefined piece of code contained like in a capsule. Each time ou need the […]
This post is for mastery points 11 and 12. I created a video explaining how to create a function and then call it later in your code. Believe me it’s really easy. First you should know that a function is a predefined piece of code contained like in a capsule. Each time ou need the […]