Answer:
#include <iostream>
using namespace std;
int max_magnitude(int firstValue, int secValue)//taking two itnerger inputs
{
if (firstValue > secValue)//using if condition
return firstValue;
else
return secValue;
}
int main()
{
int firstVal, secVal, finalVal;
cout << "Enter your first value: " << endl;
cin >> firstVal;
cout << "Enter your second value: " << endl;
cin >> secVal;
finalVal = max_magnitude(firstVal, secVal);//calling function
cout << "The greater value is: " << finalVal << endl;
return 0;//terminating program
}
Step-by-step explanation:
This exercise is for you to understand how to make and call functions. Functions are a way to basically clean the main code. If you read the main code, you can intuitively tell that the finalVal variable is being assigned a max magnitude of some kind. The program would have run absolutely fine if i simply copy pasted the function inside my main. But imagine, if I had to call this function 1000 times, it would've looked quite ugly.
Secondly, there is a certain way how functions work. Function can, and may not take arguments, depending on what it is supposed to do. for example in this case, the function is taking two arguments, because it needs two compare two values from the main function. Now imagine if I made a function to say, exit the program. I would need any arguments. I would simply call the function and it would say 'return 0' and the program will end.
Thirdly, functions may or may not RETURN a value. I our case if you look closely, our function is called 'int' max_magnitude. The int is signalling what type it will return. This means that when the function completes its processing, it will return to where it was called from and give back the value it ended on.