Sunday, 21 January 2018

Structures and Functions


Structures and Functions

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 as
an 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.

Related Posts:

  • GO TO Statement GO TO Statement This Keyword is used when we want to transfer the control to some point in the program which is not in the normal, step by step sequence of a program.  It takes control from one place to another un… Read More
  • Parameter Passing Technique Parameter Passing Technique The parameter passing mechanism refers to the actions taken regarding the parameters when a function is invoked. There are two parameter passing techniques available in C. They are: 1) Call B… Read More
  • Introduction To Functions Introduction To Functions Functions are building blocks of C. The general form of C (User-defined) function is:       Return-type-function-name(parameter list)       parameter declaration &… Read More
  • STANDARD LIBRARY FUNCTIONS STANDARD LIBRARY FUNCTIONS Arithmetic Functions: abs --> Returns the absolute value of an integer. cos --> Calculates cosine. cosh --> Calculates hyperbolic cosine. exp --> Raises the exponential e to the xt… Read More
  • Function Declaration and prototype Function Declaration and prototype It is one of the most important feature of ANSI C.A function prototype tells the compiler the type of data returned by the function, the number of arguments the function expects to r… Read More

0 comments:

Post a Comment