Structure Function in C Programming


Three methods by which the value of a structure can be transferred from one to another.Three methods by which the value of a structure can be transferred from one to another.

1. 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 is the most simple method and becomes unmanageable and inefficient when the structure size is large.

2. The second method involves passing of a copy of the entire structure to the called function. since the function is working on a copy of the structure ,any changes to structure members within the function are not reflected in the original structure. All compilers may not support this method of passing the entire structure as a parameter.

3. The third approach, 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 . The function can access indirectly the entire structure and work on it. This is similar to the way arrays are passed to function.

Syntax of calling function

Function _name(structure_variable_name);

Syntax of  called function

Function_name(struct type struct_name)
{
………
return (expression);
}

Remember Notes:

The called function must be declared for its type , it is expected to return. example  if it is returning a copy of the entire structure, then it must be declared as struct with an appropriate tag name.

The structure variable used as the actual argument and the corresponding formal argument in the called function must be of the same struct type.

The return statement is necessary only when the function is returning some data back to the calling function . The expression may be any simple variable or structure variable or an expression using simple variables.

when a function returns a structure ,it must be assigned to a structure of  identical type in the calling function.

The called functions must be declared in the calling function appropriately.

Example program

create structure Fruit which holds data of fruit name,price and quantity . enter value for incremented of price and quantity and display Updated Value of Fruit using Structure with Function .

Source Code of Structure with Function in C Programming

Output of Structure and function in C programming

Fruit Detail Before Updated
Name : Apple
Price : 23.50
Quantity : 15

Enter value of Price Increment and Quantity Increment
10
15

Fruit Detail After Updated
Name : Apple
Price : 33.50
Quantity : 30

Value of the item = 1005.00

 

(Visited 57 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *


CAPTCHA Image
Reload Image