Ask the user to enter input scanf f d wage hours read input calcnetpay wage

Ask the user to enter input scanf f d wage hours read

This preview shows page 8 - 10 out of 10 pages.

/* Ask the user to enter input */ scanf (“%f %d”, &wage, &hours) ; /* read input */ calc_net_pay (wage, hours) ; /* call the function by passing the input variables */ } // end of main void 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) ; printf (“The net pay = %8.2f\n”, net_pay) ; /* print the result stored in net_pay */ } // end of user defined function calc_net_pay Argument List Correspondence Rules : The number of actual arguments/parameter, the order of actual argument and type of actual arguments used in a call to a function must be the same as the number of formal parameters, the order of formal parameters and type of formal parameters/arguments listed in the function definition. Use of return Statement in Function : An return statement can take one of the following form : return; Or return( expression ); The first , the ‘plain’ return does not return any value, just it returns control to calling function. e.g if(error) Page 8 of 10
Image of page 8
return; The second form of return with an expression returns the value of the expression. e.g int mul(int x, int y) { int p; p=x*y; return(p); } In this above last two statements can be combined into one statement as follows : return (x*y); Can a function have more than one return Statements ? : Yes. A function may have more than one return statements . This situation arises when the value returned is based on certain conditions: e.g if(x<=0) return(0); else return(1); Note : All functions by default return int type data. But we can make it any type. Exercise: Write a menu driven program , which has the following options: 1. Find Factorial of a Number. 2. Check if Number is Prime or not. 3. Check if Number is Even. 4. Exit It must execute appropriate choice entered by user. You need to write, in addition to the main function, three other functions: factorial that returns a factorial of its parameter. The return type should be long. Page 9 of 10
Image of page 9
prime which receives an integer number and returns 1 if it is prime, 0 otherwise. A prime number is a number greater than 1, with no divisor except 1 and itself. even which should return 1 if its parameter is even, 0 otherwise. Page 10 of 10
Image of page 10

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

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

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