By using functions (functions) We can organize our programs in a more modular form, leveraging the potential of all the structured programming that C + + can provide.
A function is a block of statements that can be executed from elsewhere in the program. Here's how it's formatted:
Type name (argument1, argument2, ...) statement
- Type is the kind of data returned by the function
- Name is the name used when the function is called
- argument is a parameter that a function call needs to pass in (can declare any number of parameters). Each parameter (argument) consists of a data type followed by an identity name, as in a variable declaration (for example, int x). Parameters are valid only within the scope of the function and can be used just like other variables in the function, which allows the function to pass in parameters when called, with different parameters separated by commas (comma).
- Statement is the content of a function. It can be an instruction or a block of statements consisting of a set of instructions. In the case of a set of instructions, the statement block must be enclosed in curly braces {}, which is also the most common to us. In fact, in order to make the format of the program more uniform and clear, it is recommended to use curly braces when there is only one instruction, which is a good programming habit
An example of a function:
#include <stdio.h>
int add (int a, int b)
{
int R;
R=a+b;
return (R);
}
int main ()
{
int z;
z = Add (5,3);
printf ("The result is%d\n", z);
return 0;
}
Function parameters are passed by address:
#include <stdio.h>
void Swap (int& A, int& b)
{
int temp =a;
A = b;
b = temp;
}
int main ()
{
int x = 1, y = 3;
printf ("Begin swap:x=%d,y=%d\n", X, y);
Swap (x, y);
printf ("After swap:x=%d,y=%d\n", X, y);
return 0;
}
Default values for function parameters:
When declaring a function, we can specify a default value for each parameter. If the value of the parameter is not given when the function is called, then this default value will be used. Specifying a parameter's default value only needs to assign a value to the parameter when the function is declared. If no value is passed to the parameter when the function is called, the default value is used. However, if a specified value is passed to the parameter, the default value is replaced by the specified value. For example
#include <stdio.h>
int divide (int a, int b=2) {
int R;
r=a/b;
return (R);
}
int main () {
printf ("divide () =%d\n", Divide (12));
printf ("divide (20,4) =%d\n", Divide (20,4));
return 0;
}
function overloading
Two different functions can use the same name, as long as their parameters (arguments) of the prototype (prototype) is different, that is, you can put the same name to multiple functions, if they use a different number of parameters, or different types of parameters. For example:
#include <stdio.h>
int divide (int a, int b) {
Return (A/b);
}
Float Divide (float A, float b) {
Return (A/b);
}
int main () {
int x=5,y=2;
float m=5.0, n=2.0;
printf ("Divide (x, y) =%d\n", divide (x, y));
printf ("Divide (m, n) =%f\n", Divide (M, n));
return 0;
}
inline function
The inline instruction can be placed before the function declaration, requiring that the function must be compiled in code in the place where it was called. This is equivalent to a macro definition. Its benefits are only valid for short functions, in which case the running code of the compiled result is faster because it avoids some of the normal operation time (overhead) of the calling function, such as the time of the parameter stack operation.
It is declared in the following form:
inline type name ( arguments ... ) { instructions ... }
Its invocation is the same as other function calls. When calling a function, you do not need to write the keyword inline, only the function declaration needs to be written before.
5. Functions