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

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
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.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
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.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
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.
Programming Functions: Passing Parameters by Reference.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
1 CS161 Introduction to Computer Science Topic #10.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
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.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
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.
Instructor - C. BoyleFall Semester
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.
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 CSC103: Introduction to Computer and Programming Lecture No 16.
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.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
for Repetition Structures
CS150 Introduction to Computer Science 1
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
CS 1430: Programming in C++.
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Value returning Functions
CS150 Introduction to Computer Science 1
Counting Loops.
Functions, Part 2 of 3 Topics Functions That Return a Value
CS150 Introduction to Computer Science 1
Pass by Reference.
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
Review for Final Exam.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
Arithmetic Operations
CS150 Introduction to Computer Science 1
do/while Selection Structure
Fundamental Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Life is Full of Alternatives Part 3
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

CS150 Introduction to Computer Science 1 Functions 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Started learning about functions and modularity in C++ Today we will Talk about the different types of arguments that can be used with functions 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program 18.1 Write a program that calculates the area of a circle 18.2 Write a program that calculates the area of a circle using three functions, getRadius, calcArea and outputArea 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program 18.3 Write a function that takes as input two numbers and returns the larger 18.4 Write a function that computes the average given a sum and number of elements 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Another Solution to 18.3 void larger(int, int, int&); int main() { int num1, num2, large; cin >> num1 >> num2; larger(num1, num2, large); cout << “Larger number is “ << large << endl; } void larger(int num1, int num2, int & large) if (num1 > num2) large = num1; else large = num2; 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Passing Arguments Pass by value Values are passed into the function Any changes made in the function are not reflected in the main Pass by reference Any changes made in the function are reflected in the main 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example void swap(int &, int &); int main(void) { int i, j; cin >> i >> j; swap(i,j); cout << i << j; return 0; } void swap(int & num1, int & num2) int temp; temp = num1; num1 = num2; num2 = temp; 10/31/05 CS150 Introduction to Computer Science 1

Rules for Parameter Lists There must be the same number of actual and formal arguments The correspondence between actual and formal arguments is by position only Corresponding actual and formal arguments must match in type The names of the actual and formal arguments may be the same or different For reference arguments only, the actual argument must be a single, simple variable 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Given the following function prototype: void checkIt(float &, float &, int, int, char &); And declarations in main: float x, y; int m; char next; 18.5 Which are legal? checkIt(x, y, m+3, 10, next); checkIt(m, x, 30, 10, ’c’); checkIt(x, y, m, 10); checkIt(35.0, y, m, 12, next); checkIt(x, y, m, m, c); 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 18.6 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; } 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 18.7 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; } 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 18.8 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); 10/31/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered Programmer defined functions Readings P. 170 - 180 10/31/05 CS150 Introduction to Computer Science 1