Implementing Functions from a Detailed Design Quick Tips

Slides:



Advertisements
Similar presentations
Adding Dynamic Content to your Web Site
Advertisements

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() {
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
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.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
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.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
1 Original Source : and Problem and Problem Solving.ppt.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
1 CS161 Introduction to Computer Science Topic #9.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
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)
CMSC 104, Version 8/061L16IncrementalProg.ppt Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading.
Basic concepts of C++ Presented by Prof. Satyajit De
C++ First Steps.
Structures and Classes
Programming what is C++
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
Introduction to C Language
Functions, locals, parameters, and separate compilation
Objectives Identify the built-in data types in C++
CO1401 Programming Design and Implementation
Variables, Expressions, and IO
UMBC CMSC 104 – Section 01, Fall 2016
Implementing Functions from a Detailed Design Quick Tips
Returning Structures Lesson xx
User Defined Functions
Exceptions with Functions
Value returning Functions
Incremental Programming
Functions.
Programming Funamental slides
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Selection Control Structure
Programming We have seen various examples of programming languages
Input from the Console This video shows how to do CONSOLE input.
Topics Designing a Program Input, Processing, and Output
CS150 Introduction to Computer Science 1
The Function Prototype
Functions Divide and Conquer
Flowcharts and Pseudo Code
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
Input Based Programs.
Chapter 8 Functions.
Strings …again.
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.
CS31 Discussion 1D Fall18: week 2
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

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; }