Simple Example of Reusability in C++

Chandra Penke
1 min readMay 26, 2020

Suppose the following code is in a file called main.cpp:

#include <cmath>
#include <stdio.h>
using namespace std;int main(int argc, const char *argv[])
{
int x = 1, y = 2;
// of (1, 2)
int m1 = sqrt(x * x + y * y);
cout << m1 << "\n";
int x = 2, y = 3;
// magnitude of (2,3)
int m2 = sqrt(x * x + y * y);
cout << m2 << "\n";
}

By implementing a reusable function, magnitude, the code can be simplified to:

#include <cmath>
#include…

--

--