CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
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 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
First steps Jordi Cortadella Department of Computer Science.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
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.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
User-Defined Functions (cont’d) - Reference Parameters.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
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.
FALL 2001ICOM Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez.
What Actions Do We Have Part 1
for Repetition Structures
CS150 Introduction to Computer Science 1
Value returning Functions
CS150 Introduction to Computer Science 1
Counting Loops.
Pointers & Functions.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Introduction to Programming
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
Functions Divide and Conquer
Life is Full of Alternatives
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Arithmetic Operations
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers & Functions.
Functions Divide and Conquer
Reading from and Writing to Files Part 2
Life is Full of Alternatives Part 3
Reading from and Writing to Files
Presentation transcript:

CS150 Introduction to Computer Science 1 Functions 11/1/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Learnt about passing arguments by value and by reference Today we will Look at more function examples and talk about scope 11/1/04 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; } void changeit(int j, int& i, int& l) { i++; j += 2; l += i; } 11/1/04 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); 11/1/04 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 11/1/04 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; 11/1/04 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; 11/1/04 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; 11/1/04 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; 11/1/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered More function examples Scope Readings P. 170 - 180 11/1/04 CS150 Introduction to Computer Science 1