Function Call Example

Size: px
Start display at page:

Download "Function Call Example"

Transcription

1 Function Call Example A Function Call Example (1) ch 3-25

2 A Function Call Example (2) ch 3-26

3 Alternative Function Declaration Recall: Function declaration is "information for compiler Compiler only needs to know: Return type Function name Parameter list Formal parameter names not needed: double totalcost(int, double); Still "should" put in formal parameter names Improves readability ch 3-27

4 Parameter ( 매개변수 ) vs. Argument ( 인수 ) Terms often used interchangeably Formal parameters/arguments In function declaration (prototype) In function definition s header Actual parameters/arguments In function call (invocation) Technically parameter is "formal" piece while argument is "actual" piece ch 3-28

5 Functions Calling Another Functions We re already doing this! main() is a function! Only requirement: Function s declaration ( 선언 ) must appear first Function s definition ( 정의, 구현 ) typically elsewhere After main()"s definition Or in separate file Common for functions to call many other functions Function can even call itself "Recursion" ch 3-29

6 Boolean Return-Type Functions Return-type can be any valid type Given function declaration/prototype: bool appropriate(int rate); And function s definition: bool appropriate (int rate) { return (((rate>=10)&&(rate<20)) (rate==0)); } Returns "true" or "false" Function call, from some other function: if (appropriate(entered_rate)) cout << "Rate is valid n"; ch 3-30

7 Declaring Void Functions Similar to functions returning a value Return type specified as "void" Example: Function declaration/prototype: void showresults( double fdegrees, double cdegrees); Return-type is "void" Nothing is returned ch 3-31

8 Declaring Void Functions Function definition: void showresults(double fdegrees, double cdegrees) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1); cout << fdegrees << " degrees fahrenheit equals n" << cdegrees << " degrees celsius. n"; } Notice: no return statement Optional for void functions ch 3-32

9 Calling Void Functions Same as calling predefined void functions From some other function, like main(): showresults(degreesf, degreesc); showresults(32.5, 0.3); Notice no assignment, since no value returned Actual arguments (degreesf, degreesc) Passed to function Function is called to "do it s job" with the data passed in ch 3-33

10 More on Return Statements Transfers control back to "calling" function For return type other than void, MUST have return statement Typically the LAST statement in function definition return statement optional for void functions Closing } would implicitly return control from void function ch 3-34

11 Preconditions and Postconditions Similar to "I-P-O" discussion Comment at function declaration: void showinterest(double balance, double rate); //Precondition: balance ( 은행잔고 ) is nonnegative account balance // rate is interest rate ( 이자율 ) as percentage //Postcondition: amount of interest on given balance, // at given rate Often called Inputs & Outputs ch 3-35

12 main(): "Special" Recall: main() IS a function "Special" in that: One and only one function called main() will exist in a program Who calls main()? Operating system (OS) Tradition holds it should have return statement Value returned to "caller" Here: operating system Should return "int" or "void" ch 3-36

13 Recursive Function ( 재귀함수 ) A function that "calls itself" Said to be recursive ( 재귀 ) In function definition, call to same function C++ allows recursion As do most high-level languages Can be useful programming technique Has limitations Divide and Conquer ( 분할및정복 ) Basic design technique Break large task into subtasks Subtasks could be smaller versions of the original task! When they are recursion ch 3-37

14 Recursive void Function Example Consider task: Search list for a value Subtask 1: search 1 st half of list Subtask 2: search 2 nd half of list Subtasks are smaller versions of original task! When this occurs, recursive function can be used. Usually results in "elegant" solution ch 3-38

15 Recursive void Function: Vertical Numbers Task: display digits of number vertically, one per line Example call: writevertical(1234); Produces output: ch 3-39

16 Vertical Numbers: Recursive Definition Break problem into two cases Simple/base case: if n<10 Simply write number n to screen Recursive case: if n>=10, two subtasks: 1- Output all digits except last digit 2- Output last digit Example: argument 1234: 1 st subtask displays 1, 2, 3 vertically 2 nd subtask displays 4 ch 3-40

17 writevertical Function Definition Given previous cases: void writevertical(int n) { if (n < 10) //Base case cout << n << endl; else { //Recursive step writevertical(n/10); cout << (n%10) << endl; } } ch 3-41

18 writevertical Trace Example call: writevertical(123); writevertical(12); // (123/10) writevertical(1); // (12/10) cout << 1 << endl; cout << 2 << endl; cout << 3 << endl; Arrows indicate task function performs Notice 1 st two calls call again (recursive) Last call (1) displays and "ends" ch 3-42

19 Recursion A Closer Look Computer tracks recursive calls Stops current function Must know results of new recursive call before proceeding Saves all information needed for current call E.g.) return address after function call To be used later Proceeds with evaluation of new recursive call When THAT call is complete, returns to "outer" computation ch 3-43

20 Recursion Big Picture Outline of successful recursive function: One or more cases where function accomplishes its task by: Making one or more recursive calls to solve smaller versions of original task Called "recursive case(s)" One or more cases where function accomplishes its task without recursive calls Called "base case(s)" or stopping case(s) ch 3-44

21 Infinite Recursion Base case MUST eventually be entered If it doesn t infinite recursion Recursive calls never end! Recall writevertical example: Base case happened when down to 1-digit number That s when recursion stopped ch 3-45

22 Infinite Recursion Example Consider alternate function definition: void newwritevertical(int n) { newwritevertical(n/10); cout << (n%10) << endl; } Seems "reasonable" enough Missing "base case"! Recursion never stops ch 3-46

23 Stacks for Recursion A stack Specialized memory structure Like stack of plates in buffet Place new on top Remove from top when needed Called "last-in/first-out (LIFO)" memory structure Recursion uses stacks Each recursive call placed on stack When one completes, last call is removed from stack ch 3-47

24 Memory Map Memory Map of a process 0x Text Code (Execution code) BSS (Global variables, static local variables) Heap (dynamic memory allocation) Heap grows downward on repeated dynamic memory allocations Stack grows upward on repeated function calls (including recursive function calls) frame for function2() (parameters, local variables) frame for function1() (parameters, local variables) frame for main() (parameters, local variables) create new frame for each function call 0xFFFF FFFF Stack ch 3-48

25 Stack Overflow Size of stack is limited Memory is finite Long chain of recursive calls continually adds to stack All are added before base case causes removals If stack attempts to grow beyond limit: Stack overflow error Infinite recursion always causes this ch 3-49

26 Recursion vs. Iteration Recursion not always "necessary" in some programming languages, recursion is not allowed Any task accomplished with recursion can also be done without it Nonrecursive: called iterative, using loops Recursive: based on function call that requires additional operations (prepare stack frame, copy arguments and return address, prepare local variables, copy return value, etc.) runs slower, uses more storage elegant solution; simple in algorithm; less coding ch 3-50

27 Recursive Functions that Return a Value Recursion not limited to void functions Can return value of any type Same technique, outline: 1. One+ cases where value returned is computed by recursive calls Should be "smaller" sub-problems 2. One+ cases where value returned computed without recursive calls Base case ch 3-51

28 Recursion Example: Powers Recall predefined function pow(): result = pow(2.0,3.0); Returns 2 raised to power 3 (8.0) Takes two double arguments Returns double value Let s write recursively For simple example ch 3-52

29 Function Definition for power() int power(int x, int n) { // x: base, n: exponent if (n<0) { cout << "Illegal argument"; exit(1); } else if (n>0) { return (power(x, n-1)*x); } else { return (1); } } ch 3-53

30 Calling Recursive Function power() Example calls: power(2, 0); returns 1 power(2, 1); returns (power(2, 0) * 2); returns 1 Value 1 multiplied by 2 & returned to original call Larger example: power(2,3); power(2,2)*2 power(2,1)*2 power(2,0)*2 1 Reaches base case Recursion stops Values "returned back" up stack ch 3-54

31 Tracing Function power():

32 Fibonacci Sequence Fibonacci Series ch 3-56

33 Recursive function fibonacci(n) int fibonacci(int n) { if ((n == 0) (n == 1)) return n; else return (fibonacci(n-1) + fibonacci(n-2)); } int main() { /* Testing Fibonacci */ for (int i=0; i<=10; i++) { cout << "Fibonacci(" << setw(2) << i << ") = " << setw(10) << fibonacci(i) << endl; } cout << "Fibonacci(" << 20 << ") = " << setw(10) << fibonacci(20) << endl; cout << "Fibonacci(" << 30 << ") = " << setw(10) << fibonacci(30) << endl; cout << "Fibonacci(" << 40 << ") = " << setw(10) << fibonacci(40) << endl; } ch 3-57

34 Ignore details Thinking Recursively Forget how stack works Forget the suspended computations Yes, this is an "abstraction" principle! And encapsulation principle! Let computer do "bookkeeping" Programmer just think "big picture" ch 3-58

35 Thinking Recursively: power Consider power() again Recursive definition of power: power(x, n) returns: power(x, n 1) * x Just ensure "formula" correct And ensure base case will be met ch 3-59

36 Recursive Design Techniques Don t trace entire recursive sequence! Just check 3 properties: 1. No infinite recursion 2. Stopping cases return correct values 3. Recursive cases return correct values ch 3-60

37 Recursive Design Check: power() Check power() against 3 properties: 1. No infinite recursion: power(x,n) returns power(x,n-1)*x 2 nd argument decreases by 1 each call Eventually must get to base case of 1 2. Stopping case returns correct value: power(x,0) is base case Returns 1, which is correct for x 0 3. Recursive calls correct: For n>1, power(x,n) returns power(x,n-1)*x Plug in values correct ch 3-61

38 Scope Rules Local variables Declared inside body of given function Available only within that function Can have variables with same names declared in different functions Scope is local: "that function is the scope of the local variable" Local variables preferred Maintain individual control over data Need to know basis Functions should declare whatever local data needed to "do their job" ch 3-62

39 Display 3.8 Local Variables //Computes the average yield on an experimental pea growing patch. #include <iostream> using namespace std; double estimateoftotal(int minpeas, int maxpeas, int podcount); //Returns an estimate of the total number of peas harvested. //The formal parameter podcount is the number of pods. //The formal parameters minpeas and maxpeas are the minimum //and maximum number of peas in a pod. int main( ) { int maxcount, mincount, podcount; double averagepea, yield; cout << "Enter minimum and maximum number of peas in a pod: "; cin >> mincount >> maxcount; cout << "Enter the number of pods: "; cin >> podcount; cout << "Enter the weight of an average pea (in ounces): "; cin >> averagepea; ch 3-63

40 // Display 3.8 Local Variables (2) yield = estimateoftotal(mincount, maxcount, podcount) * averagepea; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(3); cout << "Min number of peas per pod = " << mincount << endl << "Max number of peas per pod = " << maxcount << endl << "Pod count = " << podcount << endl << "Average pea weight = " << averagepea << " ounces" << endl << "Estimated average yield = " << yield << " ounces" << endl; } return 0; ch 3-64

41 double estimateoftotal(int minpeas, int maxpeas, int podcount) { double averagepea; } averagepea = (maxpeas + minpeas)/2.0; return (podcount * averagepea); ch 3-65

42 Procedural Abstraction ( 추상화 ) Need to know "what" function does, not "how" it does it! Think "black box" Device you know how to use, but not it s method of operation Implement functions like black box User of function only needs: declaration Does NOT need function definition Called Information Hiding Hide details of "how" function does it s job ch 3-66

43 Global Constants ( 전역상수 ) and Global Variables ( 전역변수 ) Global: declared "outside" function body Global to all functions in that file Local: declared "inside" function body Local to that function Global declarations typical for constants: const double TAXRATE = 0.05; Declare globally so all functions have scope Global variables? Possible, but SELDOM-USED Dangerous: no control over usage! ch 3-67

44 Display 3.9 Global Named Constant //Computes the area of a circle and the volume of a sphere. //Uses the same radius for both calculations. #include <iostream> #include <cmath> using namespace std; const double PI = ; double area(double radius); //Returns the area of a circle with the specified radius. double volume(double radius); //Returns the volume of a sphere with the specified radius. int main( ) { double radiusofboth, areaofcircle, volumeofsphere; cout << "Enter a radius to use for both a circle n" << "and a sphere (in inches): "; cin >> radiusofboth; ch 3-68

45 // Display 3.9 Global Names Constant (2) areaofcircle = area(radiusofboth); volumeofsphere = volume(radiusofboth); cout << "Radius = " << radiusofboth << " inches n" << "Area of circle = " << areaofcircle << " square inches n" << "Volume of sphere = " << volumeofsphere << " cubic inches n"; } return 0; double area(double radius) { return (PI * pow(radius, 2)); } double volume(double radius) { return ((4.0/3.0) * PI * pow(radius, 3)); } ch 3-69

46 Blocks Declare data inside compound statement Called a "block" Has "block-scope" Note: all function definitions are blocks! This provides local "function-scope" Loop blocks: for (int ctr=0; ctr<10; ctr++) { sum += ctr; } Variable ctr has scope in loop body block only ch 3-70

47 Nested Scope Same name variables declared in multiple blocks Very legal; scope is "block-scope" No ambiguity Each name is distinct within its scope ch 3-71

48 Summary 3-1 Two kinds of functions: "Return-a-value" and void functions Functions should be "black boxes" Hide "how" details Declare own local data Function declarations should selfdocument Provide pre- & post-conditions in comments Provide all "caller" needs for use ch 3-72

49 Summary 3-2 Local data Declared in function definition Global data Declared above function definitions OK for constants, not for variables Parameters/Arguments Formal: In function declaration and definition Placeholder for incoming data Actual: In function call Actual data passed to function ch 3-73

50 Homework Estimation of PI (π) using rand() function a area of an inner circle with radius a => πa 2 area of square with width of 2a => (2 a) 2 = 4 a 2 Using random number function, generate coordinators x, y in the range of a ~ 0 ~ +a while a N_total generation of random points, when the point (x, y) is within the circle (i.e., r=sqrt(x 2 + y 2 ) < a), increase the N_Circle. N_Circle / N_total = area of circle / area of square πa 2 / 4 a 2 so, π can be estimated as N _ circle 4 N _ total Write a pseudo code and c++ program that calculates the estimation of π with repeated trials of randomly generated coordinates (x, y), at given a=100, 10,000 and 1,000,000, and N_total = 100, 10,000, and 1,000,000. Print out the estimations of π, with precision of 10 digits after the decimal point, in the format of table, shown below. ch 3-74

51 N_total Radius a ,000 1,000, ,000 1,000, Programming Project 3-4. (p. 166) Write a pseudo code and a C++ program that calculates the gravitational attractive force between two bodies with mass m1 and m2 separated by distance d. ch 3-75

52 3.3 Calculation of power(base, exponent) using recursive function. 1) Write an algorithm in pseudo code that calculates the power(base, exponent) in recursive structure. In recursive structure, power(base, exponent) is calculated as follows: power(base, exponent) = base power (base, exponent -1), where exponent 0 power(base, 0) = 1 <Example> power(2.5, 3.0) = power(2.5, 0.0) = 1 2) Write a C++ program that inputs two double integers from standard input (cin) using the argc and argv in argument passing to main() function, assigns to base and exponent, respectively, calculates the power (base, exponent) using the recursive function, and displays the result. The recursive function should print out the base and exponent values at each recursive call. ch 3-76

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 13 Recursion Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Recursive void Functions Tracing recursive calls Infinite recursion, overflows Recursive Functions that Return

More information

Chapter 3 Function Basics

Chapter 3 Function Basics Chapter 3 Function Basics Learning Objectives Predefined Functions Those that return a value and those that don t Programmer-defined Functions Defining, Declaring, Calling Recursive Functions Scope Rules

More information

CSCI 1061U Programming Workshop 2. Function Basics

CSCI 1061U Programming Workshop 2. Function Basics CSCI 1061U Programming Workshop 2 Function Basics 1 Learning Objectives Predefined Functions Those that return a value and those that don t Programmer-defined Functions Defining, Declaring, Calling Recursive

More information

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University Module 10 Recursion Adapted from Absolute Java, Rose Williams, Binghamton University Recursive void Methods A recursive method is a method that includes a call to itself Recursion is based on the general

More information

3.2 Predefined Functions Libraries. 3.1 Top Down Design. 3.2 Predefined Functions Libraries. 3.2 Function call. Display 3.

3.2 Predefined Functions Libraries. 3.1 Top Down Design. 3.2 Predefined Functions Libraries. 3.2 Function call. Display 3. 3.1 Top Down Design 3.2 Predefined Functions Libraries Step wise refinement, also known as divide and conquer, means dividing the problem into subproblems such that once each has been solved, the big problem

More information

6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers Recursive void Methods Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University A recursive method is a method that includes a call to itself Recursion is based on the general problem

More information

Procedural Abstraction and Functions That Return a Value. Savitch, Chapter 4

Procedural Abstraction and Functions That Return a Value. Savitch, Chapter 4 Procedural Abstraction and Functions That Return a Value Savitch, 2007. Chapter 4 1 Procedural Abstraction: Functions I Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction

More information

Lecture 24 Tao Wang 1

Lecture 24 Tao Wang 1 Lecture 24 Tao Wang 1 Objectives Introduction of recursion How recursion works How recursion ends Infinite recursion Recursion vs. Iteration Recursion that Returns a Value Edition 2 Introduction If we

More information

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3 Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1 Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components

More information

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

Programmer-Defined Functions

Programmer-Defined Functions Functions Programmer-Defined Functions Local Variables in Functions Overloading Function Names void Functions, Call-By-Reference Parameters in Functions Programmer-Defined Functions function declaration

More information

LAB: INTRODUCTION TO FUNCTIONS IN C++

LAB: INTRODUCTION TO FUNCTIONS IN C++ LAB: INTRODUCTION TO FUNCTIONS IN C++ MODULE 2 JEFFREY A. STONE and TRICIA K. CLARK COPYRIGHT 2014 VERSION 4.0 PALMS MODULE 2 LAB: FUNCTIONS IN C++ 2 Introduction This lab will provide students with an

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value 1 Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables

More information

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables

More information

Chapter 4. Procedural Abstraction and Functions That Return a Value

Chapter 4. Procedural Abstraction and Functions That Return a Value Chapter 4 Procedural Abstraction and Functions That Return a Value Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables

More information

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design. Chapter 4 Procedural Abstraction and Functions That Return a Value Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables

More information

Ch 4. Parameters and Function Overloading

Ch 4. Parameters and Function Overloading 2014-1 Ch 4. Parameters and Function Overloading March 19, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Fundamentals of Programming Session 13

Fundamentals of Programming Session 13 Fundamentals of Programming Session 13 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Void Functions Call-By-Reference Parameters Using Procedural Abstraction Testing and Debugging General Debugging Techniques

Void Functions Call-By-Reference Parameters Using Procedural Abstraction Testing and Debugging General Debugging Techniques Chapter 5 In this chapter, you will learn about: Void Functions Call-By-Reference Parameters Using Procedural Abstraction Testing and Debugging General Debugging Techniques void Functions Recall a Function

More information

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved Chapter Four: Loops Slides by Evan Gallagher The Three Loops in C++ C++ has these three looping statements: while for do The while Loop while (condition) { statements } The condition is some kind of test

More information

Problem Solving: Storyboards for User Interaction

Problem Solving: Storyboards for User Interaction Topic 6 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:

More information

Chapter Four: Loops II

Chapter Four: Loops II Chapter Four: Loops II Slides by Evan Gallagher & Nikolay Kirov Chapter Goals To understand nested loops To implement programs that read and process data sets To use a computer for simulations Processing

More information

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM

More information

Chapter void Functions. Overview. Functions for All Subtasks. void-functions. Using a void-function. void-function Definition

Chapter void Functions. Overview. Functions for All Subtasks. void-functions. Using a void-function. void-function Definition Chapter 5 Functions for All Subtasks Overview 5.1 void Functions 5.2 Call-By-Reference Parameters 5.3 Using Procedural Abstraction 5.4 Testing and Debugging 5.5 General Debugging Techniques Copyright 2011

More information

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8 More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #7 due today Lab #4 is due on Monday at 8:00

More information

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Review. Modules. CS 151 Review #6. Sample Program 6.1a: Review Modules A key element of structured (well organized and documented) programs is their modularity: the breaking of code into small units. These units, or modules, that do not return a value are called

More information

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline - Function Definitions - Function Prototypes - Data

More information

C Functions. Object created and destroyed within its block auto: default for local variables

C Functions. Object created and destroyed within its block auto: default for local variables 1 5 C Functions 5.12 Storage Classes 2 Automatic storage Object created and destroyed within its block auto: default for local variables auto double x, y; Static storage Variables exist for entire program

More information

Ch 6. Structures and Classes

Ch 6. Structures and Classes 2013-2 Ch 6. Structures and Classes September 1, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

Recursion CHAPTER THINKING RECURSIVELY 669 Recursive Design Techniques 669 Binary Search 671 Efficiency of Binary Search 677

Recursion CHAPTER THINKING RECURSIVELY 669 Recursive Design Techniques 669 Binary Search 671 Efficiency of Binary Search 677 CHAPTER 11 Recursion 11.1 RECURSIVE void METHODS 651 Example: Vertical Numbers 651 Tracing a Recursive Call 654 A Closer Look at Recursion 657 Stacks for Recursion 660 Recursion versus Iteration 662 11.2

More information

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92 L E S S O N S E T 6.2 Functions that Return a Value PURPOSE PROCEDURE 1. To introduce the concept of scope 2. To understand the difference between static, local and global variables 3. To introduce the

More information

Comp 249 Programming Methodology Chapter 11 Recursion

Comp 249 Programming Methodology Chapter 11 Recursion Comp 249 Programming Methodology Chapter 11 Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified

More information

Why Is Repetition Needed?

Why Is Repetition Needed? Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable

More information

Problem Solving with C++

Problem Solving with C++ GLOBAL EDITION Problem Solving with C++ NINTH EDITION Walter Savitch Kendrick Mock Ninth Edition PROBLEM SOLVING with C++ Problem Solving with C++, Global Edition Cover Title Copyright Contents Chapter

More information

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); } 1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to

More information

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions. 6 Functions TOPICS 6.1 Focus on Software Engineering: Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5 Passing Data by Value 6.6 Focus

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

More information

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Functions. Lecture 6 COP 3014 Spring February 11, 2018 Functions Lecture 6 COP 3014 Spring 2018 February 11, 2018 Functions A function is a reusable portion of a program, sometimes called a procedure or subroutine. Like a mini-program (or subprogram) in its

More information

Programming Language. Functions. Eng. Anis Nazer First Semester

Programming Language. Functions. Eng. Anis Nazer First Semester Programming Language Functions Eng. Anis Nazer First Semester 2016-2017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

Chapter 17 - Notes Recursion

Chapter 17 - Notes Recursion Chapter 17 - Notes Recursion I. Recursive Definitions A. Recursion: The process of solving a problem by reducing it to smaller versions of itself. B. Recursive Function: A function that calls itself. C.

More information

CSC1016. Welcome (again!) Get course notes handout (and read them)

CSC1016. Welcome (again!) Get course notes handout (and read them) CSC1016 The question of whether computers can think is like the question of whether submarines can swim. Edsger Dijkstra Welcome (again!) Get course notes handout (and read them) Me - for those who don

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam,

More information

Lab Instructor : Jean Lai

Lab Instructor : Jean Lai Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.

More information

WARM UP LESSONS BARE BASICS

WARM UP LESSONS BARE BASICS WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions

More information

Ch 5-2. Arrays Part 2

Ch 5-2. Arrays Part 2 2014-1 Ch 5-2. Arrays Part 2 March 29, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742

More information

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Fall 2015 Jill Seaman 1 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where

More information

CMPS 221 Sample Final

CMPS 221 Sample Final Name: 1 CMPS 221 Sample Final 1. What is the purpose of having the parameter const int a[] as opposed to int a[] in a function declaration and definition? 2. What is the difference between cin.getline(str,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recap of Function Calls and Parameter Passing Dr. Deepak B. Phatak & Dr. Supratik

More information

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A. Islamic University of Gaza Computer Engineering Dept. C++ Programming For Industrial And Electrical Engineering By Instructor: Ruba A. Salamh Chapter Four: Loops 2 Chapter Goals To implement while, for

More information

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No. The American University in Cairo Computer Science & Engineering Department CSCE 106 Instructor: Final Exam Fall 2010 Last Name :... ID:... First Name:... Section No.: EXAMINATION INSTRUCTIONS * Do not

More information

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6 Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #5 due today Lab #3

More information

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions Chapter 6: 6.1 Functions Introduction to Functions What Is A Function? Why Functions? We ve been using functions ( e.g. main() ). C++ program consists of one or more functions Function: a collection of

More information

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing

More information

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;

More information

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays What we will learn about this week: Declaring and referencing arrays Arrays in memory Initializing arrays indexed variables arrays as function arguments Arrays a way of expressing many of the same variable

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

2. Functions I: Passing by Value

2. Functions I: Passing by Value Computer Science I CS 135 2. Functions I: Passing by Value René Doursat Department of Computer Science & Engineering University of Nevada, Reno Spring 2006 Computer Science I CS 135 0. Course Presentation

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

Introduction to C ++

Introduction to C ++ Introduction to C ++ Thomas Branch tcb06@ic.ac.uk Imperial College Software Society October 18, 2012 1 / 48 Buy Software Soc. s Free Membership at https://www.imperialcollegeunion.org/shop/ club-society-project-products/software-products/436/

More information

PIC 10A. Review for Midterm I

PIC 10A. Review for Midterm I PIC 10A Review for Midterm I Midterm I Friday, May 1, 2.00-2.50pm. Try to show up 5 min early so we can start on time. Exam will cover all material up to and including todays lecture. (Only topics that

More information

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver) Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program

More information

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout

More information

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10; Solving a 2D Maze Let s use a 2D array to represent a maze. Let s start with a 10x10 array of char. The array of char can hold either X for a wall, for a blank, and E for the exit. Initially we can hard-code

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A! Recursion Definition: Any time the body of a function contains a call to the function itself. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function

More information

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017 Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The

More information

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed? Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use countercontrolled, sentinel-controlled,

More information

Memory and Pointers written by Cathy Saxton

Memory and Pointers written by Cathy Saxton Memory and Pointers written by Cathy Saxton Basic Memory Layout When a program is running, there are three main chunks of memory that it is using: A program code area where the program itself is loaded.

More information

Local and Global Variables

Local and Global Variables Lecture 10 Local and Global Variables Nearly every programming language has a concept of local variable. As long as two functions mind their own data, as it were, they won t interfere with each other.

More information

LECTURE 5 Control Structures Part 2

LECTURE 5 Control Structures Part 2 LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved Chapter Five: Functions by Cay Horstmann Chapter Goals To be able to implement functions To become familiar with the concept of parameter passing To appreciate the importance of function comments To develop

More information

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. 1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format

More information

Ch 6. Functions. Example: function calls function

Ch 6. Functions. Example: function calls function Ch 6. Functions Part 2 CS 1428 Fall 2011 Jill Seaman Lecture 21 1 Example: function calls function void deeper() { cout

More information

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

More information

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers? Review for Final (Chapter 6 13, 15) 6. Template functions & classes 1) What is the primary purpose of template functions? A. To allow a single function to be used with varying types of arguments B. To

More information

LAB: WHILE LOOPS IN C++

LAB: WHILE LOOPS IN C++ LAB: WHILE LOOPS IN C++ MODULE 2 JEFFREY A. STONE and TRICIA K. CLARK COPYRIGHT 2014 VERSION 4.0 PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 2 Introduction This lab will provide students with an introduction

More information

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition) C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures

More information

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank 1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A. integer B 1.427E3 B. double D "Oct" C. character B -63.29 D. string F #Hashtag

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 6: User-Defined Functions I In this chapter, you will: Objectives Learn about standard (predefined) functions and discover

More information

C/C++ Functions. Mark Redekopp

C/C++ Functions. Mark Redekopp 1 C/C++ Functions Mark Redekopp A QUICK LOOK 2 3 Functions Also called procedures or methods Collection of code that performs a task Do a task given some inputs (but we don't care how it does it a.k.a.

More information

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1 CSE 1 Complexity Analysis Program Efficiency [Sections 12.1-12., 12., 12.9] Count number of instructions executed by program on inputs of a given size Express run time as a function of the input size Assume

More information

PIC 10A. Final Review: Part I

PIC 10A. Final Review: Part I PIC 10A Final Review: Part I Final exam The final exam is worth 30% of your grade, same weight as 2 midterms. Could be 50% if grading option 2 turns out better for you. Length is also roughly 2 midterms

More information

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016 Chapter 6: User-Defined Functions Objectives In this chapter, you will: Learn about standard (predefined) functions Learn about user-defined functions Examine value-returning functions Construct and use

More information

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is

More information

Introduction To Computer Programming C++ Mr. Clausen Program C11A, C11B

Introduction To Computer Programming C++ Mr. Clausen Program C11A, C11B Introduction To Computer Programming C++ Mr. Clausen Program C11A, C11B Program 11A: Cone Heads with Functions 30 points Write a program that calculates the volume of a right circular cone using functions.

More information

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem Recursion Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem What is Recursion? A problem is decomposed into smaller sub-problems, one or more of which are simpler versions of

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Storage Classes Scope Rules Functions with Empty Parameter Lists Inline Functions References and Reference Parameters Default Arguments Unary Scope Resolution Operator Function

More information

Chapter 7 - Notes User-Defined Functions II

Chapter 7 - Notes User-Defined Functions II Chapter 7 - Notes User-Defined Functions II I. VOID Functions ( The use of a void function is done as a stand alone statement.) A. Void Functions without Parameters 1. Syntax: void functionname ( void

More information

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved. Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

142

142 Scope Rules Thus, storage duration does not affect the scope of an identifier. The only identifiers with function-prototype scope are those used in the parameter list of a function prototype. As mentioned

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information