Structures and Functions
The main philosophy of C language is the use of functions.C supports the passing of structure values as arguments to function. In this ,the values of a structure can be transferred from one function to another by using 3 methods.The first method is to pass each member of the structure asan actual argument of the function call. The actual arguments are then treated independently like ordinary variables.This method is inefficient when the structure size is large.The second method involves passing of copy of entire structure to the called function.Here ,the function is working
on a copy of the structure ,so any changes to structure member within the function are not reflected in the original structure (in the calling function).It is,therefore ,necessary for the function to return the entire structure back to the calling function. But all compilers may not support this method of passing the entire structure as a
parameter.The third method employs a concept called pointers to pass
the structure as an argument. In this case,the address location of the structure is passed to the called function. This function can access the entire structure indirectly. This is smiler to the way ,arrays are passed to functions. This method is more efficient as compared to the second approach. The general format of sending a copy of a structure to the called function is: function name(structure variable name) The called function takes the following form: data-type function name(st-name)
struct-type st-name
{
- - - - -
--------
return (expression);
}
1. The called function must be declared for its type,appropriate to the data type it is expected to For example, if it is returning a copy of the entire structure,then it must be declared as struct with an appropriate tag name.
2.The structure variable used as the actual argument and the corresponding formal argument in the called function must be of the same struct type.
3.The return statement is necessary only if the function is returning some data .The expression may be any simple variable or structure variable or an expression using simple variables.
4.When a function returns a structure it must be assigned to a structure of identical type in the calling function.
5.The called function must be declared in the calling function for its type if it is placed after the calling function.
No comments:
Post a Comment