How to put program into functions

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <math.h>
using namespace 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;
}
You dont need any function for this program.
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.

But your program is small and does not need such.
Topic archived. No new replies allowed.