answersLogoWhite

0


Best Answer

It allows the compiler to verify that the number and types of the functions parameters are correct when the function is actually called.

If the function were called with the wrong number or type of parameters, and no function prototype were supplied, the error would not be discovered until the program was actually run.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

The function prototype declares the type of the function, and the type and number of its parameters. This allows the compiler to check for valid calling sequence, and to generate the correct code. It was optional in C, and it is required in C++.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A function prototype is simply a forward declaration of a function that will be defined elsewhere in your code. A function prototype must include the function's return type, its identifier (its name) and the type of all its parameters. If no parameter types are specified, void is implied in C++ (but not in C). Parameters needn't be formally identified in the prototype, only their type is required. Formal parameter identifiers are only required in the definition of a function.

The prototype must also be declared prior to any calls to that function, even if it has not been defined before those calls appear. At compile time, the compiler uses the prototypes to ensure all function calls match the signatures declared in the prototypes, emitting an 'identifier not found' error if no prototype exists. At link time, the linker will marry the function calls to the appropriate definitions, emitting an 'unresolved external symbol' error if no definition exists.

Quite often there is no need to prototype functions. They can be both declared and defined as a single statement immediately before any functions that use them. While this is perfectly adequate for trivial programs, as programs become larger and more complex it makes sense to think in a more modular fashion: placing prototypes in a header file that can be included wherever required; placing the definitions in a corresponding source file; grouping header/source pairs by function or class. Sooner or later you will want to build libraries of useful functions and classes that can be re-used in several programs, and prototyping makes it that much easier because you've already modularised the code.

The only real exception to separating a prototype from its definition is when declaring class templates. They can still be separated, of course, but the definitions must be placed in the header along with the prototype. This is simply because class templates are incomplete classes and the compiler requires the complete definition, in one place, in order to generate the actual classes that will be used at runtime.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

A function prototype is a function declaration without a definition. In other words, it is a forward declaration of a function that is defined elsewhere. The definition may come later in the same translation unit or it may be defined in another translation unit altogether.

Before a user-defined name can be used it must first be declared so that the compiler knows what the name refers to and what operations are valid for that name. In the case of functions, the compiler needs to know the return type and the number and type of each of its arguments (if any). This information is proved by the function's declaration.

It is important to note that a definition is itself a declaration. As such, it is not always necessary to forward declare a function, provided it is defined before it is used. Typically, we only forward declare functions that are intended to be used by two or more translation units or when two or more functions depend on each other. To ensure consistency across all declarations of the same function, we normally place the function declaration in a header file which can then be included in any translation unit that uses that function:

Note that class member functions do not need to be forward declared because their declarations are contained within a class definition. That is, a class definition can call any member function, even if the declaration of that function appears later within the class definition.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantage of function prototype?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When can a function prototype be omitted?

when we write definition of a function i.e; body of a function above main() function, then the function prototype be omitted. -Ramashankar Nayak,M.C.A,Pondicherry University


What errors do function prototype help prevent?

Syntax errors and prototype errors.


What is prototype in C language?

A prototype in C is the declaration of a function. Without a prototype, the function cannot be called because the compiler would have no way of knowing if the function was being called correctly. Prototypes may appear in multiple translation units but can only be defined once. A definition is itself a prototype.


Why you do not declare the function prototype?

Your question makes no sense.


Function prototype in c?

Yes. Examples can be found in stdio.h

Related questions

When can a function prototype be omitted?

when we write definition of a function i.e; body of a function above main() function, then the function prototype be omitted. -Ramashankar Nayak,M.C.A,Pondicherry University


Is the main advantage of a prototype is users can work with the system before it is completed?

Essentially, yes. In computer programming terms, a prototype provides the compiler with a function's signature, making it possible for code to invoke that function even if the function definition hasn't yet been encountered by the compiler. In other words, it is not necessary to know what the function does in order to call it; the prototype provides all the information required to invoke the function. Outside of computing, a prototype is a working model of a system that developers can use to demonstrate the system's functionality and to work upon improvements to the design before going into full production.


What errors do function prototype help prevent?

Syntax errors and prototype errors.


How importance is a prototype in software engineering?

In Prototyping model instead of the end product a prototype is made. Advantage of prototype is that it helps in gathering and refining of requirements.


What is prototype in C language?

A prototype in C is the declaration of a function. Without a prototype, the function cannot be called because the compiler would have no way of knowing if the function was being called correctly. Prototypes may appear in multiple translation units but can only be defined once. A definition is itself a prototype.


What you declare in the function prototype in c?

yes


Can you write a program without specifying the prototype of any function?

You can write a program without specifying its prototype when the function returns an integer.If the prototype is not mentioned the compiler thinks that the return type of the function used is integer.When making program which return integer you can ignore writing the protoype.


Why you do not declare the function prototype?

Your question makes no sense.


Do declarations describe the data that will be used in the function?

Prototype of function describes return value as well as which data can be passed to the function.


What is the significance of using void in functions parameters lists?

It means that the function doesn't have parameters.example:int foo (void); -- it is a prototype for function 'foo', having no parametersint bar (); -- it is not a prototype as it says nothing about the parameters


What is a function prototype and how are they used?

A function prototype is the first line of a function which depicts its access specifier, modifier ,return type and name along with the number and type of arguments in it.. example: public static void zoom(int a,String b)


Function prototype in c?

Yes. Examples can be found in stdio.h