Functions and More Functions Value Parameter - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Functions and More Functions Value Parameter

Description:

Functions and More Functions Value Parameter – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 26
Provided by: myn91
Category:

less

Transcript and Presenter's Notes

Title: Functions and More Functions Value Parameter


1
Functions and More FunctionsValue Parameter
  • include ltiostream.hgt
  • int CubeByValue(int)
  • int main()
  • int num 5
  • cout ltlt "Original value of num " ltlt num ltlt
    endl
  • num CubeByValue(num)
  • cout ltlt "New value of num " ltlt num ltlt endl
  • return(0)
  • int CubeByValue(int n)
  • return n n n

2
Functions and More FunctionsReference Parameter
(C Only)
  • include ltiostream.hgt
  • void CubeByReference(int)
  • int main()
  • int num 5
  • cout ltlt "Original value of num " ltlt num ltlt
    endl
  • CubeByReference(num)
  • cout ltlt "New value of num " ltlt num ltlt endl
  • return(0)
  • void CubeByReference(int n)
  • n n n n

3
Functions and More FunctionsValue Parameters
versus Reference Parameters
  • Value parameters are parameters ...
  • for which new memory locations are allocated when
    function is called
  • into which values of corresponding arguments are
    copied
  • whose declarations have form Type Identifier
  • Reference parameters are parameters ..
  • that are aliases of their corresponding arguments
  • that reference same memory locations as their
    corresponding arguments
  • whose declarations have form Type Identifier
  • Any change to value of value parameter within
    body of function has no effect on value of its
    corresponding argument
  • Any change to value of reference parameter within
    body of function changes value of its
    corresponding argument

4
Functions and More Functions Returning Multiple
Values Using Reference Parameters
  • include ltiostream.hgt
  • void Swap(int, int)
  • int main()
  • int x, y
  • cout ltlt "\nEnter 2 integer values to be swapped
    "
  • cin gtgt x gtgt y
  • Swap(x, y)
  • cout ltlt "\nx " ltlt x ltlt " y " ltlt y ltlt endl
  • return(0)
  • void Swap(int first, int second)
  • int temp first
  • first second
  • second temp

5
Functions and More Functions Another Example on
Use of Reference Parameters
  • include ltfstream.hgt
  • include ltstdlib.hgt
  • int main()
  • void List(ifstream)
  • ifstream fin("in.dat", iosin)
  • if ( fin.fail() )
  • cerr ltlt "Error opening file."
  • exit(1)
  • List(fin)
  • fin.close()
  • return(0)

void List(ifstream fin) int value fin gtgt
value while ( !fin.eof() ) if
(value2) cout ltlt value ltlt
endl else cout ltlt value
ltlt endl fin gtgt value
6
Functions and More Functions Function Overloading
  • include ltiostream.hgt
  • void Swap(char, char)
  • void Swap(int, int)
  • void Swap(double, double)
  • int main()
  • char c1 'x', c2 'y'
  • int i1 11, i2 22
  • double d1 11.11, d2 22.22
  • Swap(c1, c2)
  • cout ltlt "\nc1 " ltlt c1 ltlt " c2 " ltlt c2 ltlt
    endl
  • Swap(i1, i2)
  • cout ltlt "\ni1 " ltlt i1 ltlt " i2 " ltlt i2 ltlt
    endl
  • Swap(d1, d2)
  • cout ltlt "\nd1 " ltlt d1 ltlt " d2 " ltlt d2 ltlt
    endl
  • return(0)
  • (continued)

7
Functions and More Functions Function
Overloading (contd)
  • void Swap(char first, char second)
  • char temp first
  • first second second temp
  • void Swap(int first, int second)
  • int temp first
  • first second second temp
  • void Swap(double first, double second)
  • double temp first
  • first second second temp

8
Functions and More Functions Function
Overloading (contd)
  • If two or more different functions have the same
    name, that name is said to be overloaded
  • The name of a function can be overloaded,
    provided no two definitions of the function have
    the same signature
  • The signature of a function is a list of the
    types of its parameters, including any const
    and reference parameter indicators ( or )
  • Examples (for the overloaded Swap function
    just discussed)
  • (char, char) (int, int) (double,
    double)
  • Signatures are important - compiler essentially
    considers a functions signature to be part of
    its name - allow compiler to distinguish calls
    to different functions with the same name
  • Return type of function is NOT part of its
    signature - 2 functions with identical
    signatures but different return types cannot have
    same name

9
Functions and More Functions Function
Overloading (contd)
  • Names should be overloaded only when it is
    appropriate
  • Different functions that perform the same
    operation (e.g., summation, swapping, find the
    minimum or maximum) on different data types are
    prime candidates for overloading
  • Giving operations that have nothing to do with
    each other the same name simply because the
    language allows you to do so is an abuse of the
    overloading mechanism and is bad programming
    style
  • The basic C/C operators , -, , and / are
    all overloaded
  • In the expression (2.0/5.0) the C/C compiler
    uses the real division operation, which produces
    the value 0.4
  • In the expression (2/5) the C/C compiler uses
    the integer division operation, which produces
    the value 0
  • Many of the other operators, including ltlt, gtgt,
    , , , -, , /, lt, gt, , lt, gt,
    and ! have also been overloaded

10
Functions and More Functions Function with
Default Argument(s)
  • In some situations, ...
  • we can declare define a single function with
    default argument(s)
  • instead of giving two or more different function
    declarations definitions
  • A function with default arguments ...
  • can be called without all of its arguments
    specified in the call
  • When arguments are unspecified in a call, ...
  • the corresponding parameters receive the default
    argument values

11
Functions and More Functions Function with
Default Argument(s) (contd)
  • include ltiostream.hgt
  • int main()
  • int Sum(int a, int b, int c 0, int d 0)
  • int a 2, b 5, c 12, d 34
  • cout ltlt "(ab) " ltlt Sum(a, b) ltlt endl
  • cout ltlt "(abc) " ltlt Sum(a, b, c) ltlt endl
  • cout ltlt "(abcd) " ltlt Sum(a, b, c, d) ltlt
    endl
  • return(0)
  • int Sum(int a, int b, int c, int d)
  • return (a b c d)

12
Functions and More Functions Function with
Default Argument(s) (contd)
  • include ltiostream.hgt
  • int main()
  • int BoxVol(int L 1, int W 1, int H 1)
  • cout ltlt "Default box volume is " ltlt BoxVol() ltlt
    endl
  • cout ltlt "Volume of box with L10, W1, H1 is "
  • ltlt BoxVol(10) ltlt endl
  • cout ltlt "Volume of box with L10, W5, H1 is "
  • ltlt BoxVol(10, 5) ltlt endl
  • cout ltlt "Volume of box with L10, W5, H2 is "
  • ltlt BoxVol(10, 5, 2) ltlt endl
  • return(0)
  • int BoxVol(int length, int width, int height)
  • return (length width height)

13
Functions and More Functions Function with
Default Argument(s)
  • Rules for Functions with Default Argument(s)
  • Default arguments must be the rightmost
    (trailing) arguments in function's parameter list
  • ? int Sum(int a, int b, int c0, int d0)
  • ? int Sum(int a, int b0, int c, int d0)
  • ? int Sum(int a0, int b0, int c, int d)
  • Default values are specified in a function's
    declaration but are not repeated in the definition

14
Functions and More Functions Function Templates
  • Overloaded Swap() Function Revisited

void Swap(char first, char second) char temp
first first second second temp
void Swap(int first, int second) int temp
first first second second temp
void Swap(double first, double
second) double temp first first
second second temp
15
Functions and More Functions Function Templates
(cont'd)
  • Wouldn't it be nice if we can represent the set
    of Swap() functions with a single generic one
  • void Swap(generic_type first, generic_type
    second)
  • generic_type temp first
  • first second second temp
  • This is the idea behind C templates
  • Allows us to parameterize type names
  • Can do so for functions - function templates
    (will do this now)
  • Can also do so for classes - class templates
    (will not do this now)
  • How'd we implement our set of Swap() functions
    using a function template?

16
Functions and More Functions Function Templates
(cont'd)
  • include ltiostream.hgt
  • template ltclass Tgt
  • void Swap(T first, T second)
  • int main()
  • char c1 'x', c2 'y'
  • int i1 11, i2 22
  • double d1 11.11, d2 22.22
  • Swap(c1, c2)
  • cout ltlt "\nc1 " ltlt c1 ltlt " c2 " ltlt c2 ltlt
    endl
  • Swap(i1, i2)
  • cout ltlt "\ni1 " ltlt i1 ltlt " i2 " ltlt i2 ltlt
    endl
  • Swap(d1, d2)
  • cout ltlt "\nd1 " ltlt d1 ltlt " d2 " ltlt d2 ltlt
    endl
  • return(0)

Template ltclass Tgt void Swap(T first, T
second) T temp first first
second second temp
17
Functions and More Functions Function Templates
(cont'd)
  • Essentially
  • Programmer writes single function template
    definition
  • Based on argument type provided in calls to the
    function, C automatically generates separate
    template functions to handle each call
    appropriately
  • Single function template definition defines a
    whole family of solutions
  • The line template ltclass Tgt is often called the
    template prefix
  • Tells compiler that the definition or prototype
    that follows is a template and that T is a type
    parameter
  • In this context, the word class actually means
    type
  • type parameter can be replaced by any type,
    whether the type is a class or not
  • in fact, ANSI Standard provides that keyword
    typename may be used instead of class in
    template prefix

18
Functions and More Functions Function Templates
(cont'd)
  • include ltiostream.hgt
  • template ltclass Tgt
  • T Max(T v1, T v2, T v3)
  • int main()
  • char c1 'x', c2 'y', c3 'z'
  • int i1 11, i2 22, i3 33
  • double d1 11.1, d2 22.2, d3 33.3
  • cout ltlt "\nThe maximum character value is "
  • ltlt Max(c1, c2, c3) ltlt endl
  • cout ltlt "\nThe maximum integer value is "
  • ltlt Max(i1, i2, i3) ltlt endl
  • cout ltlt "\nThe maximum double value is "
  • ltlt Max(d1, d2, d3) ltlt endl
  • return(0)

Template ltclass Tgt T Max(T v1, T v2, T v3) T
max v1 if (v2 gt max) max v2 if (v3 gt max)
max v3 return max
19
Functions and More Functions Overloaded
Functions vs Template Functions
  • In general, we use overloaded functions ...
  • to perform similar operations
  • that involve different program logic
  • on different data types
  • But when ...
  • the operations AND program logic are identical
  • for each data type
  • it is more compact and convenient to use
    template functions
  • Put in another way, ...
  • if each version of the operation behaves in
    exactly the same way, regardless of the type of
    data being used, choose template functions
  • otherwise, define a separate function for each
    operation and use overloading to give them the
    same name

20
Functions and More Functions Introduction to
Recursion
  • Recursion - the phenomenon of a function
    calling itself
  • A function is defined recursively if its
    definition consists of 2 parts
  • An anchor or base case, in which the value of the
    function is specified for one or more values of
    the parameter(s)
  • An inductive or recursive step, in which the
    functions value for the current value of the
    parameter(s) is defined in terms of previously
    defined function values and/or parameter values
  • Example (factorial)
  • Definition
  • Anchor or base case 0! 1
  • Inductive or recursive step for n gt 0, n!
    n(n-1)!

21
Functions and More Functions Introduction to
Recursion (contd)
  • include ltiostream.hgt
  • int factorial(int n)
  • int main()
  • int num
  • cout ltlt "To compute n!, enter n (integer only)
    "
  • cin gtgt num
  • cout ltlt num ltlt "! " ltlt factorial(num) ltlt endl
  • return(0)
  • (continued)

22
Functions and More Functions Introduction to
Recursion (contd)
  • int factorial(int n)
  • if (n 0) // base case
  • return 1
  • else if (n gt 0) // inductive step
  • return n factorial(n-1)
  • else // invalid parameter
  • cout ltlt "n! is not defined for negative n" ltlt
    endl
  • return(1)

23
Functions and More Functions Introduction to
Recursion (contd)
  • 5! 5 4! 5 24 120
  • 4! 4 3! 4 6 24
  • 3! 3 2! 3 2 6
  • 2! 2 1! 2 1
    2
  • 1! 1 0!
    1 1 1
  • 0!
    1

24
Functions and More Functions Introduction to
Recursion (contd)
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • int power(int, int)
  • int main()
  • for (int n 0 n lt 5 n)
  • cout ltlt "3 to the " ltlt n ltlt " is " ltlt
    power(3, n) ltlt endl
  • return(0)
  • int power(int x, int n)
  • if (n lt 0) cout ltlt "Bad value for power."
    exit(1)
  • if (n gt 0)
  • return( power(x, n-1) x )
  • else // n 0 (anchor or base case)
  • return (1)

25
Functions and More Functions Introduction to
Recursion (contd)
  • 35 34 3 81 3 243
  • 34 33 3 27 3 81
  • 33 32 3 9 3 27
  • 32 31 3 3 3 9
  • 31 30 3 1 3 3
  • 30 1
Write a Comment
User Comments (0)
About PowerShow.com