Stuck in Global Variables

I'm doing some of my own examples to practice c++. So I have a doubt why it is using "void" in void conv(); in 4th row. If someone able please explain me why and what are the other method we can use. This is a simple Celsius-Fahrenheit Converter.

ex:-

#include <iostream>
using namespace std;

void conv();

float ft;
float cs;

int main()
{
cout << "Input Fahrenheit : ";
cin >> ft;

conv();

cout << "Celsius : " << cs << endl;

system ("PAUSE");
return 0;
}

void conv()
{
cs = ((ft-32)/1.8);
}
You're using void because the function isn't returning a value.

It won't make a lot of sense right now, but because you're using global variables, the cs getting assigned to in the function is the same cs in main so your function doesn't need to return that value back to main.

Global variables are generally considered a bit dangerous and are mostly best avoided.

An alternative would be to use local variables. Create the floats in main, pass the ft variable into conv() and have conv() return the cs value back to main.
I would like to append the message of iHutch105.

You could do not use global variables. In this case it would be better to define the function as having a parameter and returning the result of the calculation. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;
 
float conv( float );
 
int main()
{
   float ft;

   cout << "Input Fahrenheit : ";
   cin >> ft;

   cout << "Celsius : " << conv( ft ) << endl;

   system ("PAUSE");
 
   return 0;
}

float conv( float ft )
{
   return ( ( ft - 32 ) / 1.8 );
} 
Last edited on
Thank you for the replies. And I'm having few of questions. If you help it would be easy to learn c++ for me. Questions below,

I have seen these parameters in few of my sample programs (in my book).
Can you explain me how to use a parameter and what its purpose.
What is the advantage of a parameter?
And what is the advantage using a local variable than global variable in these kind of basic programs?
A parameter is used for passing a value into a function. All functions have scope, which is basically what's in between the two curly braces of the function body. Any variables declare within a scope exist only in that scope.

For example:
1
2
3
4
5
6
7
8
9
void A()
{
   int x;
}

void B()
{
   int x;
}

In that example, x is a different variable in each of those functions. If you want to pass a variable or value into a function, you'd create a parameter for it.
1
2
3
4
void A(int y)
{
   int x = y;
}

In the above example, A now expects an integer to be passed into it, which it will refer to as 'y' in the function.

You'll probably hear a couple of different terms; parameters and arguments. They're almost interchangeable. Personally, I see parameters as the list of variables and types and function is expecting and arguments as the actual variables that are being passed in.

There isn't so much of a problem with using global variables in these basic examples, but it's a good habit to get out of them for when you create bigger programs. If you have a global variable it can be modified in any of the functions in your source file. There could be hundreds of functions. It would be pretty hellish to debug if that variable wasn't doing what you expected it to.
Seeing the answer's been provided to the question, I'll just throw this in.
Avoid global variables. Encapsulaion is the way to go.
If you absolutely must use globals, please use a namespace.
Thank you very much
Topic archived. No new replies allowed.