Reputation: 49
I just have 2 quick questions, I'm hoping someone can clarify for me.
For example if I was to write a function that used to integers in its input parameter list with no return type would this be the proper way to write it?:
int convertTemp( int a, int b) {}
Upvotes: 1
Views: 8369
Reputation: 993
Use the void
return type to declare a function will return nothing:
void convertTemp( int a, int b) {
}
Now, how would the function convert a Temp when it returns nothing?
It should definitely not modify some state behind the scenes (like global variables).
Returning the converted value is what I would expect.
The void return type would have to be changed to the type of the conversion result.
I would definitely investigate when faced with code like the above.
Upvotes: 0
Reputation: 422
I agree with the answer above that the return type should be changed to void, And when asking the first question you need to distinguish between formal and actual parameters. Formal parameters are a and b in the function definition:
void func (int a, int b) {}
whereas the actual parameters are 2 and 3 in the function call:
func (2,3)
where 2 is copied to a and 3 is copied to b.
Upvotes: 1
Reputation: 129344
void
return type, and should not have a value given in the return
statement. Upvotes: 0
Reputation: 23634
int convertTemp( int a, int b) {}
should be:
void convertTemp( int a, int b) {}
if function does not return anything, int
means the function's return type is int
. Note that you can still have return
statements in a function that with return type void
.
Upvotes: 2
Reputation: 1325
You want to make the return type void
void convertTemp( int a, int b) {
//do stuff...
return; //optional, but a good habit to get in to
}
Upvotes: 0