user2085224
user2085224

Reputation: 49

function with no return type

I just have 2 quick questions, I'm hoping someone can clarify for me.

  1. When writing a function is the input paramater list the same as the parameters?
  2. when a function has no return type is the "return 0;" just left out of the function code?

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

Answers (5)

stackunderflow
stackunderflow

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

Rabbiya Shahid
Rabbiya Shahid

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

Mats Petersson
Mats Petersson

Reputation: 129344

  1. The input parameter list is a list of type and names of the inputs. These are passes as a list of values in the calling code.
  2. If you have no return value, then the function is declared with a void return type, and should not have a value given in the return statement.

Upvotes: 0

taocp
taocp

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

eidsonator
eidsonator

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

Related Questions