05review(functionbasics)

Upload: lau-weng-loon

Post on 04-Jun-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 05review(FunctionBasics)

    1/2

    Chapter 5 Functions

    1. At least three benefits: (1) Reuse code; (2) Reduce complexity; (3) Easy to maintain.ee the ections !.2 and !.3 on ho" to declare and in#o$e functions. What is the

    subtle difference between defining a function anddeclaring a variable? A declaration usually involves

    allocating memory to store a variable, but a definition

    doesnt.

    2. int

    3. %es. return (num1 & num2) ' num1 : num2;

    !. rue: a call to a function "ith a #oid return type is al"ays a statement itself. alse: a call to a function "ith a non#oid return type is al"ays a component of an

    expression.

    *. A syntax error occurs if a return statement does not appear in a non+#oid function.%ou can ha#e a return statement in a #oid function, "hich simply exits thefunction. -ut a return statement cannot return a #alue such as return x y in a#oid function.

    /. ee ection *.2.

    0. omputin a sales commission i#en the sales amount and the commission rate

    double etommission(double salesAmount, double commissionRate)

    rintin a calendar for a month#oid printalendar(int month, int year)

    omputin a s4uare rootdouble s4rt(double #alue)

    estin "hether a number is e#en and return true if it isbool isE#en(int #alue)

    rintin a character for a specified number of times

    #oid printhar(char ch, int times)

    Computing the monthly payment, given the loan amount,

    number of years, and annual interest rate.

    double monthlyPayment(double loan, int numberOfYears,

    double annualInterestRate)

    indin the correspondin uppercase letter i#en a lo"ercase letter.

  • 8/13/2019 05review(FunctionBasics)

    2/2

    char et5pperase(char letter)

    6. 7ine /: function1 is not defined correctly. 8t does not ha#e a return type or #oid. 7ine /: type int should be declared for parameter m.

    7ine 1: the function does not ha#e a return statement.

    9. "o functions "ith the same name, defined in the same proram, is called functiono#erloadin. 8t is fine to ha#e same function name, but different parameter types.%ou cannot o#erload functions based on return type.

    1. hese t"o functions ha#e the same sinature p(int).

    11. rue

    12. (a) rand() 3! (** < 3!)(b) rand() 999(c) rand() 2/ =a>

    13.

    It would cause a compile error indicating ambiguous call.

    The reason is that these functions are overloaded with

    float, double, and long double parameter. The compiler

    cannot determine which function to match the call for an

    int argument.

    1!.

    Please run the code to find the answer.

    15.

    To test whether a character c is a digit, use

    isdigit(c).

    To test whether a character c is a letter, use

    isalpha(c).

    To test whether a character c is a lowercase

    letter, use islower(c).

    To test whether a character c is a letter or a

    digit, use isalnum(c).

    1/. 5se tolo"er(c) to con#ert c to lo"ercase and use toupper(c) to con#ert c touppercase.