Implementing Functions from a Detailed Design Quick Tips

Slides:



Advertisements
Similar presentations
Adding Dynamic Content to your Web Site
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Programming is instructing a computer to perform a task for you with the help of a programming language.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Introduction to Python
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CPS120: Introduction to Computer Science Lecture 14 Functions.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Lecture 3: Getting Started & Input / Output (I/O)
Basic concepts of C++ Presented by Prof. Satyajit De
The Second C++ Program Variables, Types, I/O Animation!
Structures and Classes
Building Java Programs
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Content Programming Overview The JVM A brief look at Structure
MT262A Review.
while Repetition Structure
Key Ideas from day 1 slides
Data Types and Conversions, Input from the Keyboard
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Chapter 6: Modular Programming
Functions, locals, parameters, and separate compilation
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CO1401 Programming Design and Implementation
Variables, Expressions, and IO
Implementing Functions from a Detailed Design Quick Tips
CSC113: Computer Programming (Theory = 03, Lab = 01)
Useful String Methods Cont…
UMBC CMSC 104 – Section 01, Fall 2016
Arrays Part II Array Function Arguments
Returning Structures Lesson xx
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Exceptions with Functions
Value returning Functions
Functions.
Programming Funamental slides
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Faculty of Computer Science & Information System
Input from the Console This video shows how to do CONSOLE input.
CS150 Introduction to Computer Science 1
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Reading Data From and Writing Data To Text Files
Chapter 8 Functions.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CISC101 Reminders Assignment 3 due today.
CS31 Discussion 1D Fall18: week 2
Chapter 1 c++ structure C++ Input / Output
CSE Module 1 A Programming Primer
Programming Concepts and Database
What you need to do… Drag the pieces of code into the correct order like you can see in the EXAMPLE below. print ( “ int input ) = + Hello world chr ord.
Python Creating a calculator.
Presentation transcript:

Implementing Functions from a Detailed Design Quick Tips

General Idea A function is segment of code that: - gets inputs from some source - performs some operation (algorithm) to transform the inputs into results - delivers the results to some destination as output

General Idea Graphically, think of something like a Sausage Grinder: usually, several things go “in” and one thing comes “out” (though that’s not always the case)

Input Input is data “coming in” to the function. Sources include: - Data passed in from another function - Data entered by the user (human interaction) - Data read from a file

Output Output is data “going out” of the function. Destinations include: - Data passed to (back to) another function - Data delivered to the user (printed) - Data written to a file

Modify Modify (also called Input/Output) is data that is both "coming in" and "going out". Source: the invoking function Destination: the invoking function

Important Design Philosophy “That’s not MY job!!!” A function should do a single task. The task of some functions is to interact with the user: ask for input and print output. But some functions do NOT interact with the user: they get their input from elsewhere, and deliver their output elsewhere.

Important Design Philosophy “That’s not MY job!!!” Think about workers in a fast food restaurant: Some interact with customers (take and deliver orders), others do not: cooks for instance. One employee takes the order, and “passes” that info to the cook. The cook prepares the food and “passes” it back to the first employee, who then gives it to the customer.

Important Design Philosophy “That’s not MY job!!!” Functions are designed to work in the same way: each does a specific job, and not another job; and some jobs don’t involve interacting with the customer (user). The point: not every function will use cin/cout!

Detailed Designs Detailed Design Documents are very “technical” in nature, but are still written in a human language. This language must be interpreted and translated into actual C++ code. One of the easiest ways to do this is to look for the use of key phrases and words that indicate the specification of the code.

Detailed Designs: Inputs Input Arguments: Look for phrases like: - “function is given” - “function is passed” - “function takes” Data that “is given” indicates use of PBV Formal Variables.

Detailed Designs: Inputs Input Arguments: Examples “Write a function that is given two integers...” Interpretation: void fun(int a, int b) “Write a function that takes a dollar amount...” Interpretation: void fun(float dollarAmt)

Detailed Designs: Inputs Input Arguments: Pitfall Here is where beginning students often make a mistake: they correctly determine that a value is an Input Argument, but when they write the function, they still “ask the user”. Remember that Input Arguments already have a value...assigned by the invoking function. So there is no need to “ask the user” again

Detailed Designs: Inputs Input Arguments: Pitfall “Write a function that is given a subtotal and calculates and returns the tax. The tax rate is 6%” float calcTax(float subtotal) { cout << ”Enter subtotal: ”; cin >> subtotal; float tax = subtotal * 0.06; return tax; }

Detailed Designs: Inputs Input Arguments: Pitfall The cout/cin are not needed, because subtotal = value_of_Actual_Arg; is executed when the function is invoked. ... AND: it is somebody else’s job to “ask”...my job is to calculate tax!

Detailed Designs: Inputs User input: Look for phrases like: “asks the user” “gets from the user” “prompts the user” These are interpreted as a cin/cout pair.

Detailed Designs: Inputs User input: Example “Write a function that asks the user to enter their age...” Interpretation: void fun() { int age; cout << ”Enter your age: ”; cin >> age; }

Detailed Designs: Outputs Output back to the invoking function: Most functions send output back to the function which invoked them. Usually there is only one such output, but sometimes there are more, and sometimes there is none. Look for the phrase “... returns ...” or “... passes back” in the description.

Detailed Designs: Outputs Output back to the invoking function: When there is no value returned: - the return type is void When there is one value returned: - the return type is the type of the value returned. - use the return command to return the value. Where there is more than one value returned: - use a void return type - use a PBR argument for each value “returned”. - do not use the return command; instead: - set the value of each PBR argument to the “returned” value.

Detailed Designs: Outputs Output back to the invoking function: no value “Write a function that prints hello on the screen.” Note, there is no mention of “returns”, so use void: void sayHi() { cout << ”Hello”; }

Detailed Designs: Outputs Output back to the invoking function: 1 value “Write a function that asks the user to enter their age and returns the age entered.” int askAge() { int age; cout << ”Enter your age: ”; cin >> age; // age is int, so return age; // return type is int }

Detailed Designs: Outputs Output back to the invoking function: 2 values “Write a function that asks the user to enter their name and age and passes these back.” void askNameAndAge(string & n, int & a) { cout << ”Enter your name: ”; cin >> n; cout << ”Enter your age: ”; cin >> a; }

Detailed Designs: Outputs User Output: Look for the words: - prints - displays - shows These are interpreted as cout’s

Detailed Designs: Outputs User Output: Example "Write a function that is given a name and greets the user printing Hello followed by the name" void greet(string name) { cout << "Hello " << name; }

Detailed Designs: In/Out Args Input/Output Argument: An argument used to both: - input data from the invoking function - output data back to the invoking function by modifying the data.

Detailed Designs: In/Out Args Input/Output Argument: Look for the words "modifies", "changes", etc. in reference to something that "is given". Since an I/O Argument must send data back, they are always PBR

Detailed Designs: In/Out Args Example 1: Write a function called doubleIt that is given an integer and modifies it by multiplying it by two. void doubleIt(int & n) { n = n * 2; }

Detailed Designs: In/Out Args Example 2: Write a function called forceInRange that modifies an integer such that its value is between a given lower and upper ends of an range, such that: if the integer > upper, set it to upper if the integer < lower, set it to lower.

Detailed Designs: In/Out Args Example 2: void forceInRange(int & n, int low, int hi) { if (n > hi) n = hi; if (n < low) n = low; }

Detailed Designs: In/Out Args Example 3: Write a function called changeName that modifies a name by asking the user to enter a new value. Print the old name before the prompt. void changeName(string & name) { cout << "Old name = " << name; cout << "Enter new name: "; cin >> name; }

Detailed Designs: In/Out Args Example 4: Write a function called order2 that modifies two integers, when needed, such that the first is smaller than the second. void order2(int & a, int & b) { int t; if (a > b) { // swap a and b t = a; a = b; b = t; }