+ All Categories
Home > Documents > COEN 244 - 6 - Functions

COEN 244 - 6 - Functions

Date post: 19-Feb-2018
Category:
Upload: maxime-pauloin
View: 227 times
Download: 0 times
Share this document with a friend

of 22

Transcript
  • 7/23/2019 COEN 244 - 6 - Functions

    1/22

    2008 Pearson Education, Inc. All rights reserved.

    1

    6

    Functions

  • 7/23/2019 COEN 244 - 6 - Functions

    2/22

    Function Components

    A function consists of function

    prototype, function header and

    function body.

    Functions often require more than one

    piece of information to perform their

    tasks, which is passed by parameters.

    Parameters are specified in both the

    function prototype and the function

    header.

  • 7/23/2019 COEN 244 - 6 - Functions

    3/22

    Function prototype

    Also called a function declaration

    Indicates to the compiler:

    ame of the function

    !ype of data returned by the function

    Parameters the function e"pects to recei#e

    !ypes of those parameters

    $rder of those parameters

    Example int max!int, int, int"

  • 7/23/2019 COEN 244 - 6 - Functions

    4/22

    Function %eader

    &ame as function prototype but it also

    includes the name of the parameters.

    '"ample: int ma"(int ", int y, int )*

    parameter names: ", y, )

  • 7/23/2019 COEN 244 - 6 - Functions

    5/22

    Function +ody

    Function body beins with a left brace

    followin the function header and ends

    with a riht brace.

    '": int ma"(int ", int y, int )*

    - int ma"alue / "0

    if ( y 1 ma"alue* ma"alue / y0 if( ) 1 ma"alue* ma"alue / )0

    return ma"alue 2

  • 7/23/2019 COEN 244 - 6 - Functions

    6/22

    Function '"ecution

    A function is in#oked by a function call

    Followin a call, statements within thefunction body will e"ecute

    '": ma" (rade3, rade4, rade5* arumets : rade3, rade4, rade5

  • 7/23/2019 COEN 244 - 6 - Functions

    7/22

    !hree ways to return control to the

    callin statement

    If the function does not return a result

    ( return type is #oid*:

    Proram flow reaches the functionendin riht

    brace or Proram e"ecutes the statement return;

    If the function does return a result:

    Proram e"ecutes the statement,

    returnexpression; expressionis e#aluated and its #alue is returned to

    the caller

  • 7/23/2019 COEN 244 - 6 - Functions

    8/22

    &torae Classes and &cope

    Identifiers are used as #ariable andfunction names. Identifier properties,

    Identifier7s storae class

    8etermines the period durin which thatidentifier e"ists in memory

    Identifier7s scope

    8etermines where the identifier can bereferenced in a proram

  • 7/23/2019 COEN 244 - 6 - Functions

    9/22

    ariables

    9ocal : !hese are #ariables defined

    within a function. !hey will be #isible

    only within the function. !hey e"ist only

    durin the e"ecution of a function(e"ception static local #ariables*.

    lobal: !hese are #ariables defined

    outside of all the functions. !hey will be#isible in all the functions and they e"ist

    durin the entire e"ecution.

  • 7/23/2019 COEN 244 - 6 - Functions

    10/222008 Pearson Education, Inc. All rights reserved.

    10

    6.; Function Prototypes and Arument

    Coercion (Cont.*

    Function signature (or simply signature) The portion of a function prototype that includes the

    name of the function and the types of its parameters

    Does not specify the functions return type

    Functions in the same scope must have unique

    signatures

    The scope of a function is the region of a programin which the function is known and accessible

  • 7/23/2019 COEN 244 - 6 - Functions

    11/222008 Pearson Education, Inc. All rights reserved.

    11

    6.; Function Prototypes and Arument

    Coercion (Cont.*

    Argument oercion Forcing arguments to the appropriate types specified by

    the corresponding parameters

    For e!ample" calling a function with an integer argument"

    even though the function prototype specifies a doubleargument

    #!$ double sqrt( double !)

    int ! % &'

    sqrt( !)

    The function will still work correctly

  • 7/23/2019 COEN 244 - 6 - Functions

    12/222008 Pearson Education, Inc. All rights reserved.

    12

    '"plicit and Implicit Con#ersion between

    data types

    nary cast operator (e!plicit conversion) reates a temporary copy of its operand with a different data

    type * #!ample$ int total % +

    static_cast< double > ( total )

    reates temporary floating,point copy of total Assignment operator( e!plicit conversion)

    - Lower data type may be converted to

    higher data type

    Ex int x !"; double y; y ! x;- #igher data type may be converted to

    lower data type

    Ex int x; double y ! "$%; x ! y;

  • 7/23/2019 COEN 244 - 6 - Functions

    13/222008 Pearson Education, Inc. All rights reserved.

    1#

    Argument coercion occurs as if the argument value werebeing assigned directly to the parameter variable*

    onverting a value to a lower fundamental type

    -ill likely result in the loss of data or incorrect values

    an only be performed e!plicitly

    .y assigning the value to a variable of lower type (some

    compilers will issue a warning in this case) or

    .y using a cast operator

  • 7/23/2019 COEN 244 - 6 - Functions

    14/222008 Pearson Education, Inc. All rights reserved.

    1$

    /romotion (implicit conversion) onverting a value (e*g* int) to another data type (e*g*

    double) to perform a calculation

    00 /romotion 1ules

    Apply to e!pressions containing values of two or more data

    types

    2uch e!pressions are also referred to as mi!ed,type

    e!pressions

    #ach value in the e!pression is promoted to the 3highest4type in the e!pression

    Temporary version of each value is created and used for

    the e!pression

    5riginal values remain unchanged

    1%

  • 7/23/2019 COEN 244 - 6 - Functions

    15/222008 Pearson Education, Inc. All rights reserved.

    1%

    Fi. 6.6< Promotion hierarchy for fundamental data types.

    1&

  • 7/23/2019 COEN 244 - 6 - Functions

    16/222008 Pearson Education, Inc. All rights reserved.

    1&

    6.6 C== &tandard 9ibrary %eader Files

    00 2tandard 6ibrary header files #ach contains a portion of the 2tandard 6ibrary

    Function prototypes for the related functions

    Definitions of various class types and functions

    onstants needed by those functions

    37nstruct4 the compiler on how to interface with library

    and user,written components

    8eader file names ending in $h

    Are 3old,style4 header files

    2uperseded by the 00 2tandard 6ibrary header files

    1'

  • 7/23/2019 COEN 244 - 6 - Functions

    17/222008 Pearson Education, Inc. All rights reserved.

    1'

    Fi. 6.>< C== &tandard 9ibrary header files. (Part 3 of ?*

    18

  • 7/23/2019 COEN 244 - 6 - Functions

    18/222008 Pearson Education, Inc. All rights reserved.

    18

    Fi. 6.>< C== &tandard 9ibrary header files. (Part 4 of ?*

    1(

  • 7/23/2019 COEN 244 - 6 - Functions

    19/222008 Pearson Education, Inc. All rights reserved.

    1(

    Fi. 6.>< C== &tandard 9ibrary header files. (Part 5 of ?*

    20

  • 7/23/2019 COEN 244 - 6 - Functions

    20/222008 Pearson Education, Inc. All rights reserved.

    20

    Fi. 6.>< C== &tandard 9ibrary header files. (Part ? of ?*

  • 7/23/2019 COEN 244 - 6 - Functions

    21/22

    )ath *i+rar -unctions

    lobal functions

    8o not belon to a particular class

    %a#e function prototypes placed in header

    files Can be reused in any proram that includes the

    header file and that can link to the function7s

    ob@ect code

    '"ample: s&rtin header files&rt( '$ )

    All functions in are lobal functions

  • 7/23/2019 COEN 244 - 6 - Functions

    22/22

    -ig. &.2 )ath li+rar /unctions.


Recommended