C Functions

FUNCTION:

                        Function is defined as self contained block of code to perform a task.

Functions can be categorized to system-defined functions and user-defined functions.

System defined functions: These are functions that are supported along with the programming language. The block of instructions of these functions and the task to be performed is predefined. For e.g. clrscr( ), printf ( ), scanf( ), pow( ), sqrt( ) etc.

User defined functions: These are functions that are not supported along with the programming language. The block of instructions of these functions and the task to be performed is defined by the programmer.

The following are the advantages of functions:

·Reusability of code i.e. block of code defined can be used when required.

·Facilitates Modular Programming i.e. breaking the complex problem into small manageable sub-problems called as modules and also makes the process of debugging easier.
·Reduces the length of the program

Note: The block of instructions defined in the function gets executed only when a function call is invoked.
Where function call is the reference made in the calling function by specifying the name of the function along with arguments if any exists and should be terminated with semicolon.

Where calling function is a function that comprises of references to the name(s) of functions along with arguments if any exists.

for e.g.

main( )   /* calling function */
{
      clrscr( );  /* function call  */
}

Defining User defined functions:
To understand user-defined functions one should understand the following elements:
· Function definition
· Function declaration
· Function call

Function Definition:

The function definition consists of two parts namely
1. Function header
2. Function body

The general format of the function definition is shown below:

                                    


 Return type  function-name(arguments)
        Argument declaration;
        {
Local variable declaration;
Executable statement1;
Executable statement 2;
Return statement;
                    }
Function Header:
The function header comprises of three parts namely
1. Return type
2. Function name
3. arguments

Return type: This part of the function header specifies the type of data (primitive/user-defined) that the function will return.
Function Name: This part of the function header takes a valid name to identify the function and this name should follow the rules for naming a user defined identifier.
Arguments: This part of the function header receives the values from the actual arguments of the function call. The number and type of arguments from function call should match with the arguments in the function definition. The arguments in the function definition can be declared for its type in the next line. The arguments in the function definition are called as formal arguments.

Function body:
The function body comprises of the following parts enclosed in flower Braces:
1. Local variable declaration
2. Executable statements
3. Return statement
Local variable declaration:  Variables that are needed in that function are declared for its type.
Executable Statements: These are statements that are needed in the function to assign a value or to read a value or to print the value or to perform some computations.
Return statement: Every function body ends with return statement. This statement is used to return a value to the calling function. If the function does not return any value then the return statement is written as
Return; or return 0;
If the function returns any value then the return statement is written as Return value; or return expression;
The function definition can be done either above the main function or after the main function.

Example of function definition:

float mul(float x, float y)
{
float result;
result=x*y;
return result;
}

In the above example in the function header the return type is float and the name of the function is mul and the arguments are x and y which are declared of type float. In the function body, result is a variable that is needed in that function and is declared locally for its type. Result=x*y is the executable statement and the return statement returns the value available in the variable result.

Function Declaration: Declaring a function also called as function prototyping is used to inform the compiler about the specification of the function that will be defined and the syntax for invoking the function call such as
Ø Return type of the function
Ø Function name
Ø Type, name, order of arguments that are used in the function (Here arguments are optional)

The general format or syntax of function declaration is given below:

return_type function_name(type param_name1,type param_name2,……type param_nameN);

Where parameters/arguments specify the type of data (primitive/user-defined) and each of these arguments should be separated with comma. The function declaration is done above the main function.


Example of function declaration:

float mul(float , float);

In the above example the return type is float and the name of the function is mul and the arguments that should be passed to the function definition through function call should be of type float.

Function call: The function definition is invoked by referring the function name along with arguments if any exists. The arguments in the function call if exists will take data or variables declared for its type matching to the function declaration and definition.
The arguments in the function are called as actual arguments.

The function call should be made always within another function.

The general format for function call which returns value is given below:

Variable=functionname(arguments);
                         Or
printf(“format string”,functionname(arguments));

Where arguments may or may not exist.

The general format for function call which does not return value is given below:

Functionname(arguments);

Where arguments may or may not exist.

Example of function calls which returns values:

main( )
{
                float y;
                y = mul(23.4,12.5);   /* function call */
                printf(“product of two numbers is %f\n”,y);
 }

Example of function call which does not return value:

main( )
{
                mul( );   /* function call */
 }

/* complete program showing the usage of function declaration, function call and definition */

#include <stdio.h>

float mul(float,float);   /* function declaration */

main()
{
                float y;
                y=mul(23.4,12.5);   /* function call */
                printf(“product of two numbers is %f\n”,y);
  }

/* function definition */
float mul(float x, float y) /* argument declaration */
{
        float result;          /* local variable declaration */
        result=x*y;         /* executable statement */
        return result;   /* returns the computed value in result to the calling function */
}

Comments:
In the above program a function is declared by name mul that takes two arguments of type float and returns a value of type float which is advance intimation to the compiler about the function that will be used in this program.
In the main function, function call is invoked by passing 23.4 and 12.5 as arguments of type float and is assigned to y as the function returns a value of type float. (The function call should be assigned to a variable if it returns a value and that variable should be declared for a type matching to the return type of that function specified in the function declaration.)
The function call leads to assigning the values 23.4 and 12.5 to arguments in the function definition i.e. x and y and these arguments should match with the data type of the function declaration. In the function body the assignment statement is executed by computing the product of x and y and assign the value to the variable result which is locally declared in the function body and the return statement transfers the value of the variable result to the calling function and assigns the value to y.

Input Arguments or Actual Arguments:
These are arguments that are passed from the function call to the function definition.

Formal Arguments:
These are arguments in the function definition that receive the value or arguments from the function call.

Output Arguments:
These are arguments that return value to the calling function.

Categories of Functions (Based on the arguments and return type)
Functions fall into four categories as listed below:
  1. Function with Arguments and with return value.
  2. Function with Arguments and no return value.
  3. Function with no arguments and with return value.
  4. Function with  no arguments and no return value.

Below we will see the comparative study of each of these categories of functions:

Function with Arguments and with return value
Function with arguments and no return value
Function with no arguments and with return value
Function with no arguments and no return value
Description:

Function call:
Function call is made by passing arguments
The function call is assigned to a variable that matches to the return type of the function definition or used in the printf function with format specifier matching to the return type of the function definition.
Function Definition:
The number of arguments in the function definition should match with the number of arguments passed from the function call.
The return value should match with the return type of the function definition.

Below shows an example of how the programming instruction changes for this category of function.

#include <stdio.h>

/* function declaration*/
int printvalue(int);

main( )
{

int value;
clrscr( );
/* function call */
value=printvalue(10);
printf(“%d”,value);
}

/* function definition */
int printvalue(int a)
{
return a;
}
Description:

Function call:
Function call is made by passing arguments
Function call is not assigned to any variable or not used in printf function.
Function Definition:
The number of arguments in the function definition should match with the number of arguments passed from the function call.
The return statement will not return any value.

Below shows an example of how the programming instruction changes for this category of function.


#include <stdio.h>

/* function declaration*/
void printvalue(int);

main()
{

clrscr();
/* function call */
printvalue(10);
}



/* function definition */
void printvalue(int a)
{
printf(“%d”,a);
return 0;
}

Description:

Function call:
Function call is made by passing no arguments
The function call is assigned to a variable that matches to the return type of the function definition or used in the printf function with format specifier matching to the return type of the function definition.

Function Definition:
The function definition will not receive any arguments.

The return value should match with the return type of the function definition.
Below shows an example of how the programming instruction changes for this category of function.

#include <stdio.h>

/* function declaration*/
int printvalue(void);

main( x)
{

int value;
clrscr();
/* function call */
value=printvalue();
printf(“%d”,value);
}

/* function definition */
int printvalue(void)
{
int a;
a=10;
return a;
}
Description:

Function call:
Function call is made by passing no arguments
Function call is not assigned to any variable or not used in printf function.
Function Definition:
The function definition will not receive any arguments.
The return statement will not return any value.


Below shows an example of how the programming instruction changes for this category of function.


#include <stdio.h>

/* function declaration*/
void printvalue(void);

main()
{

clrscr();
/* function call */
printvalue();
}


/* function definition */
void printvalue(void)
{
int a;
a=10;
printf(“%d”,a);
return 0;
}

أحدث أقدم