Well basically from what I understand you want to know how to pass a variable per parameter in C, it is important to know that in C there are two ways to pass variables per parameter in a function, there is the passage by value, and the passage by reference.
But before explaining the difference between these two passages it is important to give a brief explanation of function.
Functions must be declared before main, and to declare them is very similar to a variable just do the following: choose the return type, function name and parameter.
the return type is as the name already says, is the type of data that your function will return, the name of the function, is the name that you will use to call the function, and the parameters, are the data that you will have to pass to your function, for it to work with.
To declare the parameters just keep in mind the following things, what kind of data I will have to use for my function work, what will be the names and how many variables will be needed, eg:
int calculaMultiplicacao ( int a, int b )
{
return a * b;
}
and
void calculaMultiplicacao ( int a, int b, int* R )
{
*R = a * b;
}
These are two examples of functions, both do the same thing, but have been implemented differently, in the first case note that the return of the function is int
, this is because the value that the function will return is an integer, so we have the name, and then the parameters, note that in the first case two variables are requested per parameter, both are of the whole type, and realize that without these two variables the function would not work, for it is they who are necessary for the function to do what it has to do. So always when doing a parameter think about it, what data will be necessary that I have to pass to this function for it to work.
Already in the second example we have the following, the return is of the type void
, this because there is no return, and three variables are asked in parameter two to work with and a third to receive the result, so the return is unnecessary because the result will be passed by reference and not by return, now you will be ready to understand passage by reference and passage by value.
Pass by value, as the name already says you pass a value to a function this can be given directly in the parameter or through a variable, eg:
Let’s assume that we have a function that multiplies any two integers, then the passage by value in the main function would be the following.
#include <stdio.h>
int calculaMultiplicacao ( int a, int b )
{
return a * b;
}
int main ()
{
int a, b;
scanf(" %i %i", &a, &b);
printf("\nO resultado eh: %i\n", calculaMultiplicacao(a, b)); /* Note que eu estou pasando os dois
valores que estão em a e b */
return 0;
}
Note that when we do this, we are passing only the values that are in the variables a
and b
, an interesting test for you to better understand what I mean by this is the following, within the function calculaMultiplicacao
try changing the value of a
and b
, and then print a
and b
in main, you will notice that the value that will appear will be the value that was assigned in the scanf
, and not what was modified in the function, that is why the in this case of passage, what is passed is the value and not the "variable".
Explaining in a better way, what your program does with this is the following, the moment you call the function and pass the variable a
and b
, the program creates two other variables a1
and b1
(note that I put this name only to not confuse, because the names of the variables would be the same that was declared in the function parameters) and equals their values with those of the variables passed , a1 = a
, b1 = b
, but one variable has nothing to do with the other, so if you change the value of a
and b
in function calculaMultiplicacao
, this will not change the true value of a
and b
who are in main.
Passing by value also allows us to make another approach, since what matters is only the value, and not the variable we need not necessarily pass variables in the function parameter can do the following:
#include <stdio.h>
int calculaMultiplicacao ( int a, int b )
{
return a * b;
}
int main ()
{
printf("\nO resultado eh: %i\n", calculaMultiplicacao(5, 6)); /* Note que eu estou pasando os dois
valores diretamente. */
return 0;
}
Note that I am passing two values directly, this is possible, because as already explained previously the function will create two variables a
and b
, and will match their value to that of the past a = 5, b = 6,
(in this case it is clear).
The second way is the passage by reference, this is through the passage of a certain region of memory, which we will use to change and or save a value, based on this it is important to know the following a pointer, is a variable that the tip to a region of memory, then the moment you declare your function, the variable to be passed by reference will need to be a pointer, e.g.:
#include <stdio.h>
void calculaMultiplicacao ( int a, int b, int* R )
{
*R = a * b;
}
int main ()
{
int a, b, R;
scanf(" %i %i", &a, &b);
calculaMultiplicacao(a, b, &R); /* Note que pelo fato de R ser uma variável normal, no momento que
eu a passo por parâmetro, eu coloco o & pois estou passando o enderço
de memória de R */
printf("\nO resultado eh: %i\n", R);
return 0;
}
Note that the R
is passed with the &
, this is due to the fact that R
is a normal variable, and as we need to pass a region of memory to be changed, so we put the &
, because we pass the address of the variable R
, in this case we cannot pass a direct value (not in place of R
, but in that of a
and b
can still), as it needs to be a memory region.
Another important thing to remember is that case R
was a pointer in the main, would not need to pass it in the parameter with the &
, as a pointer already directs to a memory region.
Here is a gif link that can help you understand the difference between passing by value and passing by reference click here and go to gif, well any doubt just ask, I hope I’ve helped ;).
Do not put the code as an image. Instead add it to the question as text and format with the button
{}
– Isac
That’s not the real code, it’s just an example of how it is
– Braian Freitas
Read here why you should not put code as image. Regarding the question, your question is how to define the function to receive a pointer ? or what specifically?
– Isac
Ah, I get it, I didn’t know this about the picture, sorry.
– Braian Freitas
I need to pass a value from main to a function, this function does the calculation, and then in main I call a function just to print the result of that calculation
– Braian Freitas
Put the code as text so that it is easy for anyone to answer with the code of your question. Regardless you are already using a function that receives a pointer, a
valor_r_ex(float *n3)
. What is the real difficulty, since it would be the same principle?– Isac