CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 11 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 06 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 16 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Introduction to Methods
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 Methods Chapter 6 - Methods.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CS 106 Introduction to Computer Science I 10 / 08 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 7: User-Defined Functions II
Functions Review.
Chapter 9: Value-Returning Functions
Chapter 6 – Methods Topics are:
Presentation transcript:

CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? lab review more on programmer defined methods scope

Michael Eckmann - Skidmore College - CS Spring 2008 Lab problems problems from the lab

example method that returns a String but has no parameters public static String random_string()‏ { int num = (int) (Math.random() * 3); if (num == 0)‏ return “Hello”; if (num == 1)‏ return “Goodbye”; if (num == 2)‏ return } how to call this method: System.out.println(“the random string is: “ + random_string()); let's put this stuff in a program

reasons to create and use methods separation and modularization of ideas is helpful when writing large programs because smaller parts of a problem are easier to solve the individual methods can be tested and confirmed correct to reduce debugging woes methods lend themselves to software reuse less code repetition more readable, better designed code

scope the scope of an identifier (a variable, reference or method name) is defined as the portion of the program that can access that identifier the two scopes for identifiers are class scope and block scope class scope starts at the opening left curly brace of a class definition and ends at its corresponding right curly brace block scope refers to identifiers that are accessible from when they are declared until the next right curly brace.

scope we've seen block scope when we declare a variable inside the main method. we've seen block scope when we declare a variable in the first expression of a for loop. we've seen block scope when we declare a variable within the curly braces of a while loop. etc. What is the effect if block scope?

variables in methods variables declared in methods are said to be local variables --- they are local to that method parameters of the method are also considered local variables to that method. they are not usable, nor are they even known outside that method --- they have block scope they are created (memory is allocated for them) on entry into the method during execution they are destroyed (memory for them is marked for deallocation) on exit from the method

some method terminology the name of the method is salary_after_raise, and the body of the method is the three lines of code between the curly braces. public double salary_after_raise(double curr_sal, double raise_pct)‏ { double new_sal; new_sal = curr_sal * ( 1 + raise_pct / 100 ); return new_sal; }

example of local variables in this method, new_sal is a local variable, and the parameters of the method: curr_sal and raise_pct are also local variables. public double salary_after_raise(double curr_sal, double raise_pct)‏ { double new_sal; new_sal = curr_sal * ( 1 + raise_pct / 100 ); return new_sal; } they are only known to this method what does that mean “only known to this method”

return type and argument types the return type of the method is double the two parameters are of type double public double salary_after_raise(double curr_sal, double raise_pct)‏ { double new_sal; new_sal = curr_sal * ( 1 + raise_pct / 100 ); return new_sal; } note that the return statement returns the double and this is where the execution of the method ends

method calls in the call of the method, two values are passed in to the method (we call them the arguments) and the value returned by the method call is stored in new_salary double new_salary; double old_salary = 300; double raise = 4.5; // our call to the salary_after_raise method new_salary = salary_after_raise( old_salary, raise );

variables passed in as arguments when calling a method the values of variables (of primitive types) that are passed into a method as arguments remain unchanged after the method ends its execution even if the corresponding parameter in the method changes its value, this change is only local to the method, and the value is not “passed back out” of the method. we’ll see this in the factorial method on the next slide.

compute_factorial method public static int compute_factorial(int num)‏ { int temp_factorial=1; while (num > 0)‏ { temp_factorial = temp_factorial * num; num--; } return temp_factorial; }

compute_factorial method this method is a programmer-defined method (that is, we make up the name and the purpose of the method.)‏ the name of the method is: compute_factorial the return type of the method is: int there is one parameter to the method, which is: num (of type int)‏ note that the value of num inside the method changes (it gets decremented in the loop) but this is only a local change the other local variable (temp_factorial) is used to compute the factorial and its value is the one that is returned to the caller.

compute_factorial method note that the value of num inside the method changes (it gets decremented in the loop) but this is only a local change the variable sent in as an argument does NOT change its value Let’s write a quick program to show how the variable that is “passed in” to the method as an argument doesn’t actually get changed. We’ll print the argument’s value before and after the call to the method.