Functions in the C and C ++ programming language
Programs in the c / c ++ programming language can have a large number of commands, and if they were written one after the other in a single file, developers would find it difficult to navigate in such a code, i.e. it would be difficult to maintain it. On the other hand, it often happens that one algorithm, part of the commands that solves a small or large problem inside an application, repeats itself many times. It can be concluded that it would be much better to extract this set of commands (part of the program) as one whole only once as a subroutine or function, and then call this function as necessary whenever necessary. Subprograms are a mechanism for breaking up complex problems into subproblems.
The programming language C knows only one type of routines called functions.
The programming language C knows only one type of routines called functions.
There are two things to distinguish about functions:
- Defining Functions
- Call functions
Defining Functions
Defining means that these commands that make up the function and who need to specify a specific task in an application (program) are written somewhere in the program, to sort them in some way, give a name and determine which data should be delivered to the function as input parameters and what kind of data the function returns back as a return value. So the definition of a function should include:
- Functions of the function
- function header
- body function
There are two words in the header of the method.
The first, in this case int, is a type of return data. This method, which counts a larger number between two integers, returns that integer type. This is the type of return value.
The second one is the name of the method that the programmer chooses himself and who should suggest the purpose of the method.
The body of the method is represented by curly braces. The body contains function commands.
In the header of the function (s), after the name, the parameters (arguments) of the function are written within ordinary brackets. These are the data to be delivered to the function so that it can complete the task set. In the previous example, the task that the function needs to perform is to determine the maximum between the two integers, and the data that the function needs to deliver through the parameter are the two numbers, in this case marked as a and b. The following figure shows more detailed this method.
The first, in this case int, is a type of return data. This method, which counts a larger number between two integers, returns that integer type. This is the type of return value.
The second one is the name of the method that the programmer chooses himself and who should suggest the purpose of the method.
The body of the method is represented by curly braces. The body contains function commands.
In the header of the function (s), after the name, the parameters (arguments) of the function are written within ordinary brackets. These are the data to be delivered to the function so that it can complete the task set. In the previous example, the task that the function needs to perform is to determine the maximum between the two integers, and the data that the function needs to deliver through the parameter are the two numbers, in this case marked as a and b. The following figure shows more detailed this method.
The function parameter can be no one or more. If the function does not require input data then the bracket in the header of the function will remain blank.
If the method does not return the value, it will not have the return statement in the body, but it can only have a return. In this case the word void is written as the type of return value.
The program must have at least one function and this is the main function. An example of the main function of the previous example is shown in the picture below.
If the method does not return the value, it will not have the return statement in the body, but it can only have a return. In this case the word void is written as the type of return value.
The program must have at least one function and this is the main function. An example of the main function of the previous example is shown in the picture below.
Within the main function, two integers are loaded first, and then the maximum is determined. Maximizing commands are isolated as a special method called max (see Figure 3). In the main function, use the cout command to print this value. At the point where the print data is expected, there is a call of the max function. Data as A and B are transmitted as parameters, in fact copies of these values. The max function determines a higher value and returns it back as a return value. This vegetable value will be printed along with the text "The larger number is" at the exit. After starting and setting the values eg 5 and 10 for A and B, the output will display:
Calling an functions in c or c++
The only feature that is automatically called, when starting the application is the main function. The commands in the main are executed from bottom to bottom and when the last command ends the program ends. If we have defined a function in the project, they will not be done by ourselves. In order for a program from the main to continue execution in another function, one of the commands in the main must be the call of another.
In the example in Figure 3, the max function is called:
cout<<"Greather number is "<<max(A,B);
The declaration calls methods, in general, it looks like:
name_of_function(parameter1,parameter2,....);
The parameters that are forwarded to the function are copied in order in the parameters defined in the defined method (see Figure 2). In the definition of a method, a data type is placed in front of the parameter name.
int max(int a, int b)
In fact, a new memory marked with a and b, which receives copies of the parameter values from the function call, is actually reserved:
max(A , B);
The call does not put the data type in front of the parameter.
Copying a parameter is shown in Figure 5.
In the example in Figure 3, the max function is called:
cout<<"Greather number is "<<max(A,B);
The declaration calls methods, in general, it looks like:
name_of_function(parameter1,parameter2,....);
The parameters that are forwarded to the function are copied in order in the parameters defined in the defined method (see Figure 2). In the definition of a method, a data type is placed in front of the parameter name.
int max(int a, int b)
In fact, a new memory marked with a and b, which receives copies of the parameter values from the function call, is actually reserved:
max(A , B);
The call does not put the data type in front of the parameter.
Copying a parameter is shown in Figure 5.
Function for determining the maximum of numbers a and b in c
Task: Create a function that determines the maximum of two integers that are passed as function parameters.
Enter two integers and determine their maximum using the previously defined function.
Enter two integers and determine their maximum using the previously defined function.
Forwarding a parameter by value and by reference
In the previous example, the parameter is forwarded by value. Since the parameters in the second function represent a new memory that obtains copies of the values from the memory of the main function only, any possible change in values within the other function will not affect the data defined in the first one.
This can be illustrated by the following example:
This can be illustrated by the following example:
Example 2: Replace data values
Set the integer value to 20, then make a method that changes this value to 100.
Let's create a function that will change the value of the data sent to:
Let's create a function that will change the value of the data sent to:
When we launch this program at the exit we get
We see that in the main function this value has not changed and if at first glance it seems that the code is all right.
This value is changed within the function change_vr, but this change does not reflect the data in the main function.
In order for this to be correct, the parameters must be transmitted by reference.
This value is changed within the function change_vr, but this change does not reflect the data in the main function.
In order for this to be correct, the parameters must be transmitted by reference.
Transfer function parameters by reference
Unlike transfer by value, when transferring by reference, no new memory is created for the parameters in the second "change_ref" function, but those parameters are actually references (another name) for the same memory occupied by the data parameters in the main function. This will cause changes to parameter values in the second function to be reflected in the first.
Let's modify the previous example by substituting a function that now passes parameters by reference. Parameters are now not actually data but pointers to that data.
For more on pointers, see the Pointers in C/C++ lesson.
The previous example now looks like Figure 9:
Parameters are now not data but data references. The previous example now looks like Figure 9
Let's modify the previous example by substituting a function that now passes parameters by reference. Parameters are now not actually data but pointers to that data.
For more on pointers, see the Pointers in C/C++ lesson.
The previous example now looks like Figure 9:
Parameters are now not data but data references. The previous example now looks like Figure 9
After starting, we get the output:
Transfer function parameters by pointer
Instead of references, pointers to data can also be used. The effect is similar to passing by reference, which means that changes made inside that function to data accessed via pointers will be reflected in the original data defined in the main function. This is because the pointers point to the original data, not to some new one, which just has the same value as the original.
For more on pointers, see the Pointers in C lesson.
The previous example now looks like Figure 10:
For more on pointers, see the Pointers in C lesson.
The previous example now looks like Figure 10:
After starting, we get the output:
Function declaration
Under the declaration of a data or function in programming it is called determining the identifier and describing the properties of the data or function, without allocating a memory space for storing data or function. In the case of a function, the type of function values and the number and types of arguments are determined. In general, the declaration looks like:
label_type function_name (array_argument);
The function declaration is also called the function prototype as well as the signature of the function.
If the function is located in the file above the main function, as in the previous examples then it is not necessary to specify the declaration separately. On the contrary, the declaration should be written above the main function. For example. the prototype of the max function would be:
int max (int a, int b); or only
int max (int, int);
The prototype of change_ref looks like:
void change_ref (int *);
label_type function_name (array_argument);
The function declaration is also called the function prototype as well as the signature of the function.
If the function is located in the file above the main function, as in the previous examples then it is not necessary to specify the declaration separately. On the contrary, the declaration should be written above the main function. For example. the prototype of the max function would be:
int max (int a, int b); or only
int max (int, int);
The prototype of change_ref looks like:
void change_ref (int *);