 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 

Slides:



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

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Java: How to Program Methods Summary Yingcai Xiao.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
COMP 14: Writing Classes June 6, 2000 Nick Vallidis.
Introduction to Methods
Chapter 6: Functions.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Writing Classes (Chapter 4)
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
1 CS161 Introduction to Computer Science Topic #10.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Methods.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Chapter 7: User-Defined Functions II
A Lecture for the c++ Course
Chapter 3: Using Methods, Classes, and Objects
Pass by Reference, const, readonly, struct
User Defined Functions
6 Chapter Functions.
Chapter 9: Value-Returning Functions
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Corresponds with Chapter 5
Presentation transcript:

 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7  Exercises: p. 74, #1-3,  New files/handouts: FunctionEx3.cpp, Homework #2

 Wednesday, 9/25/02, Slide #2 “Black box” idea of functions  A function can be thought of as a “black box”, meaning its inner workings are hidden  We supply the information it needs (objects to process)  The function does its work privately  We don’t see (in main) how the function does its work PrintAverage( ) homework grade exam grade participate grade

 Wednesday, 9/25/02, Slide #3 Arguments and Parameters  When we call the function we specify what values to use -- these are called the arguments of the function  The system then matches each argument with the corresponding parameter, assigning the value of the argument to the parameter  LOCAL SCOPE: Variables and parameters declared inside a function exist only in that function  Other functions may have variables or parameters with the same name -- they are different memory locations!

 Wednesday, 9/25/02, Slide #4 ComputeAverage( ) Functions that return values  Just as parameters send information into a function from where it’s called, a function can send out information when it finishes:  For example, we could modify PrintAverage() so that, instead of printing the average, it computes and returns that average to main(): homework grade exam grade participate grade student average

 Wednesday, 9/25/02, Slide #5 Example 3: A function to compute and return a weighted average  See FunctionEx3.cpp: //ComputeAverage() Computes and returns a weighted // average of homework, exam, and participation grades double ComputeAverage(double hw, double ex, double pa) { double HwWeight = 0.50; double ExWeight = 0.35; double PaWeight = 0.15; double average = hw * HwWeight + ex *ExWeight + pa * PaWeight; return average; }

 Wednesday, 9/25/02, Slide #6 Summary of void functions  Syntax of function definition:  Syntax of function call -- A void function call is an entire statement:  Function action: When called each parameter is given the value of the corresponding argument, and the function body is executed. Control then returns to the line immediately after where the function was called. void FunctionName(type1 param1, type2 param2,...) { body of function } FunctionName(arg1, arg2,...);

 Wednesday, 9/25/02, Slide #7 Summary of value-returning functions  Syntax of function definition:  Syntax of function call -- A value-returning function call is a word, not an entire statement:  Function action: When return statement is reached, the return value is computed, and function terminates. Control is sent back to the calling line, and value is used in place of the function name. return-type FunctionName(type1 param1, type2 param2,...) { body of function: must contain at least one line: return value } cout << FunctionName(arg1, arg2,...); if ( FunctionName(arg1, arg2,...) > 0)....

 Wednesday, 9/25/02, Slide #8 Scope of variables  Local variables:  Both the variables declared inside a function definition, and the parameters of the function, are “local:” they exist only in the scope of the function  Parameters are local variables that are initialized using the values of the arguments  If the same names are used for local variables in different functions, they refer to different variables  When a function terminates, its local variables are destroyed.

 Wednesday, 9/25/02, Slide #9 Variables and constants  Sometimes we may want to name a value that we don’t want to permit to be changed.  Example: Pi =  The modifier const before the declaration makes it illegal to change the object’s value.  const float Pi = ; //Makes Pi a constant  Pi = 42.7; //Causes a compiler error!!  Constants give us added protection from programming errors.

 Wednesday, 9/25/02, Slide #10 Global constants  If several functions make use of the same constant, we can declare it  In the global scope -- not inside any function  Before the functions that want to use it  FunctionA() and FunctionB() can’t use Pi  FunctionC() and FunctionD() can use Pi int FunctionA( ); void FunctionB(int x); const double Pi = ; float FunctionC( ); double FunctionD (int a, int b);

 Wednesday, 9/25/02, Slide #11 Dangers of global variables  In general, we avoid the use of global variables, particularly if they are not const objects.  In large modular programs, it may be difficult to understand a function if some object definitions appear elsewhere  If a global object is not const and one function accidentally changes it -- that may adversely affect other functions that use the object. This is called an undesirable side effect.

 Wednesday, 9/25/02, Slide #12 Attributes of Variables (Summary)  Every variable (and constant) has:  A name  A type (what kind of data and how stored?)  A value (which, of course, can change in the case of a variable, hence the term!)  A scope (in what module(s) does it exist?)