-1

I know the way we use the function in c++ is as follows

function declaration // before main

function calling    // in main

function definition // outside main

But as I used opencv in project , I also made function in it , but I didn't use function declaration in it and it works fine without any error , I use the same tool , same language same technique . Why is function declaration is not compulsory in opencv ? or is it done with other way ?

Sample code :

void generateGradient(cv::Mat& mask)
{
    cv::Point firstPt = cv::Point(mask.size().width/2, mask.size().height/2);
    double radius = 1.0;
    double power = 0.8;

    double maxImageRad = radius * getMaxDisFromCorners(mask.size(), firstPt);

    mask.setTo(cv::Scalar(1));
    for (int i = 0; i < mask.rows; i++)
    {
        for (int j = 0; j < mask.cols; j++)
        {
            double temp = dist(firstPt, cv::Point(j, i)) / maxImageRad;
            temp = temp * power;
            double temp_s = pow(cos(temp), 4);
            mask.at<double>(i, j) = temp_s;
        }
    }
}

    int main()
    {
        cv::Mat img = cv::imread("stack-exchange-chefs.jpg");
        cv::Mat maskImg(img.size(), CV_64F);
        generateGradient(maskImg);
    }

Thank you

  • 2
    It's a basic attribute of the language and not specific to OpenCV. I suggest you pick up a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that covers the basics of C++. – Captain Obvlious Jul 11 '14 at 15:00
  • I update the code , but this question have nothing with the code , its a genral question related to the programming – Rabii Khalid Jul 11 '14 at 15:07

3 Answers3

4

The rules are (loosely):

  1. A declaration of a function must have come previously for you to call it.

  2. A definition of that function must exist somewhere within your project.

The example you gave fulfils those rules. So does this:

function definition

function calling

Because a definition is a declaration.

We like to split our code up among different files. Each .cpp file in a project is compiled separately and then linked together. It's okay to compile a file and not find a definition for a function, as long as the linking stage later finds that definition.

That's how the basic structure of a C++ program works. You might have, for example, files main.cpp and A.cpp which both include A.h. Inside main.cpp we are using a function that is declared in a A.h and defined in A.cpp. Even though main.cpp and A.cpp are compiled separately, the definition of the function that main.cpp uses is found when linking the two together at the end. main.cpp only requires the declaration of that function, which is given in A.h.

Chances are, you are including a header from OpenCV. That's just like including A.h in the previous example. It provides you some declarations of functions that you might like to use. You don't then need to write a declaration yourself because it was provided by the header. The actual definition of that function has already been compiled and is available in the OpenCV library, which you link to when you compile your program.

That is, your file looks like this:

#include <opencv/some_header>

function calling

Well if you preprocessed this file, you'd bring the contents of the header into it and it would look like this:

function declaration

function calling

This is fine. The function definition is then linked in from the library.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

The theory is at least the function signature declaration should be available when it is called as the compiler wants to know the signature of the function.

That is why we include header files to cpp/c files. other than the include the compiler cannot identify the function signature and cannot detect any compiler error in cpp/c files in the compilation.

Sujith Gunawardhane
  • 1,251
  • 1
  • 10
  • 24
0

In C++ function declarations are required because language features like overloading rely on it.

etr
  • 1,252
  • 2
  • 8
  • 15