CS503 Lec6

  • Upload
    gaascr

  • View
    232

  • Download
    0

Embed Size (px)

Citation preview

  • 8/16/2019 CS503 Lec6

    1/13

    Lecture 6:

    Functions

    So far, you used only the function main; the programming instructions are packed into it. This is good only for short programs.

    For large programs, it is not practical to put the entire programming

    instructions into one function

    You must learn how to break the problem into manageable pieces (functions).

    What is meant by a function?

    EX:When an automobile is manufactured, it is put together from previously

    manufactured parts.

    Some parts are made by the company itself; others, by different companies.

    Functions are like building blocks.They let you divide complicated programs into

    manageable pieces.

    Functions have other advantages:

    • While working on one function, you can focus on just that part of the program

    and construct it, debug it, and perfect it.

    • Different people can work on different functions in the same time.

    • If a function is needed in more than one place in a program or in different

     programs, you can write it once and use it many times.

    • Using functions greatly enhances the program’s readability because it reduces

    the complexity of the function main.

  • 8/16/2019 CS503 Lec6

    2/13

  • 8/16/2019 CS503 Lec6

    3/13

    1) Predefined Functions (Cont.)

    Examples:

    b) The square root function, sqrt(x), calculates the nonnegative square root of x

    for x >= 0.0

    • sqrt(2.25)

    = 1.5

    • sqrt(16)

    = 4.0

    The function sqrt is of type double and has only one parameter.

    1) Predefined Functions (Cont.)

    Predefined functions are organized into separate libraries.

    EX:

    the header file iostream contains I/O functions, and

    the header file cmath contains math functions

    To use predefined functions in a program, you must include the header file

    that contains the function’s specification via the include statement.

    For example, to use the function pow, the program must include:

    #include

  • 8/16/2019 CS503 Lec6

    4/13

    EX:

    #include #include //to use pow and abs functions

    using namespace std;

    int main()

    {

    int x;

    double u, v;

    u = 4.2;

    v = 3.0;

    cout

  • 8/16/2019 CS503 Lec6

    5/13

    2. User-Defined Functions

    are classified into two categories:

    a. Value-returning functions:

    Functions that have a return type.

    These functions return a value of a specific data type using return statement

     b. Void functions:

    Functions that do not have a return type.

    These functions do not use a return statement to return a value.

    a. Value-returning functions

    Because the value returned by a value-returning function is unique ,قيمة و حد

    you can use this value in one of three ways:

    • Save the value for further calculation (in an assignment statement).

    • Use the value in some calculation (as a parameter in a function call).

    • Print the value (in an output statement).

    => That is, a value-returning function is used (called) in an expression.

  • 8/16/2019 CS503 Lec6

    6/13

    The syntax of a value-returning function is:

    The syntax to call a value-returning function is:

    A function’s formal parameter list can be empty but the parentheses are still needed.

    OR: a call to a value-returning function with an empty formal parameter list is:

    Notes:

    In a function call, the number of actual parameters, must match the formal

     parameters in the given order.

    That is, actual and formal parameters have a one-to-one correspondence.

    A function call in a program causes the body of the called function to execute.

  • 8/16/2019 CS503 Lec6

    7/13

    return Statement

    Once a value-returning function computes the value, the function returns this value via

    the return statement.

    In other words, it passes this value outside of the function via the return statement.

    The return statement has the following syntax:

    Notes:

    - The data type of the value that expr computes must match the function type.

    - When a return statement executes in a function, the function immediately terminates

    and the control goes back to the caller.

    - The function call statement is replaced by the value returned by the return statement.

    - When a return statement executes in the function main, the program terminates.

    - The return statement can appear anywhere in the function. Once it executes, all

    subsequent statements are skipped. Return the value as soon as it is computed.

    return is a reserved word.

    EX1:

    Write a function that determines the largest of two numbers.

     Note that this function requires that you use an additional variable max

    (called a local declaration, in which max is a variable local to this function.

  • 8/16/2019 CS503 Lec6

    8/13

  • 8/16/2019 CS503 Lec6

    9/13

    EX1 (Cont.)

    Other solutions without declaring any local variable:

    Because the execution of a return statement in a function terminates the function, the

     preceding function larger can also be written (without else) as:

    How to use a function in a program?You can call a function in different ways as follows:

    double one = 13.00; //or receive it from the user

    double two = 36.53;

    double maxNum;

    cout

  • 8/16/2019 CS503 Lec6

    10/13

    How to use a function in a program?You cancall a function in differentways as follows (Cont.)

    Once a function is written, you can use it anywhere in the program.

    EX:

    The function compareThree calls the preceding  لس بق

    function larger:

    First, the expression larger(y, z) is evaluated; that is, the inner لد خلي call

    executes first, which gives the larger of y and z i.e., either y or z.

     Next, the outerلخ رج

    call determines the larger of x and either y or z.

    Finally, the return statement returns the largest number.

    EX2:

    The header of abs function is:

    int abs( int number)

    The definition of abs function is:

    int abs( int number)

    {

    if (number < 0)

    number = - number;

    return number;

    }

  • 8/16/2019 CS503 Lec6

    11/13

    The order in which user-defined functions should appear in a program

    Do you place the function larger before or after the function main?

    Following the rule that you must declare an identifier before you can use it and

    knowing that the function main uses the identifier larger, logically you must

     place larger before main.

    In reality, C++ programmers customarily follow the following order:

    1. Place function prototypes before the function main

    Then

    2. The function main

    Then

    3. The definition of all other user-defined functions.

    Function Prototype

    The general syntax of the function prototype of a value-returning function is:

    EX:

    double larger( double x, double y); OR  double larger( double, double);

    ends with a semicolon

  • 8/16/2019 CS503 Lec6

    12/13

    The entire program

    Program uses functions larger, compareThree, and main to determine the larger/largest of 2 or 3 numbers:

    #include

    using namespace std;

    double larger( double x, double y); //function prototype

    double compareThree( double x, double y, double z);

    int main(){

    double one, two;

    cout two;

    cout

  • 8/16/2019 CS503 Lec6

    13/13

    Questions Samples:

    1. Consider the following function definition:

    double func(double x, int y, string name)

    { //function body }

    Which of the following is the correct function prototype?

    i. double func();ii. double func(double, int, string);

    iii. double func(double x, int y, string name)

    iv. func(double x, int y, string name);///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    2. Consider the following statements:

    double num1, num2, num3;

    int int1, int2, int3, value;

    num1 = 5.0; num2 = 6.0; num3 = 3.0;

    int1 = 4; int2 = 7; int3 = 8;

    and the function prototype:

    double cube (double a, double b, double c);

    Which of the following statements are valid?

    a. value = cube (num1, 15.0, num3); b. cout