0

I'm trying to create a program with functions that prints the smaller number between two numbers. But when i assign a function to a variable it shows this error:

error: invalid conversion from ‘int (*)(int, int)’ to ‘int’ [-fpermissive]

The code :

#include <iostream>
using namespace std;
int minimum(int num1, int num2){
    int min;
    if(num1 > num2)
        min = num1;
    else
        min = num2;
    return min;
}
int main()
{
    int c{100};
    int d{200};
    int result;
    result = minimum;
    cout<<"The smallest number is " <<result;;
}

And it also shows this error :

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘’)

It'll be really helpful if you could give me a solution!

  • 1
    What do you think `result = minimum;` does? Where inside the `main` function is there a definition of a `min` variable? It seems you have skipped way to much in your text-book or tutorial or class. – Some programmer dude Jun 18 '20 at 05:54
  • i think it assigns minimum to result... –  Jun 18 '20 at 05:55
  • Yes, it tries to assign the *pointer to the `minimum` function* to the `int` variable `result`. It does *not* call the function in any way. It does *not* get any actual result from the `minimum` function. What you're doing is essentially `result = &minimum;`. – Some programmer dude Jun 18 '20 at 05:56
  • On an unrelated note, your `minimum` function is badly named, as it returns the *maximum* of two `int` values. – Some programmer dude Jun 18 '20 at 05:59

2 Answers2

1

I am pretty sure that you intended to use

result=minimum(c,d);

Additionally, inside the function definition, the direction of > should be changed to <.

K.R.Park
  • 1,015
  • 1
  • 4
  • 18
  • Thanks! it worked! but how do these parameter lists work? –  Jun 18 '20 at 05:56
  • @AustinParker Simple. minimum means the function pointer, and minimum(a,b) references the pointer with parameters a,b. – K.R.Park Jun 18 '20 at 05:59
  • 1
    @K.R.Park Actually, `a` and `b` are function _arguments_. _Parameters_ are `num1` and `num2`. – Daniel Langr Jun 18 '20 at 06:12
  • 1
    @AustinParker Why don't you learn C++ by reading some good material, e.g., some [good book](https://stackoverflow.com/q/388242/580083) for beginners. If you don't know how to call a function, learning-be-trial is quite a wrong way. – Daniel Langr Jun 18 '20 at 06:13
  • @DanielLangr Oh...You are right. Thank you for clarifying it. – K.R.Park Jun 18 '20 at 06:30
1

That happens because you call the function without any parameters, also min is never created and your function currently checks for maximum number.

#include <iostream>
using namespace std;
int minimum(int num1, int num2) {
    int min;
    if (num1 < num2)
        min = num1;
    else
        min = num2;
    return min;
}
int main()
{
    int c{ 100 };
    int d{ 200 };
    int result = minimum(c, d);
    cout << "The smallest number is " << result;
}
SzymonO
  • 432
  • 3
  • 15