Void Functions

Hi guys, I just want to know that when using a void function is it okay to use the type of the parameter's value as double or should it always be a int?
1
2
//can i use like this?
  void insert(double x);

Thanks.
you can use it as any data type (double, int, float, string, etc.)
The function type means that the function can only return that type. A void function does not return anything. The parameters can be absolutely any type you want. But if you give a function a type other than void you have to make a return (or the compiler will give a warning, it's undefined behavior) and it has to return that type.

void type -- This function will print x+y to the console, but the function returns nothing when called.
1
2
3
4
5
void foo(int x, int y)
{
    std::cout << (x + y) << std::endl;
    return; //optional, but sometimes needed to exit a function early after a comparison.
}


int type -- This function will return an integer because sum is an integer. x and y are added, the total is converted to an int. Precision is lost because the decimal portion is truncated.
1
2
3
4
5
6
int bar(double x, double y)
{
    int sum;
    sum = x + y;
    return sum;
}


string type -- firstName and lastName are concatenated with " " and returned as fullName, which is a string.
1
2
3
4
5
6
fooBar(std::string firstName, std::string lastName) 
{
    std::string fullName;
    fullName = firstName + " " + lastName;
    return fullName;
}

Thanks for the tips. (Y)
Topic archived. No new replies allowed.