12functions1_3014

12functions1_3014 - Lecture File 12 COP 3014 January 10,...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture File 12 COP 3014 January 10, 2008 A. Ford Tyson 1 COP 3014 Lecture File 12 ± Functions Part 1 – flow of control 1 – parameter passing: call by value and call by reference – void and value-returning functions – memory map during program execution Copyright 1997-present, Ann Ford Tyson FUNCTIONS ± Recap: the four major control structures in programming 2 – sequence – selection – iteration – subprogram What are functions? ± 4th major control structure – subprogram == section of a program 3 designed to complete one task or a group of related tasks under the direction of another program unit
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 12 COP 3014 January 10, 2008 A. Ford Tyson 2 Why are functions so important? ± Allow us to implement top-down design – make structure chart evident in program – program modularity readily visible ± Maximize program readability 4 – long program best understood if broken down into small sections – a function should not be more than one or two pages long – what would a textbook be like with no chapters, no sections, no organizational structure? Why are functions so important? p.2 ± Make programs easier to write, test, debug, and modify – can work on subprograms individually to a large extent 5 ± Allow us to execute same task with different I/O at different times – e.g. DrawNum could pick a lottery number, roll dice, pick a card ± Allow us to build libraries of useful tasks (example: sqrt, rand, etc.) Terms and Concepts ± CALL a function – transfer control to it; function becomes active (executes) – a function call is an expression in C++ ± ARGUMENTS (actual parameters) 6 – program elements used to transfer information into and out of functions; "pass IN" or "pass OUT (BACK)" – declared in caller ± PARAMETERS (formal parameters) – correspond to arguments – declared in function heading
Background image of page 2
Lecture File 12 COP 3014 January 10, 2008 A. Ford Tyson 3 Function Declaration == "Prototype" <return type> data type of <function name> <parameter list> () ; 7 function call expression the prototype for a function ƒ must occur either outside all functions and before any call to ƒ, or inside all functions which call ƒ tells the compiler how the function may be called Function Definition <return type> <function name> <parameter list> functio 8 { <declarations> <executable code> } n headin g block; body of the function Declaration vs Definition ± Declaration: associates an identifier with a data object, a function, or a data type so that we can refer to that item by name ( tells the 9 compiler what an identifier means ) ± Definition: causes the compiler to allocate memory for data and/or executable code ( in this case, the executable code for a function )
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 12 COP 3014 January 10, 2008 A. Ford Tyson 4 Example Prototype: standard "void" function void assert int <expression> () ; 10 must #include <cassert> to use this function action: if <expression> is false, an error message is printed and the program run is aborted { note: this function is defined as a macro in many compilers } void example p.2 ±
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

12functions1_3014 - Lecture File 12 COP 3014 January 10,...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online