answersLogoWhite

0


Best Answer

A function prototype is basically a definition for a function. It is structured in the same way that a normal function is structured, except instead of containing code, the prototype ends in a semicolon.

Normally, the C compiler will make a single pass over each file you compile. If it encounters a call to a function that it has not yet been defined, the compiler has no idea what to do and throws an error. This can be solved in one of two ways.

The first is to restructure your program so that all functions appear only before they are called in another function.

The second solution is to write a function prototype at the beginning of the file. This will ensure that the C compiler reads and processes the function definition before there's a chance that the function will be called.

For example, let's take a look at a few functions form a linked list implementation.

// Sample structures

struct linked_list_node {

int data;

struct linked_list_node *next;

};

struct linked_list {

int size;

struct linked_list_node *root;

};

// Function Prototypes

void deleteLinkedList(struct linked_list *list);

void deleteNodes(struct linked_list_node *node);

// Actual functions:

// deletes the given linked list

void deleteLinkedList(struct linked_list *list) {

if( list != NULL ) {

// delete nodes

deleteNodes(list->root);

// lose the pointer

list->root = NULL;

// delete actual list

free(list);

}

}

// deletes all nodes starting at node

void deleteNodes(struct linked_list_node *node) {

if( node != NULL ) {

// deallocate next, if it exists

if( node->next != NULL ) {

deleteNodes(node->next);

// lose the pointer

node->next = NULL;

}

// deallocate node

free(node);

}

}

User Avatar

Wiki User

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

Wiki User

14y ago

The function prototype serves to ensure that calls to the function are made with the proper number and types of arguments. In the case of function overloading, the different prototypes serve to distinguish which version of the function to call.

The compiler will complain with an error, if no function prototype is found for any particular call to a function.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

In a nutshell, function prototypes help speed up compilation times. Without function prototypes, the compiler cannot determine if a function call is well-formed or not until the function is fully-defined. The earlier that definition is encountered in the compilation process the better because if the call is badly-formed, the compilation will fail, and the earlier it fails, the quicker the programmer can fix the problem and recompile.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A function prototype is just another name for a function declaration which essentially describes the function's interface (as opposed to its implementation). The declaration must include the name of the function, the number and type of its arguments (if any) and the return type. Formal argument names are not required in a prototype, nor is the implementation, but the implementation must be defined somewhere, whether it is in an external library which links to your program, a separate source file within your program, or implicitly inline expanded within the prototype itself.

Prototypes are typically placed in a header file while the implementation is placed in a corresponding source file. Separating interfaces from implementations in this manner modularises your code. When you need to make use of a function, you simply include the header that contains the prototype. The prototype provides all the information required in order to use the function, and tells the compiler all it needs to know in order to match the interface with its definition.

Prototypes are also used to forward declare functions. In C++, a function must be declared before it can be used, even if the function is not fully defined at the point it is first used. By placing prototypes in a header, the programmer does not have to look up the prototype in order to write a forward declaration, he simply includes the header that contains the prototype itself. When you include a header it's just as if you'd copy/pasted the entire contents of that header into your source code at the point of inclusion.

Note that templates must be fully-defined before they can be used. Typically the definition is placed in the same file as the prototype (in some cases inline expanded), but you can also include the definition file in the prototype header in order to modularise interfaces and implementations.

Note also that the definition of a function is also a declaration of that same function, even if it was forward declared. However, there can only ever be one forward declaration and one definition at most of any one function (the "one definition rule", or ODR). This presents a problem when you have several source files that all require access to the same function prototype, since you'd effectively be declaring the function once for each inclusion.

This problem is resolved by using inclusion guards in the header. An inclusion guard is a compiler directive that ensures a file is only ever included once in any compilation. A typical inclusion guard for a file named MyHeader.h is as follows:

#ifndef _MY_HEADER_H_

#define _MY_HEADER_H_

// actual content of header goes here

#endif _MY_HEADER_H_

The first time this file is included, the _MY_HEADER_H_ symbol will not be defined, so the compiler defines it and then includes the content in your source file. The next time the file is included, the compiler will see that it has already defined _MY_HEADER_H_, thus everything up to the corresponding #endif _MY_HEADER_H_ at the end of the file is completely ignored.

The definition must also appear just once in your source. Although the definition is itself a declaration, the compiler knows that if a function declaration has not been defined then it must be a prototype or forward declaration, and will use the prototype to match the declaration with its definition. But once defined, it cannot subsequently be redefined.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A function prototype is a declaration of a function that omits the function body. The prototype gives the minimum amount of information required to use the function, including its name, return type and the number and type of its arguments (the function's arity). The argument names are not important and may be omitted from the prototype.

The function prototype allows you to separate the function's interface from its implementation. Typically they are used as forward declarations, however by placing them in header files (which can be included as and when required) you gain the benefit of forward declarations and the ability to specify an interface for those prototypes. Although there can only ever be one definition per prototype per translation unit, you can also provide alternate interfaces to those prototypes. For instance, you might have one interface for UNICODE builds and another for non-UNICODE builds, and the appropriate interface will be compiled according to whether the UNICODE compile-time directive is defined or not.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A function prototype is a declaration of a function as opposed to its definition. While the definition of a function tells you what the function does, the prototype tells you how to interface with the function. The prototype tells you the function's return type, its name, arity and argument types.

Prototypes are often used as forward declarations for functions that will be defined elsewhere in your program. However, the definition of a function is also a declaration. Where the two are separate, the declarations must match and there can only be one definition per prototype. The only real difference is that the prototype does not require formal argument names but the definition does (the definition also requires an implementation, of course).

Forward declarations are particularly important in C++, where type safety is more stringent than with C. The main advantage of forward declarations is that the compiler has all the information it needs to ensure all your function calls are type safe as and when they are encountered. Without prototypes, the compiler would have to cache each function call until the definition is encountered, and only then can it identify any problems with those calls. When compile times can easily take several minutes (or hours!), the quicker your compiler identifies errors the quicker you can fix them. Caching declarations (whether defined or not) is not only quicker than caching function calls, it uses far less memory.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

It is to inform the c-compiler that we are using a function with the specified name, type of arguments(parameters) it can accept, and return type.

Ex:

void function (int a, int b)

| | |

Return Function parameter

type name type

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Nowadays it is the same thing. Long long time ago the following form was also acceptable as function declaration:

FILE *fopen ();

Currently it is:

FILE *from (const char *, const char *);

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

//function prototype int myFunction(char a); //function definition int myFunction(char a) { ...//your code }

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a function prototype in C and C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What you declare in the function prototype in c?

yes


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.


Function prototype in c?

Yes. Examples can be found in stdio.h


What are the building function in c plus plus?

There is no such term as "building function" in C++.


What is difference function prototype and function define in turbo c?

In C, a function prototype is a declaration to the compiler that a certain function exists, without a full definition. This allows other functions to call that function. The linker will later make sure that a function definition actually exists (perhaps somewhere else), and will turn the call to the function to a call to the correct function.


Why Clearscreen in c language is used?

'Clearscreen' is not used in C language. TurboC has a clrscr function (prototype in conio.h).


What is a reference variable in c plus plus?

A reference variable in C++ is a formal parameter of a function call that automatically dereferences itself, as if it were a pointer, into a reference to the original value in the calling routine. You declare the reference type in the function declaration and prototype, but the compiler automatically adds the reference (&) operator on call, and the dereference (*) operator on use.


Explain about header files used in c language?

list of header files in c and function prototype associated with each file


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


What are its program components?

the main() function,the#include Directive, the variable definition. function prototype, program statements, the function definition,program comments and braces are the components of program in C++