The types of functions in c + + are 4, but they are actually combinations of the 2 things that a function can do. If you are lost in features I recommend reading my previous post: Functions in C + + .
A function, as I said, you can do (or not) two things: 1 - Receive data and 2 - Return data. From this came the four types of functions:
- Not receive or return
- Receive and do not return
- They receive and return
- Receive and return
Let's make a program to add two numbers using the four types of functions:
Not receive or return
The simplest. To use them just need to know how to create and how to call them. A function is created in this general form:
The 'type' refers to the data type (int, float, void, char) and the functions that always return void.
tipo nombre(){}
The 'type' refers to the data type (int, float, void, char) and the functions that always return void.
The 'name' is the name of the function: anyone who starts with a letter, which is significant and is not a reserved word.
To call them simply type the function name followed by its parentheses and a semicolon (;).
nombre();
So our program is:
You see, everything you would put in our main better put in a function and call it from main. A function always, always, always have to go before the main.
One function of this type we have used many times is getch ();
Receive and not return
How would you ask for two numbers in the main function and do the sum? For that we need to do a function capable of receiving data, then the syntax changes a bit:
'Type' and 'name' refer to the same thing as not always return type void.
tipo nombre(tipo_var1 nombre_var1, tipo_var2 nombre_var2){}
'Type' and 'name' refer to the same thing as not always return type void.
Within the brackets we have other issues:
'Tipo_var1' refers to the type of the variable that our role is to receive.
'Nombre_var1' refers to the name of that variable.
If we receive a variable until there is enough, if we want another variable a comma (,) and declare the following variable.
To call this function, we put the variables inside the parentheses to send in the order in which we declared in the function:
nombre(var1, var2);
Our program would look like:
We call the two numbers in the main, send them to the feature, it adds them and displays them.
One function of this type we have used many times is hated by many, loved by others, gotoxy (x, y);
Return and do not receive
What if now we want the opposite? Order numbers in the function, but show in the main. For that we need a function that returns.
Receive data is sent to the function main. Return is sending data to the main function. To return data to do two things: do not use void as return type and use.
In general:
The 'type' must be the type of variable you want to return, if our variable returns an int variable, because the type of the function is int.
tipo nombre() { return var; }
The 'type' must be the type of variable you want to return, if our variable returns an int variable, because the type of the function is int.
To indicate which variable we are returning will use the word return followed by the variable. Usually this comes at the end of the function.
To call the function, prepare a bed in which the variable falls is returning.
The variable is returning our role is to be stored in the variable 'var'. This is a good time to remind you that the variables declared between two braces {} only between those two keys. So the variable 'var' function is not the same as the variable 'var' function, however the main var're getting the value of the main var. A bit confusing I know, do not worry.
var = nombre();
The variable is returning our role is to be stored in the variable 'var'. This is a good time to remind you that the variables declared between two braces {} only between those two keys. So the variable 'var' function is not the same as the variable 'var' function, however the main var're getting the value of the main var. A bit confusing I know, do not worry.
Our program would look like:
Can anyone think of a known function of this kind?
Receiving and Returning
Now we want our function only add, the main will be in charge of asking for numbers and add the results. For that we need that our function receives the variable, and returns the result. Wow! Is it that possible? Sure.
It's only a matter of combining the functions that are not returned with the returning and do not receive.
Our program would look like:
The math.h library functions are mostly of this type. sqrt (), pow (), sin ();
At first it may seem that the functions they serve only to organize the code, which is true, but not all.Can you imagine if we had to write all the code behind a simple gotoxy (); Oh really ...
Well, I would stay quiet if they are not showing the optimized version of the last function:
0 comments:
Post a Comment