How do I make a function in C++ that can take an input as a parameter? For example, here is an addition function below.
#include <iostream>
using namespace std;
double add(double a, double b)
{
return a + b;
}
int main()
{
add(3.1, 5.3);
return 0;
}
The output will be 8.4. I specified what a and b were as I called the function. But if I wanted to take a and b as inputs, how can I do that? I tried to make the function have no parameters and take a and b as the input from there, but I know there is a better way. I also tried add(cin >> a, cin >> b);
, but that didn't compile. Is there a way in which I can use the parameters a and b as inputs, instead of giving them a value right away when we call the function? Thanks!
Here is the same function in Python:
def add(a, b):
return a+b
add(int(input("Enter your first number")), int(input("Enter your second number")))
output:
8