Creating functions:
The first thing you have to do it´s define it. After the name is given you should assing to variables the already given parameters (parameters given in the main). You can create new variables where the main variables will be assigned or just call them as the variables given such as the example. Remember that the parameters are always inside os parenthesis. After define it, and declaring the parameter { } are required. If your function it´s an integer remember to return something, it can be a random number. If you have a void function remember that you chould not return something.
Code example:
#include <iostream>
using namespace std;
int sumatoria (int FIRST,int SECOND ) //DEFINE function NAME (PARAMETERS<– given in the main) { WORK }
{
cout << “The sumatory it´s: ” << FIRST + SECOND << endl;
cout << “THe diference it´s: ” << FIRST – SECOND << endl;
cout << “The remainder of: (FIRST/SECOND) it´s: ” << FIRST % SECOND << endl;
cout << “The result of the division of (FIRST/SECOND) it´s: “<< FIRST/SECOND << endl;
return 0;
}
void sumatoria1 (int FIRST, int SECOND)// DEFINE function NAME (PARAMETERS<– given in the main) { WORK }
{
cout << “The sumatory it´s: ” << FIRST + SECOND << endl;
cout << “THe diference it´s: ” << FIRST – SECOND << endl;
cout << “The remainder of: (FIRST/SECOND) it´s: ” << FIRST % SECOND << endl;
cout << “The result of the division of (FIRST/SECOND) it´s: “<< FIRST/SECOND << endl;
} //WON´T RETURN AN INTEGER
int main ()
{
int FIRST,SECOND;
cout << “The following program will compute the sumatory, diference, remainder and division of two given numbers ” << endl;
cout << “First number: ” << endl;
cin >> FIRST;
cout << “Second number: ” << endl;
cin >> SECOND;
sumatoria (FIRST, SECOND); //Giving parameters to the function
sumatoria1 (FIRST, SECOND);//Giving parameters to the function
return 0;
}
#Mastery12 #TC1017 by Pablo Guerra is licensed under a Creative Commons Attribution 4.0 International License.