CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
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.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Functions g g Data Flow g Scope local global part 4 part 4.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
First steps Jordi Cortadella Department of Computer Science.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
User-Defined Functions (cont’d) - Reference Parameters.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
for Repetition Structures
CS 1430: Programming in C++.
Value returning Functions
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.
CS150 Introduction to Computer Science 1
Counting Loops.
Pointers & Functions.
CS150 Introduction to Computer Science 1
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Arithmetic Operations
Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers & Functions.
Introduction to Functions
Functions Divide and Conquer
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Presentation transcript:

CS150 Introduction to Computer Science 1 Summary Assignment: Wednesday, October 29, 2003. Last time we talked about functions: A function consists of: Function Prototype Function Definition Function Call Rules of parameter list. Call by reference & call by value. 1/15/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the output? void changeIt (int, int&, int&); void main() { int i,j,k,l; i = 2; j = 3; k = 4; l = 5; changeIt(i,j,k); cout << i << j << k << endl; changeIt(k,l,i); cout << i << k << l << endl; changeIt(i,j,j); cout << i << j << endl; changeIt(i,i,i); cout << i << endl; } void changeIt(int a, int& b, int& c) { a++; b += 2; c += a; } 1/15/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the output? #include <iostream> void changeIt(int, int&, int&); void main() { int i,j,k,l; i = 2; j = 3; k = 4; l = 5; changeIt(i, j, k); cout << i << j << k << endl; changeIt(k,l,i); cout << i << k << l << endl; } void changeIt(int j, int& i, int& l) { i++; j += 2; l += i; } 1/15/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a function to compute the sum and average of two integers, and return the values of sum and average. An example function call would look like: compute (4, 5, sum, average); 1/15/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Scope Variables have scope - places in the program where they can be referenced. Local scope - valid only in function or main program. Global scope - valid anywhere in program. We will use local variables most of the time. 1/15/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example void computeSum(int, int); int sum; void main() { int i,j; cin >> i >> j; sum = 0; computeSum(i,j); cout << sum << endl; } void computeSum(int num1, int num2) sum = num1 + num2; 1/15/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example int computesum(int, int); void main() { int i,j; cin >> i >> j; computesum(i,j); cout << sum << endl; } int computesum(int num1, int num2) int sum; sum = num1 + num2; return sum; 1/15/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example int computesum(int, int); void main() { int i,j,sum; cin >> i >> j; sum = computesum(i,j); cout << i << j << sum << endl; } int computesum(int num1, int num2) int i; i = num1 + num2; return i; 1/15/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example void silly(float); void main() { float x, y; x = 23; y = 5; silly(x); cout << x << y << endl; } void silly(float x) float y; y = 25.0; x = y; 1/15/2019 CS150 Introduction to Computer Science 1