/    /  Concepts related to Functions

Concepts related to Functions


i) Function declaration or function prototype
ii) Function call
iii) Function definition (function declaratory and a function body)
iv) Elimination of function declaration
v) Passing arguments
vi) Return statement

 

i) Function Declaration (Or) Function Prototype:

A function declaration provides the subsequent information to the compiler
The name of the function.
The type of the value returned ( function type like., void, int, float, char).
The number and the type of arguments that has got to be supplied during a call to the function.
The semicolon “; ” is compulsory at the ending of the function declaration.

 

Syntax:

 

return_typefunction_name( );
(Or)
return_typefunction_name(dataype1, datatype2, …, datatype n );

 

Note:return_type specifies the data-type of the value within the return statement. If return_type is void, then no return statement is important, and if return_type is other than void like int, float, char,etc, then return statements is important and its compulsory.

 

Return type

Example 1: void hi( );
Example 2: void hi(int, int, float );
Example 3: int hi( );
Example 4: float hi(int, float, int );
Example 5: int swap (int * , int *);

 

ii) Function Call:

A function may be a hidden entity, which involves implementation when a call is made to the function. A call is specified by the function name followed by the values of the parameters enclosed within parentheses, terminated by a semicolon (;).

 

Syntax ( i ):

 

function_name( );

 

Syntax ( ii ):

 

function_name( parameter 1, parameter 2,…., parameter n);

 

Example 1: compute ( );
Example 2: compute( a, b, c);
Example 3: compute( 1, 2, 3);
Example 4: compute( &a, &b, &c);

 

Note: While calling the function, we should always not use any function type as void, int, float,char, etc.,

 

iii) Function Definition:

 

The function definition is analogous to the function declaration but doesn’t have a semicolon. the primary line of the function definition is named a function declaratory. This is followed by the function body. It is composed of the statements that structure the function, delimited by braces. The declaratory and declaration must use an equivalent function name, number of arguments, argument types, and therefore the return type. No function definition is allowed within any function definition. The function definition always should be outside the functions only.

 

Syntax ( i ):

 

return_typefunction_name( )
{ statement 1;
statement 2;
}

 

Syntax ( ii ):

 

return_typefunction_name( parameter1, parameter2,…., parametern)
{ statement 1;
statement 2;
}

 

Example 1:

 

void compute( )
{
printf(“Hi.. welcome to C programming language”);
printf(“Thank u ”);/* Here no return statement required because
function type is void*/
}

 

Example 2:

 

int compute( int a, int b)
{ int c;
c=a+b;
return c; }
/* here return statement is must and its type must be integer */

 

Void:

 

  • The void is an empty data type.
  • When the void is used as a function return type, it means that the function does not return any value.

 

Example 1:

 

void hello( int i) /* return type is void so we cannot return a value */
{ printf(“Hello ur no. is %d”,i);
}

 

Example 2:

 

int hello( int i) /* return type is not void so we can return a value */
{ printf(“Hello ur no. is %d”,i);
 return i; }

 

  • When void is used as a parameter in function declaration (Or) function definition, it means that the function is not sending any parameters from one function to other function.

 

Example 3:

 

void hi(void); can also be written like this format-> void hi( );
int hi(void); can also be written like this format->int hi( );

 

Example Program by using function calling, declaration and definition:

 

#include<stdio.h>
main( )
{
 void add( ); /* Function Declaration */
 add( ); /* Function Calling*/
}
void add( ) /* Function Definition */
{
 int c,a,b;
 printf("Enter the values of a & b:");
 scanf("%d %d",&a,&b);
 c=a+b;
 printf("Sum is=%d",c);
}

 

iv) Elimination of function declaration:

 

In C- Programming language user having a choice to define the functions anywhere in the program. If we defined the function at bottom of the main( ) function body then function declaration is necessary and if the function is defined before the main( ) body then the declaration is unnecessary. The following example illustrates this concept:

 

Example:

 

#include<stdio.h>
void add( ) /* Function Definition */
{
int c,a,b;
printf("Enter the values of a & b:");
scanf("%d %d",&a,&b);
c=a+b;
printf("Sum is=%d",c);
}
main( )
{
void add( ); /* Function Declaration is Optional &
It is not necessary*/
add( ); /* Function Calling*/
}