Skip to main content

what is Function Declarations?


A function declaration establishes the name of the function and the number and types of its parameters. A function declaration consists of a return type, a name, and a parameter list. In addition, a function declaration may optionally specify the function's linkage. In C++, the declaration can also specify an exception specification, a const-qualification, or a volatile-qualification.

A declaration informs the compiler of the format and existence of a function prior to its use. A function can be declared several times in a program, provided that all the declarations agree. Implicit declaration of functions is not allowed: every function must be explicitly declared before it can be called. In C89, if a function is called without an explicit prototype, the compiler provides an implicit declaration. The compiler checks for mismatches between the parameters of a function call and those in the function declaration. The compiler also uses the declaration for argument type checking and argument conversions.

A function definition contains a function declaration and the body of the function. A function can only have one definition.

Declarations are typically placed in header files, while function definitions appear in source files.

>>-+--------+--+----------------+--function_name---------------->
   +-extern-+  '-type_specifier-'
   '-static-'

      .-,-------------.
      V               |
>--(----+-----------+-+--+--------+--)--+----------+------------>
        '-parameter-'    '-,--...-'     +-const----+
                                        '-volatile-'

>--+-------------------------+--;------------------------------><
   '-exception_specification-'

A function argument is an expression that you use within the parentheses of a function call. A function parameter is an object or reference declared within the parenthesis of a function declaration or definition. When you call a function, the arguments are evaluated, and each parameter is initialized with the value of the corresponding argument. The semantics of argument passing are identical to those of assignment.

Some declarations do not name the parameters within the parameter lists; the declarations simply specify the types of parameters and the return values. This is called prototyping A function prototype consists of the function return type, the name of the function, and the parameter list. The following example demonstrates this:

int func(int,long);
Function prototypes are required for compatibility between C and C++. The nonprototype form of a function that has an empty parameter list means that the function takes an unknown number of parameters in C, whereas in C++, it means that it takes no parameters.

Function Return Type

You can define a function to return any type of value, except an array type or a function type. These exclusions must be handled by returning a pointer to the array or function. A function may return a pointer to function, or a pointer to the first element of an array, but it may not return a value that has a type of array or function. To indicate that the function does not return a value, declare it with a return type of void.

The return type of a function must be void if the return statement does not contain an expression. However, if an expression does appear in the return statement, then the return type of the function cannot be void: the compiler converts the return expression as if by assignment to the return type of the function.

 The return statement of a function does not require an expression if the function has the return type void, or is a constructor or destructor. In these cases, the function does not return a value. When a function returns a value, the return statement must contain an expression, which is returned to the caller of the function after being implicitly converted to the return type of the function in which it appears.

A function cannot be declared as returning a data object having a volatile or const type, but it can return a pointer to a volatile or const object.

Limitations When Declaring Functions in C++

Every function declaration must specify a return type.

Only member functions may have const or volatile specifiers after the parenthesized parameter list.

The exception_specification limits the function from throwing only a specified list of exceptions.

Other Limitations When Declaring a Function

The ellipsis (...) may be the only argument in C++. In this case, the comma is not required. In C, you cannot have an ellipsis as the only argument.

Types cannot be defined in return or argument types. For example, the C++ compiler allows the following declaration of print():

struct X { int i; };

void print(X x);
The C compiler allows the following declaration:

struct X { int i; };

void print(struct X x);

Neither the C nor C++ compiler allows the following declaration of the same function:

void print(struct X { int i; } x);   //error

This example attempts to declare a function print() that takes an object x of class X as its argument. However, the class definition is not allowed within the argument list.

In another example, the C++ compiler will allow the following declaration of counter():

enum count {one, two, three};
count counter();
Similarly, the C compiler allows the following declaration:

enum count {one, two, three};

enum count counter();

Neither compiler allows the following declaration of the same function:

enum count{one, two, three} counter();   //error

In the attempt to declare counter(), the enumeration type definition cannot appear in the return type of the function declaration.


Popular posts from this blog

C++ Program to find the sum, difference, product and quotient of two integers

#include <iostream.h> #include <conio.h> void main() {   clrscr();   int x = 10;   int y = 2;   int sum, difference, product, quotient;   sum = x + y;   difference = x - y;   product = x * y;   quotient = x / y;   cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl;   cout << "The difference of " << x << " & " << "y <<  is " << difference << "." << endl;   cout << "The product of " << x << " & " << y << " is " << product << "." << endl;   cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl;   getch(); }

Program of virtual piano

//////////////Tested And Created By C++/////////////////////////////// #include<stdio.h> #include<dos.h> #include<conio.h> #include<stdlib.h> #define SHOW 1 #define HIDE 2 union REGS input,output; class piano {  public:int BIGKEY,MIDKEY,back,border;     piano()//init constructor     {         BIGKEY=15;         MIDKEY=1;         back=7;         border=15;     } }color; void drawpiano(int x,int y); int check_xy(int x,int y); void BOX(int c,int r,int c1,int r1,int col); int initmouse(); void setupscreen(); void pointer(int on); void restrictmouse(int x1,int y1,int x2,int y2); void check_keys(int x,int y); void getmouse(int *button,int *x,int *y); float freq[7] = {130.81, 146.83, 164.81, 174.61,196, 220, 246.94 } ; int n=0,a=4,backcolor=2,exitcode=1; void showbar(int t) {  if(t>65) t=65;  if(t<1) t=1;  textcolor(15);  for(int q=0;q<=t;t++)  {     gotoxy(3+q,4);     cprintf("Û");  } } void main() {  int

Putimage function in c

putimage function outputs a bit image onto the screen. Declaration:- void putimage(int left, int top, void *ptr, int op); putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left, top). ptr points to the area in memory where the source image is stored. The op argument specifies a operator that controls how the color for each destination pixel on screen is computed, based on pixel already on screen and the corresponding source pixel in memory. c smiling face animation This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily. C programming code #include<graphics.h> #include<conio.h> #include<stdlib.h>   main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p;   initgraph(&gd,&gm,"C:\\TC\\BGI");   setcolor(YELLOW);