#WSQ08-On to functions

Hey world…

In this WSQ I had to go back to the WSQ03 and yes is the same but the difference with this one, is that now the code has functions out of the “main” and I learnt how to create th synthesis of a function in C++.

You will go back and do WSQ03 – Fun with Numbers again.

But this time, write a function for each calculation. Each function should define two parameters (in this example of type int) and return the correct value as an integer as well.

You main program needs to ask the user for the input and then call each function to calculate.

//CODE:

#include <iostream>
using namespace std;

int difference(int a, int b) {
return(a-b);
}
int product(int a, int b) {
return(a*b);
}
int integerdiv(int a, int b) {
return((int)(a/b));
}
int residue(int a, int b) {
return(a%b);
}

int main(int argc, char const *argv[]) {
int x, y, d, p, i, r;

std::cout << “This program is similar to WSQ03, but now with functions.” << std::endl;
std::cout << “Please enter number 1.” << std::endl;
std::cin >> x;
std::cout << “Please enter number 2.” << std::endl;
std::cin >> y;
d=difference(x, y);
p=product(x, y);
i=integerdiv(x, y);
r=residue(x, y);
std::cout << “The difference is… “<<d << std::endl;
std::cout << “The product is… “<<p << std::endl;
std::cout << “The integer division is… “<<i << std::endl;
std::cout << “The residue is… “<<r << std::endl;
return 0;
}

This are all the functions I created.

And this is how they are called into the “main”.

Captura de pantalla 2016-02-17 a las 18.49.12

CC BY-SA 4.0 #WSQ08-On to functions by everolivares is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.