Friday, October 18, 2019

Function

FUNCTION

C enables programmer to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function in the program is supposed to perform a well-defined task. Therefore, program code of one function is completely insulated from the other function.
Every function interfaces to the outside world in terms of how information is transferred to it and how result generated by the function are transmitted back from it. This interface is basically specified by function name.
For example, 
main( )
{
...............
................
function 1( );
...............
return(0);
}
function 1( )
statement block;
}

In this example, the main () function call the function named func1(). Therefore, main() is the calling function and func1() is called function. The moment the compiler encounters the function call,instead of executing the next statement the control jumps to the statement that are part of the called function. After the called function is executed, the control is returned back to the calling program.
It is not necessary that the main() can call only one function, it can call as many function as it wants and as many time as it wants. Another point is that it is not only the main () that can call other functions. A function can call any other function.
For example,
main( )
{..........
..........
Func1( );
.........
return();
}
Func1( )
{.......
.......
Func2();
.......
........
return( );
}
Func2( )
{
..........
..........
}

Therefore,function is self contained block of statements which can execute and perform the specific task.
Following are the advantages of function approach : 
1- Increases the efficiency (time & space) of a program.
2- Normally function approach decreasing the complexity of the program and increases the understandability.
3- It is more easy to debug the program when we are using function approach.

WHY FUNCTIONS ARE NEEDED?

Dividing the program into to separate well defined function facilitate each function to written and tested separately. This simplifies the process of getting the total program to work.
Understanding,coding and multiple separate function are easier than doing the same for one huge function.
 If a big problem has to be developed without the use of any function other than the main() function ,then there will be countless lines in the main function and maintaining this program will be very difficult.
When a big problem is broken into comparatively smaller function, then different  programmers working on that project can divide the workload by writing different functions.
Like C libraries,programs can also write their functions and use them from different points in the main program or any other program that need its functionalities.

DEFINITION OF FUNCTION

Before using the function,the compiler must know about the number of parameters and type of parameter that the function expect to receive and the data type of the value that it will return to the calling program.
Hence, "function declaration" is a declaration statement that identifies a function with its name,a list of argument that it accept and the type of data it return.
The general format of declaring a function that accepts some argument and returns some value as a result can be given as :

return _datatype    function_name (data_type var.1,data_type var.2,...);

Here, function name is a valid name for the function. The function name is used to call it for execution in a program. Every function must have a different name that indicates a particular job that the function does.
return data_type specifies the data type of the value that will be returned to the calling function as a result of processing performed by the called function.

data_type variable 1, data_type  variable 2,...  is a list of variable of a specified data types. These variables are passed from calling function to call function. They are also known as parameter or argument that the call function accept to perform its task.
For example, 
int func (int, char, float);

1. After declaration of every function there should be a semicolon.
2. The function may be declared globally so that the declared function can be called from any point in the program.
3. A function cannot be declared within the body of another function.
4. If the function declaration does not specify any return type then by default, the function return an integer type.
5. A function having void as it return type cannot return any value.
6. Function having void as a parameter list cannot accept any value.

FUNCTION DEFINITION

main() function is defined the space is allocated for that function in the memory. A function comprises of two part:
1. Function header
2. Function body

The sentence of a function definition can be given as:
return_data_type  function_name (data_type var 1,data_type var 2,...)
{.........
............
Statement;
.........
return (statement) ;
}

Hence,  the function definition consist of a function header that indicates the function, followed by the body of the function containing the executable code for that function.
The number of argument and the order of argument in the function header must be same.
In the above syntax statement,
return_data_type function_name (data_type var 1,data_type var 2,...)

is the function header and the rest of the portion comprising of program statement within curly braces is the function body which contains the code to perform the specific task.
The function header is same as that of a function declaration. The only difference between the two is that the function header is not followed by a semicolon.

FUNCTION CALL

The function call statement invokes the function. When a function is invoked the compiler jumps to the call function to execute the statement that are the part of that function. Once  the called function is executed, the program control passes back to the calling function.
Function call statement has the following syntax:

function_name (variable 1,variable 2,...);

Function definition are often placed in a separate header file which can be included in other c source file that wish to use the function.
Function name and the number and the type of argument in the function call must be same as that given in the function declaration and function header of the function definition.
#). If by mistake the parameter passed to a function are more or less than what is specified to accept then the extra argument will be discarded and the unmatched argument will be initialised to some garbage value.
#). The parameter list must be separated with commas.

RETURN STATEMENT

The return statement is used to terminate the execution of a function and return to the calling function. When the return statement is encountered, the program execution resumes in the calling function at the point immediately following the function call.
Return statement may or may not return a value to the calling function. The syntax statement can be given as:
return (expression) ;
The value of the expression if present is returned to the calling function. However, in case, expression is omitted, the return value of the function is undefined.
A function that has a void as it return value they cannot return any value to the calling function.
An error is generated when the function does not return data of the specified type.

USING VARIABLE NUMBER OF ARGUMENT

Some function have a variable number of arguments and data types that cannot be known at the compile time.
Typical examples of such function include the printf ( ) and scanf ( )function. ANSI C offers a symbol called ellipses to handle such functions. The ellipses consists of three periods (...). It can be used as-
int func(char ch,...);
The function declaration statement given above states that function is a function that has an arbitrary number and type of argument. However, one must ensure that both the function declaration and function definition should use the ellipse symbol.

PASSING PARAMETER TO THE FUNCTION

When a function is called, the calling function may have to pass some value to the called function. 
Basically there are two ways in which argument of parameters can be passed to the called function. They include-
1. CALL BY VALUE

call by value in which values of the variable are passed by the calling function to the called  function.  The program that we have written so far all call the function using call by value method of passing parameter.
In call by value method, the call function creates new variable to store the value of the argument passed to it. Therefore, the called function uses a a copy of the actual parameter to perform it's intended task.
Value of the variable in the calling function remain unaffected when the argument passed using call by value technique.
Therefore, call by value method of passing argument to a function must be used only in two cases:

  1. When the call function does not modify the value of the actual parameter.
  2. When we want that the called function should only temporarily modify the value of the variable and not permanently so all the called function may modify the value of the variables, but this variable remain unchanged in the calling program.

2. CALL BY REFERENCE

Call by reference in which address of the variable are passed by the calling function to the called function.
In call by reference we declare the function parameter as reference rather than the normal variable. When this is done any changes made by the function to the argument it receives are visible to the calling program.
To indicate  that an argument is a password using call by reference an ampersand (&)sign is  placed after the type in the parameter list. This way, changes made to the parameter in the called function body will then be reflected in its value in the calling program.

ADVANTAGES

The advantages of using call by reference technique of passing argument are as follows:

  1. Since argument are not copied into new variables, it provides greater time and space efficiency.
  2. The function can change the values of the argument and the change is reflected in the caller.
  3. A function can return  only one value. In case we need to return multiple values, pass those arguments by reference so that modified values are visible in the calling function.

DISADVANTAGES

When an argument is a passed using call by address, it become difficult to tell whether that argument is meant for input, output or both.

No comments:

Post a Comment

Function

FUNCTION C enables programmer to break up a program into segments commonly known as functions, each of which can be written more or...