PHY 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
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Advertisements

Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
CSC Programming for Science Lecture 30: Pointers.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
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 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.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
UNIT 14 Functions with Pointer Parameters.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
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. 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.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
1 C Programming Week 2 Variables, flow control and the Debugger.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
PHY 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
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
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
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.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Problem of the Day  Why are manhole covers round?
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
C Functions -Continue…-.
Functions Dr. Sajib Datta
Module 4 Functions – function definition and function prototype.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
User-Defined Functions
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
6 Chapter Functions.
Chapter 6 Methods.
CISC181 Introduction to Computer Science Dr
Pointers and References
Methods Scope How are names handled?
Parameters and Arguments
Scope Rules.
C Parameter Passing.
Presentation transcript:

PHY 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

Variable Scope int findMin(int num) { int temp, min; scanf(“%d”, &min); num = 1; do { scanf(“%d”, &temp); if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num = 3 small = findMin(num); printf(“In: %d, min= %d\n”, num, small); return 0; }

Variable Scope int findMin(int num) { int temp, min; scanf(“%d”, &min); num = 1; do { scanf(“%d”, &temp); if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num = 3 small = findMin(num); printf(“In: %d, min= %d\n”, num, small); return 0; } One name -but- two memory locations

Variable Scope int findMin(int num) { int temp, min; scanf(“%d”, &min); num = 1; do { scanf(“%d”, &temp); if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num = 3 small = findMin(num); printf(“In: %d, min= %d\n”, num, small); return 0; } 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

Passing-By-Reference  Memory location NOT created  Memory location NOT created for parameter assigned memory location of argument  Parameter 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!

Pass-By-Reference Basics

Passing-By-Reference Errors

Passing-By-Reference Warning  Important to match type of argument & param  C/C++ might show warning, but code will compile  0 s & 1 s misinterpreted since using wrong type  When types differ, several outcomes possible  Get incorrect results, but only for that variable  Assignments change value of multiple variables  Program crashes (but not necessarily immediately)

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 Note

Tracing Example

Pass-By-Reference Basics

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