end of bigger function void main double number1number2 max variable declaration

End of bigger function void main double

This preview shows page 5 - 9 out of 10 pages.

} // end of bigger function void main( ) { double number1,number2, max; // variable declaration printf("Please input two numbers :"); scanf("%lf %lf", &number1, &number2); // input max = bigger(number1, number2); // function call printf("The max of %lf and %lf is = %lf",number1,number2, max); } // end of calling main function Sample Output: Example (iii): #include<stdio.h> float mul (float x,float y) // user defined function mul { return x*y; } // end of mul function float division (float number1, float number2) // user defined function division { float div; div=number1/number2; return div; } // end of division function void main ( ) // calling function { float n1, n2, product, d; printf("Please input value of n1 and n2 : ", &n1, &n2); scanf("%f %f", &n1, &n2); Page 5 of 10
Image of page 5
product = mul(n1, n2); // function call d = division(n1, n2); // function call printf("The product of %f and %f is = %f\n", n1, n2, product); printf("The division of %f and %f is = %f", n1, n2, d); return; } // end of main Sample Output: A short function is written below to calculate the net pay of an employee based on values for the wage rate and hours worked. These values are passed as arguments to function called calc_net_pay . The function then computes the net pay and returns the computed value to main . Example (iv): #include <stdio.h> float calc_net_pay (float wage, int hours) ; /* function prototype. Write this before main always */ void main (void) { float wage, net_pay ; int hours ; printf (“Enter the wage rate and number of hours worked\n”) ; /* Ask the user to enter input */ scanf (“%f %d”, &wage, &hours) ; /* read input */ net_pay = calc_net_pay (wage, hours) ; /* call the function by passing the input variables */ printf (“The net pay = %8.2f\n”, net_pay) ; /* print the result stored in net_pay returned by function */ } // end of main Page 6 of 10
Image of page 6
float calc_net_pay (float wage, int hours) /* function header same as function prototype given at the start of program */ { float g_pay, f_tax, soc_sec, net_pay ; /* function body */ const float FED_TAX = 0.28 ; /* constant declaration */ const float SOC_SEC = 0.055 ; g_pay = wage * hours ; f_tax = FED_TAX * g_pay ; soc_sec = SOC_SEC * g_pay ; net_pay = g_pay – (f_tax + soc_sec) ; return ( net_pay ) ; /* end of function by returning the result in net_pay */ } // end of main The calc_net_pay function is called from main and given two arguments wage and hours, which are usually the input variables . The name of the function, the number of arguments and the types of arguments should always be same while calling the function and while declaring the function in the header or as prototype . Once the function is called the control is transferred to the function and returns back to the main program with the return statement. In the above program if the function calc_net_pay does not return any value then it has to print the result inside the function itself. Also while calling the function just call the function along with its arguments without assigning the call to any variable. It is cleared from following Example : Example(v): #include <stdio.h> void calc_net_pay (float wage, int hours) ; /* function prototype. Write this before main always */ void main (void) { float wage, net_pay ; int hours ; Page 7 of 10
Image of page 7
printf (“Enter the wage rate and number of hours worked\n”) ;
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 10 pages?

  • Spring '10
  • zaman
  • Subroutine, Return statement, Control flow, User defined function

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes