0

If I have the C++ code:

void valFunc(float val)
{
    val = 50;
}

int main()
{
    int mainVal = 9;
    valFunc(mainVal);
    cout << "mainVal: " << mainVal << endl;
    return 0;
}

valFunc's parameter is of type float, but the argument is of type int. This code compiles and runs correctly. So I'm assuming in C++ doesn't care if parameter type and argument type match?

Another question: When I change int mainVal = 9 to float mainVal = 9.0, the output is still 9 and not 9.0. Why doesn't it output as 9.0?

cigien
  • 57,834
  • 11
  • 73
  • 112
David
  • 619
  • 2
  • 8
  • 15

2 Answers2

0

This may help with the first question about int to float conversion:

Implicit type conversion rules in C++ operators

Community
  • 1
  • 1
mattman
  • 366
  • 1
  • 9
0

C++ does care if a parameter type and the value passed to it are the same type as it's a strongly typed language. However what's going on here is your integer is being implicitly cast to a float which makes it legal. Check out this table of implicit numeric conversions by Microsoft

Edit:

So there's a couple of things wrong with your code. Firstly your valFunc is essentially doing nothing to your mainVal variable, notice how the value is still 9 after you pass it to the function and not assigned the value of 50? That's because it's passed-by-value, which essentially takes a copy of your variable and any modification to it in the function scope does not persist. The remedy to this issue is to pass-by-reference which can be done by simply appending the & operator onto your type as so:

void valFunc(float& val)
{
    val = 50; // Now your mainVal is 50 :)
}

Okay so for your second question:

When I change int mainVal = 9 to float mainVal = 9.0, the output is still 9 and not 9.0. Why doesn't it output as 9.0?

This is nothing to do with your variable not being a floating point value, but more the way the console is printing your value. To get the console to print your value as a floating point you need to include the iomanip header to your project and declare fixed before outputting the value.

Example:

#include <iostream>
#include <iomanip>

using namespace std;

void valFunc(float& val) // Assigning the value will now persist
{
    val = 50;
}

int main()
{
    float mainVal = 9;
    valFunc(mainVal);
    cout << "mainVal: " << fixed << mainVal << endl;
    return 0;
}

This will print the value of 50.000000 to the console (remember this is 50 and not 9 because I added the reference operator to your valFunc parameter and the value now persists as it was passed-by-reference).

There's one more final touch you can add to make your console output a bit nicer - you can declare setprecision after fixed to tell the console how many decimal places to print.

Example:

// Displays mainVal: 50.00
cout << "mainVal: " << fixed << setprecision(2) << mainVal << endl; 
Beege1
  • 16
  • 1
  • Thanks for the reply. Regarding the second question, how come the output is not 9.0? Also, the link you sent me is for C#, does the same apply for C++? – David Jan 29 '16 at 19:14
  • @David Check my edits to my original answer, I hope this helps :). – Beege1 Jan 29 '16 at 20:33
  • @David If my answer solved your issue, please mark is as solved. :) – Beege1 Jan 30 '16 at 16:22