1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.

Slides:



Advertisements
Similar presentations
CSE Lecture 10 – Functions
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
Single-Result Functions Section /25/11. Programming Assignment On website.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CS 1430: Programming in C++.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
Chapter 6: User-Defined Functions I
CS 1430: Programming in C++ No time to cover HiC.
Chapter 5 Functions DDC 2133 Programming II.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
CS 1428 Exam II Review.
Chapter 5 Function Basics
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ No time to cover HiC.
CS150 Introduction to Computer Science 1
Functions, Part 2 of 3 Topics Functions That Return a Value
CS 1428 Final Exam Review.
CS 1430: Programming in C++.
CS 1428 Final Exam Review.
Chapter 6: User-Defined Functions I
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

1 CS 1430: Programming in C++

Quiz 1 2

3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute and return the square root } Function Header, Body Receiving value through parameters Function Call root1 = (-coefB + sqrt(Delta)) / (2 * coefA); Function Parameters (Arguments) Formal Parameters Actual Parameters

4 Write a C++ function to find and return the largest value of three integers. Function prototype Name, Type, Parameters, Parameter Types Function Name: Largest or LargestOfThreeInt Your choice but must be meaningful! Function Type: int Parameters: How many? num1, num2, num3 Data Type of each parameter int

5 Function prototype int Largest(int num1, int num2, int num3);

6 // Function prototype int Largest(int num1, int num2, int num3); // Specify types! // Formal parameters. int main() { int score1, score2, score3, max; cin >> score1 >> score2 >> score3; // Call function to find the highest score max = Largest(score1, score2, score3); // Function call: No types! // Actual parameters. // Different names from formal parameters. cout << "The largest score is " << max; return 0; }

7 Function definition // // The function finds and returns the largest // value of three integers. // int Largest(int num1, int num2, int num3) { // local variable int maxVal; maxVal = num1; if (num2 > maxVal) maxVal = num2; if (num3 > maxVal) maxVal = num3; return maxVal; } // Could be implemented in different ways // Where to get the values of num1, num2, num3? // Actual parameters at function calls.

8 // Function definition // Function Header // Function body int Largest(int num1, int num2, int num3) { int maxVal; maxVal = num1; if (num2 > maxVal) maxVal = num2; if (num3 > maxVal) maxVal = num3; return maxVal; }

9 Programming Rules Every function, except the main function, must have a comment which describes what it does. Other rules on functions in the future.

10 // // Comment block // // Includes // Constants // Function prototype int Largest(int num1, int num2, int num3); int main() { int score1, score2, score3, max; cin >> score1 >> score2 >> score3; max = Largest(score1, score2, score3); cout << "The largest score is " << max; return 0; } // // The function finds and returns the largest // value of three integers. // int Largest(int num1, int num2, int num3) { // local variable int maxVal; … return maxVal; }

11 Function Call and Control Transfer int main() { int score1, score2, score3, max; cin >> score1 >> score2 >> score3; max = Largest(score1, score2, score3); cout << “Max = “ << max; return 0; } int Largest(int num1, int num2, int num3) { int maxVal; maxVal = num1; if (num2 > maxVal) maxVal = num2; if (num3 > maxVal) maxVal = num3; return maxVal; } main() Largest() Function call Passing parameters Returning to main() With return value

12 Tracing in VS J:\Public_html\CS143\Labs\LargestByFunction Break Points Locals Autos Debug menu Start Debugging F5 Step Over Trace Into Largest Returned

13 Tracing on Paper Input values: main() Largest() score1 score2 score3 max num1 num2 num3 maxVal ? ? ? ? ? ? ? ?

14 Schedule Quiz4 -1 Submit to the Grader Due 10 pm Next Monday Lab 4 Test 1 Friday Program 2 Due next Tuesday (better before Test 1) Grace: next Friday

15 Using Input File Lab 4 Program 2 How to do it? –Lab4 description

16 How to Prepare for Test 1 Test 1: Friday 60 Points C++ Statements Tracing C++ Program Other staff Review Notes: 01 – 10 Quizzes Labs Prog1 Prog2

DO ALL QUIZZES! 17

STAR PROGRAMS EARLY! 18

DO NOT MISS CLASSES! 19