I'm learning how to use the functions in C but I can't understand why while this:
float TableauDebut(float size[], float poi[], char sex[], int nbElem)
{
int i;
for(i=0; i < nbElem; i++){
printf(" %d)%.2f %.2f ", i, size[i], poi[i]);
switch(sex[i]){
case 'F': printf("Woman\n"); break;
case 'M': printf("Man\n"); break;
}
}
displays my table as well as using void
But when I try to do the same in Java:
static void TableauDebut(double [] size, double [] poi, char [] sex, int nbElem)
{
int i;
for(i=0; i < nbElem; i++){
System.out.printf(" %d)%.2f %.2f ", i, size[i], poi[i]);
switch(sex[i]){
case 'F': System.out.printf("Femme\n"); break;
case 'M': System.out.printf("Homme\n"); break;
}
}
I can just use void and not double
What's the difference between using float and void in C language and why I cannot do the same in Java?
A void
return type means your function doesn't return a value. Anything else means your function must return an object of that type (or, where applicable, something that is convertible to that type.)
Your first example promises to return a float
but fails to do so. This is an error, but a C compiler need not tell you about this. With suitable compiler settings you should get a warning, but the bottom line is that it is possible to compile such an erroneous piece of code in C, run it, and get undefined behaviour at runtime.
Java protects you against this and fails to compile the code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.