How does the return of functions in C work?

Asked

Viewed 102 times

1

From what I’ve been seeing, every C function needs a comeback, whether I’m right or not?

1- What is the reason for this? It influences something? (I would like a logical explanation if possible).

2- In the case of main(), the type of return will influence something, regardless of being 0, 10, 20, S, AB, 1000...?

Because in a role the return part will influence where you are making your call, right? Then I was left with this doubt in the return of main().

3- Even for type functions void it is necessary to put a return?

4- Just to confirm, in case the return is related to the type that was set in the function, correct? So if I set as int, no use having a return in float because it will cause error, ok?

If you have more information, tips, suggestions to pass on this subject I am available.

  • By definition a function always returns something, which you may or may not use. In C we do not have the concept of procedure but you can simulate defining the return type of the function as void, indicating that no use will be made of the return.

1 answer

2


every function in C needs a return, whether I’m right or not?

1- What is the reason for this? It influences something?

Because almost every function produces a result. In the mathematical concept all result in something, it does not make sense a function result in something, it was created for this. So it’s like this to synthesize the mathematical concept and also because it’s easier to maintain a syntax in a linear way and not have to deal with a different way of doing.

In programming it makes sense to have some routines that do not generate a result, in general this is called Procedure, but he agreed to use the type void of the function to indicate that it is a procedure and not a real function, for the reason explained above. See more on What is the difference between functions and procedures?.

This might help too: Why use only Return, without returning any data?.

2- In the case of main(), the type of return will influence something, regardless of being 0, 10, 20, S, AB, 1000...? Because in a role the return part will influence where you are making your call, right? Then I was left with this doubt in the return of main().

I don’t quite understand what you want to know here. Read this:

3- Even for type functions void it is necessary to put a return?

If you say you have a command return, you don’t have to, you can use it if the exit is conditional or if you want to make it more explicit, but it’s not common to do it just to make it explicit. A finality there is always.

4- Just to confirm, in case the return is related to the type that was set in the function, correct? So if I set as int, no use having a return in float because it will cause error, ok?

Let’s write in a way that makes more sense: the type of result that will be used in a return is the same type that was declared in the function signature. That said, this is it. Of course there are some Casts implicit so some types can be used that they will be interpreted as the declared type, but this is another rule, regardless of being a return.

If you do not define your problem clearly your codes will not be clear. So I suggest you try to understand things in a more structured way and try to be clearer when you put what you are thinking on paper.

Browser other questions tagged

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