1 CS161 Introduction to Computer Science Topic #10.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

CS161 Topic #11 1 Today in CS161 Lecture #11 More Functions Write Programs using Functions Black Jack (add the dealer and make corrections) Arguments Now.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
An Introduction to Programming with C++ Fifth Edition
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Introduction to C++ Functions Topic #2 1CS162 Topic #2.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 CS161 Introduction to Computer Science Topic #3.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
CPS120: Introduction to Computer Science Lecture 14 Functions.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
1 CS161 Introduction to Computer Science Topic #9.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 CS161 Introduction to Computer Science Topic #17.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 CS161 Introduction to Computer Science Topic #15.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
User-Written Functions
Chapter 10: Void Functions
A Lecture for the c++ Course
New Structure Recall “average.cpp” program
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
User Defined Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
CS150 Introduction to Computer Science 1
Lecture 18 Arrays and Pointer Arithmetic
Introduction to C++ Functions
Simulating Reference Parameters in C
Function “Inputs and Outputs”
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Today in CS161 Week #6 Introduction to Functions
Functions Chapter No. 5.
Presentation transcript:

1 CS161 Introduction to Computer Science Topic #10

CS161 Topic #102 Today in CS161 Functions with Arguments –What are arguments? –How do we define a function with args? –Actual arguments versus Formal arguments Functions with Arguments –Call by value versus Call by reference Questions? –Programming assignment #4

CS161 Topic #103 Functions: What are arguments? If we want to send information to a function when we call it, we can use arguments For example, when we supplied two items within the parentheses for the pow function -- these were arguments that were being passed to the function pow! We can define functions with no arguments, or with many arguments

CS161 Topic #104 Functions: What are arguments? If we go back to our example of converting inches to millimeters... –if we write a function to perform the calculations, we would need to somehow send to the function the number of inches to convert –this can be done by passing in the number of inches as an argument –and receiving the number of millimeters back as the returned value

CS161 Topic #105 Functions: What are arguments? For example, from our main program we could say: float convert (float inches);//prototype int main() { float in;//local variable to hold # inches float mm;//local variable for the result cout << “Enter the number of inches: “; cin >> in; mm = convert (in);//function call cout << in << “ inches converts to “ << mm << “mm”; return 0; }

CS161 Topic #106 Functions: What are arguments? Then, to implement the function we might say: float convert (float inches) { float mils;//local variable mils = 25.4 * inches; return mils;//return (mils); }

CS161 Topic #107 Functions: What are arguments? Notice that we can have arguments to functions! These must be in the function header for both the function declaration (prototype) and function definition. In this example, inches is a variable...which is a argument because it is defined in the function header.

CS161 Topic #108 Functions: What are arguments? When you call convert, –you are establishing an association between the main program's in variable –and the function's inches variable; –this function does some calculations, –and returns a real number which is stored in the calling routines mm variable.

CS161 Topic #109 Functions: What are arguments? Notice that variables are declared in a function heading; –these are FORMAL ARGUMENTS –they look very much like regular variable declarations, except that they receive an initial value from the function call The arguments in the function call (invocation) are called ACTUAL ARGUMENTS.

CS161 Topic #1010 Functions: What are arguments? When the function call is executed, –the actual arguments are conceptually copied into a storage area local to the called function. –If you then alter the value of a formal argument, only the local copy of the argument is altered. –The actual argument never gets changed in the calling routine.

CS161 Topic #1011 Functions: What are arguments? C++ checks to make sure that the number and type of actual arguments sent into a function when it is invoked match the number and type of the formal arguments defined for the function. The return type for the function is checked to ensure that the value returned by the function is correctly used in an expression or assignment to a variable.

CS161 Topic #1012 Functions: What are arguments? When we deal with FORMAL VALUE ARGUMENTS... –the calling actual argument values cannot be modified by the function. –This allows us to use these functions, giving literals and constants as arguments without having conflicts. –This is the default way of doing things in C++.

CS161 Topic #1013 Let's write a function to sum two numbers: int sumup(int first, int second);//function prototype intmain() { int total, number, count; total = 0; for (count = 1; count <= 5; count++) { cout << " Enter a number to add: "; cin >> number; total = sumup(total, number);//function call } cout << " The result is: " << total << endl; return 0; } int sumup(int first, int second) { //definition return first + second; }

CS161 Topic #1014 Functions: Value vs. Reference Call by value brings values into a function (as the initial value of formal arguments) –that the function can access but not permanently change the original actual args Call by reference can bring information into the function or pass information to the rest of the program; –the function can access the values and can permanently change the actual arguments!

CS161 Topic #1015 Functions: Value vs. Reference Call by value is useful for: - passing information to a function - allows us to use expressions instead of variables in a function call - value arguments are restrained to be modified only within the called function; they do not affect the calling function. - can't be used to pass information back, except through a returned value

CS161 Topic #1016 Functions: Value vs. Reference Call by reference is useful for: - allowing functions to modify the value of an argument, permanently - requires that you use variables as your actual arguments since their value may be altered by the called function; - you can't use constants or literals in the function call!

CS161 Topic #1017 Example of call by reference: void convert (float inches, float & mils); int main() { float in;//local variable to hold # inches float mm;//local variable for the result cout << “Enter the number of inches: “; cin >> in; convert (in, mm);//function call cout << in << “ inches converts to “ << mm << “mm”; return 0; } void convert (float inches, float & mils) { mils = 25.4 * inches; }

CS161 Topic #1018 Example of call by reference: void swap (int & a, int & b); int main() { int i = 7, j = -3; cout << "i and j start off being equal to :" << i << " & " << j << '\n'; swap(i,j); cout << "i and j end up being equal to :" << i << " & " << j << '\n'; return 0; } void swap(int & c,int & d) { int temp = d; d = c; c = temp; }

CS161 Topic #1019 What kind of args to use? Use a call by reference if: 1) The function is supposed to provide information to some other part of the program. Like returning a result and returning it to the main. 2) They are OUT or both IN and OUT arguments. 3) In reality, use them WHENEVER you don’t want a duplicate copy of the arg...

CS161 Topic #1020 What kind of args to use? Use a call by value: 1) The argument is only to give information to the function - not get it back 2) They are considered to only be IN parameters. And can't get information back OUT! 3) You want to use an expression or a constant in function call. 4) In reality, use them only if you need a complete and duplicate copy of the data