CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Advertisements

CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
Chapter 5 Functions.
CSC Programming for Science Lecture 30: Pointers.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Chapter 6: Functions.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
CPS120: Introduction to Computer Science Decision Making in Programs.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CPS120: Introduction to Computer Science Functions.
1 CS161 Introduction to Computer Science Topic #10.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CPS120: Introduction to Computer Science Lecture 14 Functions.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Announcement Today’s Goal  Know how to write selections besides if-else  Why we use select or if - else and how.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CSC 107 – Programming For Science. Announcements.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CSC 107 – Programming For Science. Final Exams Dec. 16, 8AM – 10AM in OM221  Exam for CSC107: Dec. 16, 8AM – 10AM in OM221  Will be done using paper.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
CSC Programming for Science Lecture 23: More on Function Parameters.
Chapter Structured Data 11. Combining Data into Structures 11.2.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Chapter 5 Function Basics
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
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.
Function “Inputs and Outputs”
CS150 Introduction to Computer Science 1
Data Structures and Algorithms Introduction to Pointers
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get parameters to do whatever you want  How to use parameters and what they are

Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to remember  Assignments update memory location with new value  Memory location updated by assignment ONLY  When variable is used in program…  …uses current value at that memory location  But what if something else modified memory?  Variable's value "updated" without an assignment  But we have no control over these location…

Variables

Variable Scope  Variables not universal, lifetime limited by scope usable  Once declared, variable usable only in braces  Scope defines lifetime where memory location used  Marks memory location "free" when scope ends  Cannot use outside scope as name will not be known

 Variables not universal, lifetime limited by scope usable  Once declared, variable usable only in braces  Scope defines lifetime where memory location used  Marks memory location "free" when scope ends  Cannot use outside scope as name will not be known  Unless we could access memory location directly  Variable is convenience to access computer's memory

 Variables not universal, lifetime limited by scope usable  Once declared, variable usable only in braces  Scope defines lifetime where memory location used  Marks memory location "free" when scope ends  Cannot use outside scope as name will not be known  Unless we could access memory location directly  Variable is convenience to access computer's memory

Variable Scope int findMin(int num) { int temp, min; cin >> min; num = 1; do { cin >> temp; if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num; small = findMin(num); cout << "In: " << num << ", min= " << small << endl; return 0; }

int findMin(int num) { int temp, min; cin >> min; num = 1; do { cin >> temp; if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num; small = findMin(num); cout << "In: " << num << ", min= " << small << endl; return 0; } Variable Scope One name -but- two memory locations

int findMin(int num) { int temp, min; cin >> min; num = 1; do { cin >> temp; if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num; small = findMin(num); cout << "In: " << num << ", min= " << small << endl; return 0; } Variable Scope One name -but- two memory locations

Parameters are Variables  Just like variables, they name memory location  Get new location each time function is called  Value stored at location changed by assignments  Unrelated to other variables even if names overlap  Parameters initialized to value in argument  Copies value into memory  Copies value into memory location created for param  Assignments affect memory location for parameter

Parameters are Variables  Just like variables, they name memory location  Get new location each time function is called  Value stored at location changed by assignments  Unrelated to other variables even if names overlap  Parameters initialized to value in argument  Copies value into memory  Copies value into memory location created for param  Assignments affect memory location for parameter  Unless new, evil parameter type can be created

Pass-By-Reference Parameters

Passing-By-Reference  Memory location NOT created  Memory location NOT created for parameter assigned memory location of argument  Instead it is assigned memory location of argument  Updates argument's value  Updates argument's value with each assignment  All this works even though argument is out-of-scope!

Passing-By-Reference Caveats  For code to compile argument must be variable  Using argument's memory location, so this required  Since lack location to use, no literals or expression  Data types must match exactly for it to work  Need the memory location sizes to be equal  0 s & 1 s must be interpreted in identical manner  Compiler will enforce rules strictly  Huge security holes can be opened by mistake

Will It Compile?

Passing-By-Reference Usage  Limits how function used now & into future  If more precision needed, cannot change data types  Needs variable for parameter, no matter what MUST  If writing function that MUST return 2+ values  Use return statement for one of the values  Have other values "returned" with pass-by-reference  Much better to rewrite code to avoid this  Pass-by-reference often makes hard to solve bugs

Passing-By-Reference Example

Tracing Example

Your Turn  Get into your groups and try this assignment

For Next Lecture  Read about arrays in Section 10.1 – 10.5  How can we use more than 1 value at a time?  What is meant by the term array?  How are array used and why do we need all these []?  Weekly Assignment #7 out & due next Tuesday  Also do not wait to start Program Assignment #2