Subprograms - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Subprograms

Description:

void printResults(float root1); printResults(root1, root2) ... int printResults(float root1, float root2) { cout 'I should be printing out results' endl; ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 24
Provided by: anniegro
Category:

less

Transcript and Presenter's Notes

Title: Subprograms


1
Functions
2
Subprograms
  • A program within a program
  • A module
  • code written to define one well-defined subtask
  • Benefits of modularity
  • Easier to test, debug, and correct
  • bottom up testing - independent testing of
    modules
  • Readability
  • Reusability
  • Maintainability

3
Structured Programming
  • Subprograms are an important element in
    structured programming
  • Emphasis is on the flow of control between
    modules and how the modules communicate
  • Easy to distribute work between multiple
    developers
  • This skill can be applied to other languages you
    wish to learn
  • Examples of some other structured programming
    languages C, Java, Lisp, Smalltalk, Visual Basic

4
Functions
  • Subprograms in C
  • 3 components of functions
  • function declaration/prototype/signature
  • function call
  • function definition, function body with the header

5
Function Declarations
  • ltreturn typegt ltfunction namegt (ltlist of
    argumentsgt)
  • ltreturn typegt is any valid type
  • this specifies the type of the value returned by
    the function, if any
  • this is also specifies the type of the function
  • ltfunction namegt is any valid identifier
  • make this descriptive
  • remember verbNoun suggestion from Style Guide
    e.g. computeAverage()
  • ltlist of argumentsgt
  • these are called parameters
  • this specifies any values that are needed by the
    function
  • it is ok to just specify the type and not assign
    a name in a function declaration
  • float computeAverage(int, int, int)
  • float computeAverage(int first, int second, int
    third)
  • just like variables, functions must be declared
    before they are used

6
Function Call
  • ltreturn variable gt FunctionName(ltparameter
    listgt)
  • ltreturn variablegt is only appropriate if the
    function is of type other than void
  • int selection GetMenuChoice()
  • DisplayMenu()
  • float sqrt pow(x, 0.5)

7
Function Definition
  • The function heading plus its implementation
  • float computeAverage(int first, int second, int
    third)
  • float retVal 0
  • retVal (first second third) / 3
  • return retVal

8
Library Functions
  • Functions that are used so frequently that they
    are provided for you by the compiler
  • To call/invoke a library function
  • functionName(ltparameter listgt)
  • These functions are declared in the appropriate
    header files, e.g. include ltmath.hgt
  • there is no need to provide any additional
    declarations for these, it would actually be an
    error
  • Return values
  • float answer pow(3,4) // answer now contains
    the value 81
  • To find out how to use these functions
  • look in the header file where it is defined for
    its function signature
  • look at the manual pages or online help for that
    function
  • man pow

9
Library Function Examples
  • Mathematics functions
  • pow, sqrt
  • found in math.h
  • Trigonometric functions
  • sin, cos, tan, acos, asin, atan
  • also found in math.h
  • String manipulation functions
  • strcat, strcpy
  • found in string.h

10
User-defined Functions
  • Need for this arises because library functions do
    not provide you with all the functionality you
    desire
  • float cube(float x)
  • return (xxx)
  • float cube(float x)
  • return pow(x,3)
  • Your program has specific needs and functionality

11
Function Components Example
12
Functions without Returned Values
  • Return type of void
  • Does some work and doesnt need to return
    anything
  • void displayResults(int numStudents, float
    average)
  • Needs to return multiple values
  • void getInput(int first, int second, int
    third)

13
Functions without Parameters
  • Has a parameter list of void
  • Does work without requiring input
  • void displayHeader(void)
  • Requires no input, but returns a single value
  • int getSingleInput(void)

14
Parameters/Arguments
  • Necessary so values can be passed to and from
    functions
  • Passed by value
  • this is the default
  • input only
  • Passed by reference
  • for anyone who knows C, this is something C does
    not have
  • input and output

15
Terms
  • Formal parameter
  • the symbolic name for the parameter used in the
    function definition
  • Actual parameter
  • the symbolic name for the parameter used in the
    function call
  • int area(int length, int width) return length
    width
  • int result area(x,y)
  • length and width are the formal parameters
  • x and y are the actual parameters
  • Allocate
  • to set aside a chunk of memory
  • this happens every time you declare a variable
  • Deallocate
  • to release a chunk of memory
  • this happens every time you leave a block

16
Value Parameters
  • The actual value of the parameter is copied and
    used in the function
  • What does this mean?
  • A new chunk of memory is allocated(created) for
    the size of the parameter
  • The value of the parameter is copied into this
    chunk of memory
  • this is called the local copy
  • Any changes that you make to the local copy will
    be lost when the function is exited unless you
    return the value
  • the memory is deallocated(destroyed)
  • Use value parameters when you do not intend to
    modify the values of the parameters in the
    function

17
Reference Parameters
  • A reference to the value of the parameter is used
    by the function
  • Whats a reference?
  • A pointer to the chunk of memory where the value
    is located
  • Whats the effect?
  • Any changes that you make to the parameter will
    still be visible after the function is exited
  • No copying takes place
  • Memory is allocated for a pointer
  • Use reference parameters when your function needs
    to modify the values
  • Use if you need to return more than one value

18
More on Reference Parameters
  • Uses the symbol
  • // Function getData
  • void getData(int length, int width)
  • cout ltlt Enter a value for the length
  • cin gtgt length
  • cout ltlt Enter a value for the width
  • cin gtgt width
  • Oftentimes, functions using reference parameters
    have type void

19
Program Design Using Functions
  • void main(void)
  • getData()
  • resultType result computeResults()
  • displayResults(result)

20
Function Overloading
  • You can create multiple functions with the same
    namethis is called overloading
  • you must vary either the number of arguments or
    the types of arguments
  • cannot overload by permutating the return type
    only
  • char append(char dest, char src)
  • char append(char dest, char src)

21
Default Function Arguments
  • As an alternative to overloading, you can provide
    default values for function arguments
  • Provide default value in the function
    declarationNOT in the function definition
  • int increment(int value, int step1)
  • int increment(int value, int step)
  • return valuestep
  • if you choose to default an argument, you must
    default all arguments following it in the
    parameter list

22
Errors with Functions
  • Parameter mismatches
  • void printResults(float root1)
  • printResults(root1, root2)
  • Forgetting to return a value if a return value is
    required
  • int printResults(float root1, float root2)
  • cout ltlt I should be printing out results ltlt
    endl
  • Passing by value when reference is necessary
  • run time error
  • void getData(float a, float b, float c)

23
Function Miscellany
  • Interface
  • how functions communicate with main and with
    other functions
  • a function signature tells other functions,
    including main, how to interface with it
  • You may use a function call to determine whether
    to enter a block
  • if (IsLeapYear(year))
  • cout ltlt Its a leap year! ltlt endl
  • You may cout the results of a function
  • cout ltlt pow(2,8)
  • You may nest function calls within function calls
  • result pow(3, pow(3,3)) // tower function
Write a Comment
User Comments (0)
About PowerShow.com