First, that line inside the main
:
float CalculaHora(horas, minutos, segundos, &conversao);
This is an attempt to function statement, shouldn’t be there. On this line you should be calling for the function.
An outline of what it would be (in the end we will see the complete code):
// função CalculaHora (declaração da função)
float CalculaHora(float horas, float minutos, float segundos, float *pConversao) {
// código que calcula...
}
int main() {
float horas, minutos, segundos, conversao;
// printf, scanf...
// CHAMAR a função
CalculaHora(horas, minutos, segundos, &conversao);
}
Notice that to call the function I don’t need to put float
in front. The float CalculaHora
is used in function declaration to say "this is function CalculaHora
, that returns a float
". When it comes to calling, you just... call, without putting the guy in front.
Now the question of *
versus &
.
In the function statement you use float *pConversao
, for pConversao
is a pointer to float
(the asterisk indicates this, which is a pointer). This means that the function waits for a pointer there: when you call the function, you have to pass a pointer.
Now on the main
, the variable conversao
is not a pointer: she is a float
. But to call the function CalculaHora
, you need to pass a pointer to float
. And how do you pass a pointer to the variable float
? Putting the &
before, indicating that you are passing her address (i.e., a pointer to float
, which is precisely what the function CalculaHora
waiting).
That being said, it makes no sense to change the function at the same time the value of the pointer and return it. If it already returns, it would not need to receive the pointer.
And from what I understand, the calculation is wrong. If you get, for example, 1 hour, 10 minutes and 30 seconds, the amount of seconds would not be 4230?
Anyway, returning: if the function receives a pointer and modifies its value within it, there is no reason to return that same value, then it could be like this:
// void, pois não precisa retornar *pConversao (já que o valor é modificado, então o return é redundante)
void CalculaHora(float horas, float minutos, float segundos, float *pConversao) {
*pConversao = horas * 3600 + minutos * 60 + segundos;
}
int main() {
float horas, minutos, segundos, conversao;
printf("Digite a hora, minutos e segundos: ");
scanf("%f %f %f", &horas, &minutos, &segundos);
CalculaHora(horas, minutos, segundos, &conversao);
printf("A quantidade de segundos eh %f!\n", conversao);
return 0;
}
But if you want the function to return the value, then you wouldn’t actually need a pointer:
// retorna o valor, então não precisa receber o ponteiro
float CalculaHora(float horas, float minutos, float segundos) {
return horas * 3600 + minutos * 60 + segundos;
}
int main() {
float horas, minutos, segundos, conversao;
printf("Digite a hora, minutos e segundos: ");
scanf("%f %f %f", &horas, &minutos, &segundos);
// variável recebe o valor retornado pela função
conversao = CalculaHora(horas, minutos, segundos);
printf("A quantidade de segundos eh %f!\n", conversao);
return 0;
}
Again I will draw your attention to: a function must be defined either before main or after main, provided that its signature (or protop) is declared before main. Do not sign within of main.
– anonimo