How to decide the type of a function?

Asked

Viewed 88 times

2

In many C written codes on which I see on the internet I only see type functions void and int Is it possible to declare functions of other types? For example struct and float.

I did a simple test with the guys char and float and it worked, but I still don’t understand the real importance of function type when I should use which?

#include <stdio.h>
#include <stdlib.h>

float rf(){

    float testfloat = 1.3;
    char string['C'];
    printf("'%f' '%s'", testfloat, string);

}

char rc(){

    float testfloat = 1.3;
    char string['C'];
    printf("'%f' '%s'", testfloat, string);

}

int main(){

 rf();
 rc();
 return 0;

}
  • 2

    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.

  • the functions return what is contained in them @Joãosobral

  • 1

    @Assanges the only function of your example that returns something (in this case 0) is the main. The rest is not returning anything.

1 answer

5


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.

  • in case I imply a function that returns a struct I could create a kind of cool class like +1 by the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.