Actually the function has no type, who has type is the value it returns. This yes is important.
The return, as well as the parameters, variables, constants, etc. can and should have any type available in the code, can be one already defined in the language, in the standard libraries, in other libraries you are using, or types defined by your code (with typedef
and probably struct
).
Obviously the function should return something with the command return
and the returned value must be compatible with the return type stated on it.
If to return anything, then the type of return should be void
. So the function cannot be used in expressions that always expect some value. void
is no value, is "less" than null.
You should use whatever is most suitable to solve the problem. No generic response can determine which is most appropriate for the specific situation.
Of course, some things you know. A guy has to hold all the valid values he knows that one element of the code can have. For example, if you declare a int
, probably (depends on architecture), the highest possible value is 2.147.483.647.
In some rare cases you may need an unmarked type (which only accepts positives), it is rare and does not come to the case here because you should not opt for them as default.
If you need a monetary value the novice programmer goes hot on float
or double
(which allow decimal part values), but you can’t use them for that.
Anyway, are just examples, can not put all basic rules under penalty of the question get too wide.
The slightly improved example according to my speculation:
#include <stdio.h>
float rf() {
float testfloat = 1.3;
char string['C'];
printf("'%f' '%s'", testfloat, string);
return testfloat;
}
char rc() {
float testfloat = 1.3;
char string[]; //havia um erro aqui e isso ainda não vai dar certo
printf("'%f' '%s'", testfloat, string);
return 'a';
}
void rv() {
printf("faz algo aqui");
return;
}
int main() {
float x = rf();
char c = rc();
rv();
}
I put in the Github for future reference.
You can declare functions that return any type of data. Only that I think you do not understand very well what is return in the function, since its function does not return anything.
– João Sobral
the functions return what is contained in them @Joãosobral
– user45474
@Assanges the only function of your example that returns something (in this case
0
) is the main. The rest is not returning anything.– Bacco