COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Chapter 6: User-Defined Functions
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Methods.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
TOPIC : SEQUENTIAL AND PARALLEL BLOCKS Module 2.3 : Behavioral modeling in verilog.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Department of Computer Science
– Introduction to Object Technology
Static Methods 14-Nov-18.
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
CS2011 Introduction to Programming I Methods (I)
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Local variables and how to recognize them
Loops and Iteration CS 21a: Introduction to Computing I
Methods/Functions.
Corresponds with Chapter 5
Control Statements:.
Presentation transcript:

COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4

Breaking Problems Down () We commonly break large problems into smaller pieces –How do you put a puzzle together? Locate the perimeter pieces –Edges –Corners Separate the remaining pieces into smaller groups –Major colors and patterns –Common shapes –We follow a similar process when developing a program Split the program’s functionality into smaller sections Determine what data communication occurs between the sections –The data each section requires –The data each section returns Organize the sections logically Code each section one at a time –Start with the main function –Continue with the remaining sections »In logical order

Methods() Every Java program has 1 or more functions –1 method must be named “main” –Each method represents a logical section of the program –Method operation The main method is executed A statement of the main method calls another method –Execution of the program moved to the called method Every statement in the called method is executed –Following a top-down approach –If a statement calls another method »Execution moves to the called method After the last statement of the called method is executed –Execution of the program returns to the calling method »The statement immediately following the calling statement is executed The last statement of the main method is last statement executed –Unless execution is stopped earlier »A runtime error »An exit statement is executed

Data type of the returned value User-defined Munctions () General design of a method public int calcArea(int length, int width){ int area; area = length * width; return area; // Not required for void data type } Method name Parameter list (0 or more variables) Method body public int calcArea(int length, int width) The method’s header Method’s visibility

User-defined Functions () Important notes about method –A method can have any number and type of parameters (variables) A method without any parameters will always produce the same results –The return statement is required for all methods Except in the case of the void return type –A return statement can still be used »It must return “void” »i.e., return void; return; –The returned value must match the specified return type

Calling Methods () Calling a method requires the following –The name of the method –A value for every parameter in the method Values must be listed in the proper order Values must match the specified data type –The returned value may be assigned to a variable Not required, but the data will be lost public static void main(String[] args) { int myArea; myArea = calcArea(4, 6); calcArea(2, 3); } public static int calcArea(int length, int width) { int area; area = length * width; return area; }

Variable Scope Scope = Accessibility A variable’s scope is dependent on its location in the program –Global Scope Must be declared outside any methods –Usually in the global section of the class Global scope variables can be accessed from any method in the class –This can lead to unexpected results if the programmer is not careful Global scope variables are accessible as long as the class exists –Local Scope Must be declared inside a method –Includes parameter variables Local scope variables can only be accessed from within their method Local scope variables can only be accessed after they are declared A local scope variable can “hide” a global scope variable –This can lead to unexpected results if the programmer is not careful –Can be avoided by always using unique names for all variables

Variable Scope Examples public class ScopeTest { private static int area = 0; private static int len = 5; private static int wid = 3; public static void main(String[] args) { int myArea; myArea = calcArea(4, 6); calcArea(2, 3); } public static int calcArea(int length, int width) { int area; area = length * width; return area; }