You are correct in all your initial assumptions.
To return an integer you do not need parameters. The return works separately. You can have parameter but it has no direct relation to the return. The return is a result you pass back to the caller.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int exemplo1() { return 1; }
int exemplo2(int i) { return i * 2; }
int exemplo3() {
srand(time(0));
return rand();
}
int exemplo4(int i) { return i > 0 ? 1 : 0; }
int main(void) {
printf("%i\n", exemplo1());
printf("%i\n", exemplo2(2));
printf("%i\n", exemplo3());
printf("%i\n", exemplo4(2));
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Note that in C you should always declare the data type for everything: variables, vector elements, structure fields, parameters and of course, you can declare the type of functions, which are confused with the function return type. void
means to return nothing. And of course, int
means it will return an entire.
In C there is even how to return values through parameters, but this is advanced, don’t worry about it until you learn other things before. Still no use to you.
This is Java or C?
– Maniero
Exactly, it would be good to specify which of the two languages is using, because there are some differences that should be considered.
– Math
example is in C.
– Jose Maximilian
@Josemaximilian always try to use the right tags that help define what your problem is about to make it easier for people to help you. See the edits made to understand that the questions should be as clean as possible, with nothing that does not matter to the question, so it can help other people in the future.
– Maniero
Sorry, I’ll do it next time. I’m Noob around here.
– Jose Maximilian
@Josemaximilian is normal, the staff takes a little getting used to a site of Questions and Answers. When you get the hang of it, you’ll see it’s for everyone’s benefit.
– Maniero