The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Similar documents
EECS402 Lecture 02. Functions. Function Prototype

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

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

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

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

4. C++ functions. 1. Library Function 2. User-defined Function

Lab Instructor : Jean Lai

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CSCE Practice Midterm. Data Types

BITG 1113: Function (Part 2) LECTURE 5

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

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

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

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

CSCE 206: Structured Programming in C++

True or False (12 Points)

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

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

LECTURE 06 FUNCTIONS

FORM 2 (Please put your name and form # on the scantron!!!!)

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

type name(arg1, arg2,...) { body return }

Functions. Arizona State University 1

Study Guide for Test 2

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

6. C++ Subprograms David Keil CS I slides 7/03 1

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.

Name Section: M/W or T/TH. True or False (14 Points)

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Functions. Arash Rafiey. September 26, 2017

CSCE Practice Midterm. Data Types

Lecture 23: Pointer Arithmetic

Functions and Recursion

Object Oriented Design

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Functions, Arrays & Structs

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Chapter 6: Functions

Inheritance and Overloading. Week 11

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Lecture 5: Methods CS2301

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Unit 7. 'while' Loops

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

Computer Programming

Introduction to C ++

C/C++ Functions. Mark Redekopp

CSc Introduction to Computing

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Ch 6. Functions. Example: function calls function

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

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

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Use the dot operator to access a member of a specific object.

Lecture 9 - C Functions

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

Name SECTION: 12:45 2:20. True or False (12 Points)

W3101: Programming Languages C++ Ramana Isukapalli

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

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

COEN244: Class & function templates

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

A SHORT COURSE ON C++

Your first C++ program

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

TEMPLATES AND EXCEPTION HANDLING

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

The University Of Michigan. EECS402 Lecture 09. Andrew M. Morgan. Savitch Ch Compiling With Multiple Files Make Utility.

METHODS. CS302 Introduction to Programming University of Wisconsin Madison Lecture 15. By Matthew Bernstein

Unit 14. Passing Arrays & C++ Strings

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

C++ For Science and Engineering Lecture 12

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Scientific Computing

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

2. Functions I: Passing by Value

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Transcription:

The University Of Michigan Lecture 02 Andrew M. Morgan Savitch Ch. 3-4 Functions Value and Reference Parameters Andrew M. Morgan 1 Functions Allows for modular programming Write the function once, call it many times Group similar things together Parameters allow values from calling function to be used within the function May, or may not, return a value to calling function General function template: return_type function_name(formal parameter list) function code... Andrew M. Morgan 2 1

Function Prototype A function prototype "declares a function" Provides user with information about the function. Name of function, what values (and types) need to be passed in, what type to expect the function to return Here is a function prototype for a factorial function int computefactorial(int num); The function is called "computefactorial" Takes in one integer value from the calling function as a parameter Returns an integer value to the calling function Note: The function name should be descriptive of its purpose! Andrew M. Morgan 3 Function Definition A function definition provides the implementation (in C++) of an algorithm Here is a function definition for computing factorial of a number passed in by the user: int computefactorial(int num) //Function header int result = 1; //Value to return.. int i; //Loop variable for (i = 1; i <= num; i++) result *= i; return (result); //Returns an integer, as expected Function header matches function prototype (no ; though) Andrew M. Morgan 4 2

Function Call A function is "called" when you want to use the algorithm that was implemented in the function Here is how the main function would call computefactorial: int fact; //Factorial result int val = 5; fact = computefactorial(val); //function call! cout << "Fact. of " << val << " is: " << fact << endl; Fact. of 5 is: 120 Andrew M. Morgan 5 Complete Function Program Here is the complete program layout #include <iostream> //Need for cout. using namespace std; //Function: computefacorial - Computes //factorial of num and returns it. int computefactorial(int num); //proto int fact; int val = 5; //Factorial result fact = computefactorial(val); //call int computefactorial(int num) //header int result = 1; //Value to return.. int i; //Loop variable for (i = 1; i <= num; i++) result *= i; return (result); //Return factorial cout << "Fact. of " << val << " is: " << fact << endl; Fact. of 5 is: 120 Note: The order shown is important Andrew M. Morgan 6 3

Multiple Parameters Often, multiple values from the calling function are needed Any number of parameters can be passed in to a function #include <iostream> //Need for cout. using namespace std; //Function: addnums - Computes sum //of 3 ints and returns it int addnums(int a, int b, int c); int num1 = 5; //Integer for test int num2 = 3; //Integer for test int result; //Result of call int addnums(int a, int b, int c) int result; result = a + b + c; return (result); //Return sum result = addnums(num1, 6, num2); cout << "Result is: " << result; cout << endl; Result is: 14 Andrew M. Morgan 7 Overloading Functions Multiple functions can have same name Must have unique parameter list, though Function signature Function name and types and order of parameters in parameter list Functions must have a unique signature Overloading: Multiple functions with same name //square an int, and //return the value int squareint(int num); //square a float, and //return the value float squarefloat(float num) //Draw a square on //the screen int drawsquare(int x, int y, Not Overloaded Overloaded //square an int, and //return the value int square(int num); //square a float, and //return the value float square(float num) //Draw a square on //the screen int square(int x, int y, int len, int wid); int len, int wid); Andrew M. Morgan 8 4

Overloading Example int overloadsum(int a, int b, int c) cout << "(i i i) version" << endl; return (a + b + c); float overloadsum(float a, float b, float c) cout << "(f f f) version" << endl; return (a + b + c); float overloadsum(int a, float b, float c) cout << "(i f f) version" << endl; return (a + b + c); //The following produces a compile error! //int overloadsum(int a, float b, float c) // // cout << "(i f f) version" << endl; // return (a + b + c); // float ans; float f1 = 6.4; float f2 = 4.2; int i1 = 4; int i2 = 6; ans = overloadsum(f1, f2, f2); cout << ans << endl; //This (can) produce a compiler warning! ans = overloadsum(i1, i2, i2); cout << ans << endl; //This produces a compiler error! //ans = overloadsum(i2, i1, f1); //cout << ans << endl; ans = overloadsum(i2, (float)i1, f1); cout << ans << endl; (f f f) version 14.8 (i i i) version 16 (i f f) version 16.4 Andrew M. Morgan 9 Some Words On Scope Any variable declared in a function is "local" to that function It only exists from the time it's declared until the end of the function The function add4() can NOT access the variables bar, or result, from main(). The function main() can NOT access the variables foo, or result, from add4(). Even though both functions have a variable called result - they are unique variables, in unique addresses, with unique scopes. int add4(int foo) int result; result = foo + 4; return (result); int bar = 7; int result; result = add4(bar); Andrew M. Morgan 10 5

Pass-By-Value When a parameter is "passed-by-value" into a function, a new variable is declared to store the parameter The initial value of the parameter is copied from the value passed in from the calling function When the parameter is changed, only the copy is changed The following slides step through an example Left side: Program with arrow indicating current statement Right side: Memory contents at each step through the program Bottom: Current output of program Andrew M. Morgan 11 Pass-By-Value Example, p. 1 //changeval() changes the value of //the parameter within the function int changeval(int val) result = changeval(num); (none yet) 1001? main::result 1002 Andrew M. Morgan 12 6

Pass-By-Value Example, p. 2 //changeval() changes the value of //the parameter within the function int changeval(int val) result = changeval(num); 1001? main::result 1002 Andrew M. Morgan 13 Pass-By-Value Example, p. 3 //changeval() changes the value of //the parameter within the function int changeval(int val) result = changeval(num); 1001? main::result 1002 19 changeval::val Andrew M. Morgan 14 7

Pass-By-Value Example, p. 4 //changeval() changes the value of //the parameter within the function int changeval(int val) result = changeval(num); 1001? main::result 1002 2 changeval::val Andrew M. Morgan 15 Pass-By-Value Example, p. 5 //changeval() changes the value of //the parameter within the function int changeval(int val) result = changeval(num); 1001 2 main::result 1002 Andrew M. Morgan 16 changeval::val goes out of scope and is removed! 8

Pass-By-Value Example, p. 6 //changeval() changes the value of //the parameter within the function int changeval(int val) result = changeval(num); result: 2 num: 19 1001 2 main::result 1002 Andrew M. Morgan 17 Pass-By-Reference Andrew M. Morgan 18 C++ Only (Not available in C) Unlike pass-by-value, parameter "references" the same memory location (no copy is made) Accomplished by including an '&' before the parameter name in function prototype and header Changing the value of a reference parameter in a function changes the value of the variable in the calling function (since the same memory is referenced) Argument in function call MUST be a variable Can not be a literal or a constant (since it could be changed) Allows for multiple values to be "returned" from a function An example is traced on the following slides 9

Pass-By-Reference Example, p. 1 //changeref() changes the value of //the parameter in main as well! int changeref(int &val) result = changeref(num); 1001? main::result 1002 Andrew M. Morgan 19 Pass-By-Reference Example, p. 2 //changeref() changes the value of //the parameter in main as well! int changeref(int &val) result = changeref(num); 1001? main::result 1002 1000 & changeref::val Andrew M. Morgan 20 10

Pass-By-Reference Example, p. 3 //changeref() changes the value of //the parameter in main as well! int changeref(int &val) result = changeref(num); 1000 2 main::num 1001? main::result 1002 1000 & changeref::val Andrew M. Morgan 21 Pass-By-Reference Example, p. 4 //changeref() changes the value of //the parameter in main as well! int changeref(int &val) result = changeref(num); 1000 2 main::num 1001 2 main::result 1002 Andrew M. Morgan 22 11

Pass-By-Reference Example, p. 5 //changeref() changes the value of //the parameter in main as well! int changeref(int &val) result = changeref(num); result: 2 num: 2 1000 2 main::num 1001 2 main::result 1002 Andrew M. Morgan 23 Swap Example, Multiple Reference Params void swap(int &a, int &b) //Pass-by-reference! int temp; temp = a; a = b; b = temp; int n1 = 5; int n2 = 10; cout << "Before swap - n1: " << n1 << " n2: " << n2 << endl; swap(n1, n2); cout << "After swap - n1: " << n1 << " n2: " << n2 << endl; Before swap - n1: 5 n2: 10 After swap - n1: 10 n2: 5 Andrew M. Morgan 24 12

Advantages of Modularity Cleaner code A call to a function called "computefactorial()" is compact and essentially self-documenting A loop to compute the factorial would not be immediately clear Breaks the program into smaller pieces Real world: Write specifications and prototypes for needed functions, then distribute different functions to different people - parallel coding is faster Easier testing How to test one, huge, monolithic, 30,000 line program? Modular program can be tested module by module (function by function, in this case) Andrew M. Morgan 25 Modular Testing - Driver Programs Driver programs allow you to test a newly written function The purpose of a driver program is simply to call your function and output some results to check correctness Most main programs in lectures so far have been driver programs - to demonstrate the use of other functions Especially helpful when the function you are writing is buried deep in some million line project If adding functionality to a simulation that takes 12 hours to run, you don't want to have to run 50 test cases (25 days) using the entire simulation just to test one function Andrew M. Morgan 26 13

Modular Testing - Stubs Stubs allow you to test a program that is unfinished If waiting for someone else to finish an important function, you would still want to do some testing. Provide the function prototype and a "dummy" body Stub does not return actual value that the function will, but allows you to call the function as if it were complete, for testing. This simply allows you to have the function defined in some way so when the function is ready, the stub is simply replaced with the actual function. Andrew M. Morgan 27 14