1

I am looking at an old book and it contains function prototypes. For example:

#include<iostream>
using std::cout;
int main()
{
    int square(int); //function prototype

    for(int x = 0; x<=10; x++)
    {
        cout<<square(x)<<"";
    }

    int square(int y)
    {
        return y * y;
    }

    return 0;
}

However, on newer C++ tutorials, i don't see any function prototypes mentioned. Are they pbsolete after C++98? What are the community guidelines for using them?

Example: https://www.w3schools.com/cpp/trycpp.asp?filename=demo_functions_multiple

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
user1584421
  • 3,499
  • 11
  • 46
  • 86
  • If you removed `int square(int);` you would get a compiler error. Because square is not known at `cout< – drescherjm Dec 03 '21 at 17:51
  • C++ lets you define functions within functions within functions. Is this actually a thing now? – selbie Dec 03 '21 at 17:52
  • In your example project at the link move the implementation of `void myFunction() {` to after `int main()` and see what happens. – drescherjm Dec 03 '21 at 17:54
  • The C++ nomenclature tends to be "function declaration", rather than "prototype". They're still used. – Nathan Pierson Dec 03 '21 at 17:54
  • @selbie gcc extension probably. https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html It does say it isn't supported in C++ though, so who knows? – Retired Ninja Dec 03 '21 at 17:55
  • @drescherjm It says that the function was not declared in this scope. – user1584421 Dec 03 '21 at 17:57
  • If the book you're reading really does define (implement) the `square` function *inside* the `main` function, you should throw it away. You probably should do it anyway if it's old enough to be before the C++ standardization. [Here's a list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) of more modern books. – Some programmer dude Dec 03 '21 at 17:59
  • ***It says that the function was not declared in this scope.*** That is the reason why in modern c++ we still need function declarations and the answer to your question is No they are not obsolete. – drescherjm Dec 03 '21 at 17:59
  • @selbie Actually that functionality was added in C++11, as lambdas... ;) – Some programmer dude Dec 03 '21 at 18:00
  • @Someprogrammerdude - lambda yes. But not like the above, right? At least not on the Visual C++ compiler I have in front of me. But maybe g++ ? What's actdually standard vs extension here? – selbie Dec 03 '21 at 18:01
  • @selbie No "normal" functions like that are not allowed to be nested. As I said if that's in the book then it should be thrown away. – Some programmer dude Dec 03 '21 at 18:04
  • 2
    @Someprogrammerdude - About once a month, I see someone post some C++ code that makes me question if I actually know this programming language or not. :) – selbie Dec 03 '21 at 18:07

2 Answers2

3

For starters defining a function within another function like this

int main()
{
    //...
    int square(int y)
    {
        return y * y;
    }

    return 0;
}

is not a standard C++ feature. You should define the function square outside main.

If you will not declare the function square before the for loop

int square(int); //function prototype

for(int x = 0; x<=10; x++)
{
    cout<<square(x)<<"";
}

then the compiler will issue an error that the name square is not declared. In C++ any name must be declared before its usage.

You could define the function square before main like

int square(int y)
{
    return y * y;
}

int main()
{
    //...
}

In this case the declaration of the function in main

int square(int); //function prototype

will be redundant because a function definition is at the same time the function declaration.

What are the community guidelines for using them?

A function with external linkage if it does not have the function specifier inline shall be defined only once in the program. If several compilation units use the same function then they need to access its declaration.

In such a case a function declaration is placed in a header that is included in compilation units where the function declaration is required and the function definition is placed in some module.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

They're not obsolete but they would mostly be in header files rather than *.cpp files.

They may be called "forward declarations" by some C++ tutorials (such as learncpp.com). Here's a page talking about them: https://www.learncpp.com/cpp-tutorial/forward-declarations/

Cannon
  • 51
  • 2