0

I am learning c++ and I come across problems in this program. What I'm trying to do is to write a function computing max of 2 parameters x and y, each of type “reference to const double”. The function returns type “reference to double”. Return the entire conditional expression, type casting it as (double &) to override the “const" variable.

So I write this function in a file named "fmax.cpp"

double& fmax(const double &x, const double &y)
{
    return (x > y) ? x : y; //Need type casting here?
}

I try to call the funciton in another file main.cpp

include <iostream>
using namespace std;

double& fmax(const double &x, const double &y);

int main()
{
    const double &x; //should I put &x or x here?
    const double &y; //should I put &y or y here?

    cout << "enter 2 values" << '\n';
    cin >> x >> y; //should I put & or no & here?

    //Am I calling the function correctly?
    cout << fmax(double &x, double &y) << '\n';
    return 0;
}

Please advise how to write the program correctly.

Simon S.
  • 65
  • 4
  • 5
    Lots of mistakes in this code, sounds like you should [read a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Paul Sanders Jul 17 '20 at 13:44
  • 4
    May I suggest getting [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? Almost every line with a comment is wrong (including your choice for the name of the function when combined with `using namespace std;`) – Yksisarvinen Jul 17 '20 at 13:44
  • You have declared the variable as const, then you won't be able to assign a value to it in cin line. You should also create a header file and include it in main.cpp. Why do you want to define reference variable? No you are not calling it correctly. There are few other mistakes too. You can follow some cpp tutorials, which will help in understanding this concepts. – Deepak Patankar Jul 17 '20 at 13:46

2 Answers2

3

This program should look like this:

#include <iostream>

double fmax(double x, double y) {
    return x > y ? x : y;
}

int main()
{
    double x; 
    double y; 

    std::cout << "enter 2 values" << '\n';
    std::cin >> x >> y;

    const auto res = fmax(x, y);
    std::cout << res << '\n';
    return 0;
}

There is no use to pass doubles (ints, floats and other small types) by references, because size of a memory address being passed is the size of the platform pointer (4 bytes on 32bits, 8 bytes on 64bits). Size of int is 4 bytes, for example.

I added const and auto as an example.

Vasilij
  • 1,861
  • 1
  • 5
  • 9
  • This topic is covered here: https://stackoverflow.com/questions/40185665/performance-cost-of-passing-by-value-vs-by-reference-or-by-pointer Sorry to give links instead of an answer, but this is a good one. TLDR: passing small types by value is faster. – Vasilij Jul 17 '20 at 14:04
2

There are several defects in the code:

  1. Only a function signature exists, but nowhere defined or linked any header file.

  2. You don't need to use:

    fmax(double &x, double &y)
    // __^^^^^^_____^^^^^^____ errors
    
  3. Used const qualifier when taken reference, it's usually used when the variable is guaranteed to not be changed anywhere in the code. You don't need to use a reference here.

Beware of using const when the program is going to alter the value of the variable. Otherwise, errors will be generated.


Code redefined:

fmax.h

double& fmax(const double &x, const double &y);

fmax.cpp

double& fmax(const double &x, const double &y) {
    return (x > y) ? x : y;
}

main.cpp

#include <iostream>
#include "fmax.h"

int main(void) {
    double x, y;

    std::cout << "Enter 2 values: ";
    std::cin >> x >> y;

    std::cout << fmax(x, y) << '\n';

    return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34