Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Functions ANSI-C.

Similar presentations


Presentation on theme: "More on Functions ANSI-C."— Presentation transcript:

1 More on Functions ANSI-C

2 Function Type and Value
A function can return a value. Like all values in C, a function return value has a type. The function is said to have the type of its returned value. /* computes net price of a retail item */ double net ( double gross, int disc, int taxPer){ double discounted = gross – gross*disc/100.0; double tax = discounted*taxPer/100.0; return discounted + tax; } Examples of call: double finalPrice = net( 5.15, 10, 8);

3 Attributes of a variable
name Type Value Location in memory Arguments and parameters must match in: Number Order

4 Function examples And the call:
/* computes net price of a retail item */ double net ( double gross, int disc, int taxPer){ double discounted = gross – gross*disc/100.0; double tax = discounted*taxPer/100.0; return discounted + tax; } And the call: double finalPrice = net( 5.15, 10, 8); Argument values are used to initialize the parameters More specifically: For each parameter Corresponding argument is evaluated A copy of resulting value is given as value to the parameter After parameter initialization, no more communication between argument and parameter exists. This is called: pass-by-value

5 Assume the main declares
/* computes net price of a retail item */ double net ( double gross, int disc, int taxPer){ double discounted = gross – gross*disc/100.0; double tax = discounted*taxPer/100.0; taxPer = 0.0; gross = 0.0; return discounted + tax; } Assume the main declares double gross = 100.0; int discount = 20; int taxRate = 8.6; double total = net(gross, discount, taxRate); printf(“ gross>>%f , tax Rate>>%d”, gross, taxRate); It will print: gross>>100.0, tax Rate>>8.6 /* No CHANGE */

6 Functions which do not return values
The keyword void has two different rolls in this function definition. /* write separator line on output*/ void PrintBannerLines (void) { printf(“***************\n”); } indicates that the function does not return an output value. indicates that the function expects no parameters.

7 More examples void displayNet( double net){
printf(“The net price of the item is $%7.2f”, net); } void displayNet( double gross, int discRate, int taxRate, double net){ printf(“Item princing information\n”); printf(“ gross price>>$%7.2f\n”, gross); printf(“ discount rate>>$%d\n”, discRate); printf(“ tax rate>>$%d\n”, taxRate); printf(“ net price>>$%7.2f\n\n”, net);


Download ppt "More on Functions ANSI-C."

Similar presentations


Ads by Google