1 / 38

Functions in C++: Predefined and User-defined

Learn about standard functions, user-defined functions, value-returning functions, and the difference between value and reference parameters in C++ programming. Explore the scope of identifiers and static variables, function overloading, and functions with default parameters.

georgedunn
Download Presentation

Functions in C++: Predefined and User-defined

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EECE 230 Chapter 6: Functions

  2. Objectives In this chapter you will: • Learn about standard (predefined) functions • Learn about user-defined functions • Examine value-returning functions, including actual and formal parameters • Discover the difference between value and reference parameters • Learn about the scope of an identifier C++ Programming: Program Design Including Data Structures, Second Edition

  3. Objectives • Examine the difference between local and global identifiers • Discover static variables • Learn function overloading • Explore functions with default parameters C++ Programming: Program Design Including Data Structures, Second Edition

  4. Functions • Functions are like building blocks, Called modules • They allow complicated programs to be divided into manageable pieces C++ Programming: Program Design Including Data Structures, Second Edition

  5. Predefined Functions • Some of the predefined mathematical functions are: • sqrt(x) • pow(x,y) • Predefined functions are organized into separate libraries • I/O functions are in iostream header • Math functions are in cmath header C++ Programming: Program Design Including Data Structures, Second Edition

  6. The Power and sqrt Functions • pow(x,y) calculates xy, pow(2,3) = 8.0 • pow returns a value of the type double • x and y are called the parameters (or arguments) of the function pow • The square root function sqrt(x) • Calculates the non-negative square root of x, for x >= 0.0 • sqrt(2.25) is 1.5 • Type double and has only one parameter C++ Programming: Program Design Including Data Structures, Second Edition

  7. Value-Returning Functions • The syntax is: functionType functionName(formal parameter list) { statements } • functionType: type of the value returned by the function, Also called the data type • Void functions: do not have a data type C++ Programming: Program Design Including Data Structures, Second Edition

  8. Value-Returning Functions (continued) • Heading: first line • Formal Parameter: variable declared in the heading • Actual Parameter: variable or expression listed in a call to a function A function call in a program results in the execution of the body of the called function C++ Programming: Program Design Including Data Structures, Second Edition

  9. Syntax • The syntax of the formal parameter list is: dataType identifier, dataType identifier, ... • The syntax for a function call is: functionName(actual parameter list) • The syntax for the actual parameter list is: expression or variable,expression or variable, ... C++ Programming: Program Design Including Data Structures, Second Edition

  10. Functions • The formal parameter list can be empty • functionType functionName() • functionType functionName(void) • The call is: functionName() C++ Programming: Program Design Including Data Structures, Second Edition

  11. The return Statement • Once the function computes the value, the function returns the value via the return statement • The syntax of the return statement is: return expression or variable; • When a return statement executes • Function immediately terminates • Control goes back to the caller • When a return statement executes in the function main, the program terminates C++ Programming: Program Design Including Data Structures, Second Edition

  12. Function Prototype • Function Prototype: function heading without the body of the function • The syntax is: functionType functionName(parameter list); • Function prototypes appear before any function definition • It is not necessary to specify the variable name in the parameter list • The data type of each parameter must be specified C++ Programming: Program Design Including Data Structures, Second Edition

  13. Function Overloading • If several functions have the same name, every function must have: • A different set of parameters • If the number of formal parameters is the same, then the data type of the formal parameters, in the order you list, must differ in at least one position • Overloading a function refers to the creation of several functions with the same name C++ Programming: Program Design Including Data Structures, Second Edition

  14. Flow of Execution • A function call statement results in • Transfer of control to the first statement in the body of the called function • After the last statement of the called function is executed • Control is passed back to the point immediately following the function call C++ Programming: Program Design Including Data Structures, Second Edition

  15. Flow of Execution • A value-returning function returns a value • After executing the function • The value that the function returns replaces the function call statement C++ Programming: Program Design Including Data Structures, Second Edition

  16. Recursion • Recursive functions • Functions that call themselves • Example: factorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1 • Recursive relationship ( n! = n * ( n – 1 )! ) 5! = 5 * 4! 4! = 4 * 3!… • Base case (1! = 0! = 1) C++ Programming: Program Design Including Data Structures, Second Edition

  17. Recursion (continued) • Fibonacci series: 0, 1, 1, 2, 3, 5, 8... • Each number sum of two previous ones • Example of a recursive formula: • fib(n) = fib(n-1) + fib(n-2) C++ Programming: Program Design Including Data Structures, Second Edition

  18. Recursion: Towers of Hanoi • N disks of different diameters are placed on peg A so that a larger disk is always below a smaller disk. • Only the top disk on any peg may be moved to any other peg, and a larger disk may never rest on a smaller one. • Problem: move the n disks to peg c using peg b as an auxiliary: • it can be solved for n=1 • we suppose it can be solved for n-1 then show it can be solved for n Solution: • n=1: move the single disk from A to C. • n: move the top n-1 disks from A to B using C as the auxiliary • move the remaining disk from A to C • move the n-1 disks from B to C using A as the auxiliary C++ Programming: Program Design Including Data Structures, Second Edition

  19. Inline Functions • Inline functions • Keyword inline before function • Asks the compiler to copy code into program instead of making function call • Reduce function-call overhead • Compiler can ignore inline • Good for small, often-used functions C++ Programming: Program Design Including Data Structures, Second Edition

  20. Value Parameters • If a formal parameter is a value parameter • The value of the corresponding actual parameter is copied into it • The value parameter has its own copy of the data • During program execution • The value parameter manipulates the data stored in its own memory space C++ Programming: Program Design Including Data Structures, Second Edition

  21. Reference Variables as Parameters • If a formal parameter is a reference parameter • It receives the address of the corresponding actual parameter • A reference parameter stores the address of the corresponding actual parameter C++ Programming: Program Design Including Data Structures, Second Edition

  22. Reference Variables as Parameters (continued) • During program execution to manipulate the data • The address stored in the reference parameter directs it to the memory space of the corresponding actual parameter C++ Programming: Program Design Including Data Structures, Second Edition

  23. Reference Variables as Parameters (continued) • A reference parameter receives the address of the actual parameter • Reference parameters can: • Pass one or more values from a function • Change the value of the actual parameter C++ Programming: Program Design Including Data Structures, Second Edition

  24. Reference Variables as Parameters (continued) • Reference parameters are useful in three situations: • Returning more than one value • Changing the actual parameter • When passing the address would save memory space and time C++ Programming: Program Design Including Data Structures, Second Edition

  25. Parameters & Memory Allocation • When a function is called • Memory for its formal parameters and variables declared in the body of the function (called local variables) is allocated in the function data area • In the case of a value parameter • The value of the actual parameter is copied into the memory cell of its corresponding formal parameter C++ Programming: Program Design Including Data Structures, Second Edition

  26. Parameters & Memory Allocation (continued) • In the case of a reference parameter • The address of the actual parameter passes to the formal parameter • Content of the formal parameter is an address C++ Programming: Program Design Including Data Structures, Second Edition

  27. Parameters & Memory Allocation (continued) • During execution, changes made by the formal parameter permanently change the value of the actual parameter • Stream variables (for example, ifstream and ofstream) should be passed by reference to a function C++ Programming: Program Design Including Data Structures, Second Edition

  28. Scope of an Identifier • The scope of an identifier refers to where in the program an identifier is accessible • Local identifier - identifiers declared within a function (or block), accessible only within the block until the end of the block • Global identifier – identifiers declared outside of every function definition accessible everywhere in the program from the point it is declared C++ Programming: Program Design Including Data Structures, Second Edition

  29. Scope of an Identifier • Nested blocks: With nested blocks, if a variable in an inner block has the same name as the outer block, the outer is hidden until the inner block terminates C++ Programming: Program Design Including Data Structures, Second Edition

  30. Global Variables • The operator :: is called the scope resolution operator • By using the scope resolution operator • A global variable declared before the definition of a function (block) can be accessed by the function (or block) even if the function (or block) has an identifier with the same name as the variable C++ Programming: Program Design Including Data Structures, Second Edition

  31. Side Effects of Global Variables • Using global variables has side effects • Any function that uses global variables • Is not independent • Usually it cannot be used in more than one program C++ Programming: Program Design Including Data Structures, Second Edition

  32. Side Effects of Global Variables (continued) • If more than one function uses the same global variable and something goes wrong • It is difficult to find what went wrong and where • Problems caused by global variables in one area of a program might be misunderstood as problems caused in another area C++ Programming: Program Design Including Data Structures, Second Edition

  33. Static and Automatic Variables • Automatic variable - memory is allocated at block entry and deallocated at block exit • Static variable - memory remains allocated as long as the program executes • Variables declared outside of any block are static variables • By default variables declared within a block are automatic variables • Declare a static variable within a block by using the reserved word static C++ Programming: Program Design Including Data Structures, Second Edition

  34. Static and Automatic Variables (continued) • The syntax for declaring a static variable is: static dataType identifier; • The statement static int x; declares x to be a static variable of the type int • Static variables declared within a block are local to the block • Their scope is the same as any other local identifier of that block C++ Programming: Program Design Including Data Structures, Second Edition

  35. Functions with Default Parameters • When a function is called • The number of actual and formal parameters must be the same • C++ relaxes this condition for functions with default parameters • You specify the value of a default parameter when the function name appears for the first time, such as in the prototype C++ Programming: Program Design Including Data Structures, Second Edition

  36. Functions with Default Parameters (continued) • If you do not specify the value of a default parameter • The default value is used • All of the default parameters must be the rightmost parameters of the function • In a function call where the function has more than one default parameter and a value to a default parameter is not specified • You must omit all of the arguments to its right C++ Programming: Program Design Including Data Structures, Second Edition

  37. Functions with Default Parameters (continued) • Default values can be constants, global variables, or function calls • The caller has the option of specifying a value other than the default for any default parameter • You cannot assign a constant value as a default value to a reference parameter C++ Programming: Program Design Including Data Structures, Second Edition

More Related