answersLogoWhite

0

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

15y ago

What else can I help you with?

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'.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.