I'm trying to learn to program and have made a program for my physics work, is there a way to make this program better by putting it into functions? such as having the variables in different functions so everything isn't in the main, or is that how it should be? I tried doing this myself but I don't understand functions well enough to do it.
Thanks.
#include <iostream>
#include <math.h>
usingnamespace std;
int main()
{
cout << "program to calculate charge and how many electrons are needed to carry that charge" << endl;
cout << "Enter the current in amps: " << endl;
float I;
cin >> I;
cout << "Enter the time in seconds: " << endl;
float t;
cin >> t;
float C = I*t;
cout << "The charge is: " << C << " coulombs" << endl;
double e = 1.6*pow(10, -19);
cout << C/e << " electrons are required to carry a charge of " << C << " coulombs" << endl;
return 0;
}
There, function double e = 1.6*pow(10, -19); hehe, just use function only when you need it somethin like to make your code more easiily to read, or multiple use of a block of code, well its your choise anyway
You can put all of your code into a procedure then call it from main()
Use functions when your code has one or more independent parts.
So one function is responsible for one part.
Then you combine all of them by calling each one from main.