Question about Functions

Hi,

I was wondering if someone would be so kind to answer my question(s).

Basically, what I would like to know is if it is possible to declare variables inside functions and initialise them with the value inputted by the user?

Thanks

Well, this is rather straightforward for an answer: yes. You can.
Yes.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int get_number_from_user()
{
     int number ; // local variable defined at function scope

     std::cout << "please enter a number: " ;
     std::cin >> number ; // get value from user

     return number ;
}
Thanks Ispil.

This leads me to my next question then.

If I was wanting to create a function of type float which returned the calculated value from the function would this work?

float convertor ();//declaration of prototype

//function takes inputted temp in celsius and converts it to fahrenheit and returns the value
float convertor()
{
float temp_celsius;
cout << "Please enter temperature in Celsius: ";
cin >> temp_celsius;
return (((temp_celsius * 9)/5) + 32);
}

If I were to call this function would it return the calculated value?
Last edited on
Thanks JLBorges, I think your post answered the last question I posed.

Going to try and implement this to resolve the problem I have. I will be sure to post back if I have any issues.

Thanks for all the help guys. you are the best. :)
Prefer writing the function this way:

1
2
3
4
5
// prefer using double as the floating point type
// give the function and its parameters names pregnant with meaning
// the function takes as input the temperature in celsius
// and returns the equivalent temperature in fahrenheit
double to_fahrenheit( double celsius ) ;


And get the input (temperature in celsius) from the user in main() and pass it to the function.

1
2
3
4
double temp_celsius ;
std::cout << "please enter temperature in celsius: " ;
std::cin >> temp_celsius ;
const double temp_fahrenheit = to_fahrenheit(temp_celsius) ;


A good function is a function that does one small thing, and does it well.
This one converts from celsius to fahrenheit, it doesn't need to know how the celsius value was obtained.
What is the benefit of declaring the variable in main and passing it in by value?
> What is the benefit of declaring the variable in main and passing it in by value?

Ok, let us consider a hypothetical situation.
After this program is done and dusted, you are asked to write another program:

Print out a celsius to fahrenheit conversion chart from 00 celsius to 1000 celsius in steps of 50.
Your output should look something like this:

1
2
3
4
5
6
Celcius      Fahrenheit

 0.0            32.0
 5.0            41.0
10.0            50.0
etc...


Wouldn't it make you feel good if you discover that you can reuse, as it is, the converter function that you had already written, tested, fixed errors and finally got working?
Last edited on
I am quite new to programming and would like to get in to good habits early.

Someone told me that it is best to keep the main body as free as possible and that is why I thought of putting the variable in the function, as it was not going to be used anywhere outside of the function.

However, I take your point that by declaring the variable in the main program it makes the code easier to reuse in the future as I can export the convertor function.

Thanks for clarifying this for me.
Keeping the main function as free as possible doesn't mean to not put stuff in it as much as not to clutter it up and make it hard to understand. Suppose you were asked one week to write a function that added two numbers and then two weeks later asked to modify it to multiply the result with another number. You would just be cluttering up your function instead of main. Something like this is much easier read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

using namespace std;

int add(int a, int b);
int multiply(int a, int b);

int main()
{
	int a;
	int b;
	int x;
	int first;
	int second;

	cout << "Enter a number:";
	cin >> a;
	cout << "Enter another number to add to the first:";
	cin >> b;
	
	first = add(a, b);
	cout << a << " plus " << b << " is " << first << '\n';
	
	cout << "Enter a number to multiply the first result by:";
	cin >> x;
	
	second = multiply(first, x);
	cout << first << " times " << x << " is " << second << '\n';
	
	system("pause");
	return 0;
}

int add(int a, int b)
{
	return a + b;
}

int multiply(int a, int b)
{
	return a * b;
}


Besides being able to reuse your first function as JLBorges said, it is easier to just modify what your asking the user in main and get all kinds of different variations. The variable list might get longer along with the cout/cin statements but that's about all.
Topic archived. No new replies allowed.