0

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
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • You are asking about how to read commandline parameters to your program? Or how to read input from interactive, e.g. console? – Yunnosch Jun 25 '20 at 16:01
  • I want to make the inputs interactive, like in console. – DragonFish12345 Jun 25 '20 at 16:02
  • 1
    What does your C++ book about this? – Stephen Newell Jun 25 '20 at 16:03
  • 1
    Does this answer your question? [Read input.txt file and also output.bmp file from terminal (C-programming)](https://stackoverflow.com/questions/47535120/read-input-txt-file-and-also-output-bmp-file-from-terminal-c-programming) – Yunnosch Jun 25 '20 at 16:03
  • Ah, sorry. Then the duplicate proposal is off. But then I have to ask, which tutorial you tried and why it did not help you. – Yunnosch Jun 25 '20 at 16:04
  • Sorry nope, I haven't learned much C++ yet to deal with files. I just want to use something like cin >> to get the input, but I can't seem to make it work with a and b are parameters. – DragonFish12345 Jun 25 '20 at 16:05
  • 1
    Sorry, again, it seems " take a and b as the input from there" means you do know how to read input from user... But still what is the obstacle? Can you show the code for that attempt and explain in which way exactly that failed? – Yunnosch Jun 25 '20 at 16:06
  • I have been trying from here. https://youtu.be/vLnPwxZdW4Y?t=5559 , at that time stamp. He shows that you can call a function, enter in a parameter between the parenthesis and it will help. However, he does not show if you could use the parameters as inputs. Can I try to make an example in Python? – DragonFish12345 Jun 25 '20 at 16:09
  • 2
    Pleae do not link videos. Show code instead, along with errors messages or reports of observed misbehaviour. – Yunnosch Jun 25 '20 at 16:10
  • 1
    I have tried this: #include using namespace std; double add(double a, double b) { return a + b; } int main() { add(cin >> a, cin >> b); return 0; } – DragonFish12345 Jun 25 '20 at 16:12
  • Sure, you can show the code that you would write in python, and there may be a way to do that in c++. Please add all information to the question, not as comments. – cigien Jun 25 '20 at 16:13
  • Okay, here is the code in Python. def add(a, b): print(a+b) add(int(input("Enter your first number")), int(input("Enter your second number")) – DragonFish12345 Jun 25 '20 at 16:15
  • Sorry, that looks really messy on stack comments. – DragonFish12345 Jun 25 '20 at 16:15
  • Please [edit] to add info to the question instead of to the comments down here. It will make the code look much more readable. – Yunnosch Jun 25 '20 at 16:18

3 Answers3

2

Something like this should do it. You can use cin to read in values for your variables from the user via console.

#include <iostream>
using namespace std;

double add(double a, double b)
{
        return a + b;
}

int main(){
double a, b, c;

cout << "Enter two integers: ";
cin >> a >> b;
c = add(a, b);

// Prints sum 
cout << a << " + " <<  b << " = " << c;     

return 0;

}

1

You could try passing the stream:

double Add(std::istream& input)
{
  double a, b;
  input >> a >> b;
  return a + b;
}

int main()
{
  std::cout << "Enter two numbers: ";
  double sum = 0.0;
  sum = Add(std::cin);
  std::cout << "\n"
            << "Sum is: " << sum << std::endl;
  return EXIT_SUCCESS;
}

A more common approach is to read the values in main() then call your function with the two values.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • This is probably exactly what OP meant by "take a and b as the input from there" and hence unlikely to be helpful. I still wonder what exactly the problem is and think this answer might be a little quick. – Yunnosch Jun 25 '20 at 16:08
1

You can write a function that reads values from standard input, and use that for either of the arguments to add, like this:

auto input()   // reads a double from cin
{
    double d;
    std::cout << "enter a number ";
    std::cin >> d;
    return d;
}

double add(double a, double b)
{
    return a + b;
}

int main()
{
    std::cout << add(input(), input());
    std::cout << add(input(), 3.2);
    std::cout << add(4.5, input());
    std::cout << add(4.5, 3.2);
    return 0;
}

Here's a demo.

Note that the first call to add only works if the order that you input the 2 values doesn't matter. In other words, add should be commutative. A function like subtract, where the order of inputs matters, won't work with the first function call.

cigien
  • 57,834
  • 11
  • 73
  • 112