agr
agr

Reputation: 127

Is function prototype compulsory in C++?

By this I mean that even though I define the function before the function call,is it required to have a function prototype? I tried a program with definition b4 call,it worked.but in C++ by Herbert schildt it says C++ requires full function prototyping.so am confused. So,does C++ really require full function prototyping?

Upvotes: 0

Views: 1414

Answers (4)

ytobi
ytobi

Reputation: 673

A function prototype or function interface is a declaration of a function that specifies the function's name and type signature (arity, data types of parameters, and return type), but omits the function body

a function prototype merely specifies its(the function) interface(I/O).

Prototype of a function is also called signature of the function.

If a call or reference has to be made to a function in a code section before the actual function definition, then the function prototype is compulsory.

A function prototype can be "discerned" or gotten from its definition, hence if a call is not made to the function before its actual definition, declaring the function prototype is not compulsory.

Upvotes: 2

Alpha Mineron
Alpha Mineron

Reputation: 348

Considering the question, I'll assume that you are at the beginner level. First of all, You should start learning from a better book.

Look at the compiler as an intelligent person who reads your program and converts it into machine language. When the compiler starts compiling your program it may encounter a function call such as: foo(); Now, it has to make the control jump to the function's address for the desired instructions to be executed during run but the compiler doesn't know what or where the function is! Providing the compiler with a reference helps it to compile your code.

Prototypes are such references, you could also define a function before any function calls and it would still work.

Other than technical reasons, prototypes makes code much more clean in case of big projects.

Otherwise, you would just be scrolling through Functions definitions having no idea what the functions are doing. Remember, you are like a human compiler too!

A usual mistake I've noticed: It is usual that people revolt this idea and define their functions on top. Although, when calling another function from a function, problems can arise.

int function_1()
{
        return function_2();
}

int function_2()
{
        return function_1();
}

Here, there are no arrangements of functions definitions which will work. Prototyping is much better.

Assumptions: There are obviously some more statements to break the endless loop and return values, this is just to illustrate the concept.

Upvotes: 0

user0042
user0042

Reputation: 8018

By this I mean that even though I define the function before the function call,is it required to have a function prototype?

No, it doesn't mean that.

So,does C++ really require full function prototyping?

Yes it does. The compiler needs to see at least the declaration of a function before it can be used:

void foo(int i);

Alternatively you can provide a full (inlined) definition

inline void foo(int i) {
   // do something
}

Upvotes: 0

user2100815
user2100815

Reputation:

A function definition is also a function declaration (i.e. a prototype). So if you define the function before you call it everything will be hunkydory. And I would strongly recommend not attempting to learn C++ from the works of Herb Schildt.

Upvotes: 6

Related Questions