3.2 Predefined Functions Libraries. 3.1 Top Down Design. 3.2 Predefined Functions Libraries. 3.2 Function call. Display 3.

Size: px
Start display at page:

Download "3.2 Predefined Functions Libraries. 3.1 Top Down Design. 3.2 Predefined Functions Libraries. 3.2 Function call. Display 3."

Transcription

1 3.1 Top Down Design 3.2 Predefined Functions Libraries Step wise refinement, also known as divide and conquer, means dividing the problem into subproblems such that once each has been solved, the big problem is solved. The subproblems should be smaller and easier to understand and to solve than the big problem. C++ comes with libraries of predefined functions. How do we use predefined (or library) functions? Everything must be declared before it is used. The statement #include <file> brings the declarations of library functions into your program, so you can use the library functions Predefined Functions Libraries 3.2 Function call #include <cmath> // include declarations of math // library functions // include definitions of // iostream objects // make names available int main() cout << sqrt(3.0) << endl; // call math library function // sqrt with argument 3.0, send A function call is an expression consisting of a function name followed by arguments enclosed in parentheses. Multiple arguments are separated by commas. Syntax: FunctionName(Arg_List) where Arg_List is a comma separated list of arguments. Examples: side = sqrt(area); cout << 2.5 to the power 3.0 is << pow(2.5, 3.0); // the returned value to cout 3 4 Display 3.1 A Function call //Computes the size of a dog house that can be purchased //given the user s budget. #include <cmath> const double COST_PER_SQ_FT = 10.50; double budget, area, length_side; cout << "Enter the amount budgeted for your dog house $"; cin >> budget; Display 3.1 A Function call (b) cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "For a price of $" << budget << endl << "I can build you a luxurious square dog house\n" << "that is " << length_side << " feet on each side.\n"; area = budget/cost_per_sq_ft; length_side = sqrt(area); // the function call 5 6

2 PITFALL: Problems with Library functions 3.2 Predefined Functions Type changing functions Some compilers do not comply with the ISO Standard. If your compiler does not work with use #include <iostream.h> Similarly, for headers like cstdlib: use stdlib.h, and for cmath, use math.h Most compilers at least coexist with the headers without the.h, but some are hostile to these headers. 7 Question: 9/2 is an int, but we want the 4.5 floating point result. We want the integer part of some floating point number. How do we manage? Answer: Type casting, or type changing functions. C++ provides a function named double that takes a value of some other type and converts it to double. Example: int total, number; double ratio; // input total, number winnings = double(total) / number; 8 PITFALL: Integer division drops the fractional part function definitions REMEMBER: integer division returns just the quotient. The remainder (and any fractional part) are dropped. If the numerator and denominator are both integer values, the division is done as integer arithmetic, dropping the fractional part. Example: 11 / 2 is 5, NOT / 3 is 3, NOT Even if assigned to a double variable: double ratio = 10/3; // assigned value is 3, NOT You can define your own functions. You can put your functions in the same file as the main function or in a separate file. If you put your function after the function containing a call to your function, or in a separate file, you must put a prototype (defined in the next slide) for your function some place in the file where it is called, prior to the call. 10 function prototypes A function prototype tells you all the information you need to call the function. A prototype of a function (or its definition) must appear in your code prior to any call to the function. Syntax: Don t forget the semicolon Type_of_returned_value Function_Name(Parameter_list); Place prototype comment here. Parameter_list is a comma separated list of parameter definitions: type_1 param_1, type_2 param_2,. type_n param_n Example: double total_weight(int number, double weight_of_one); // Returns total weight of number of items that // each weigh weight_of_one 11 A function is like a small program To understand functions, keep these points in mind: A function definition is like a small program and calling the function is the same thing as running this small program. A function has formal parameters, rather than cin, for input. The arguments for the function are input and they are plugged in for the formal parameters. A function of the kind discussed in this chapter does not send any output to the screen, but does send a kind of output back to the program. The function returns a return-statement instead of cout-statement for output. 12

3 Display 3.3 A function Definition (Slide 1 of 2) Display 3.3 A function Definition (Slide 2 of 2) double total_cost(int number_par, double price_par); //Computes the total cost, including 5% sales tax, //on number_par items at a cost of price_par each. double price, bill; int number; cout << "Enter the number of items purchased: "; cin >> number; cout << "Enter the price per item $"; cin >> price; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << number << " items at " << "$" << price << " each.\n" << "Final bill, including tax, is $" << bill << endl; The function call 13 double total_cost(int number_par, double price_par) The function heading heading The function const double TAX_RATE = 0.05; //5% sales tax double subtotal; The function definition body subtotal = price_par * number_par; 14 Call-by-value Parameters Consider the function call: The values of the arguments number and price are plugged in for the formal parameters. This process is (A precise definition of what plugged means will be presented later. For now, we will use this simile.) A function of the kind discussed in this chapter does not send any output to the screen, but does send a kind of output back to the program. The function returns a return-statement instead of cout-statement for output. Alternate form for Function Prototypes The parameter names are not required: double total_cost(int number, double price); It is permissible to write: double total_cost(int, double ); Nevertheless, code should be readable to programmers as well as understandable by the compiler, so check for readability and chose to use parameter names when it increases readability. Function HEADERS (in the definition) must use parameter names. (There is an exception to this rule that we will not deal with in this course.) Anatomy of a Function Call (part 1 of 3) Anatomy of a Function Call (part 2 of 3) Review Display Before the function is called, the value of the variables number and price are set to 2 and 10.10, by cin. 1 The following statement, which includes a function call, begins executing: 2 The value of number (which is 2) is plugged in for number_par and the value of price (which is 10.10) is plugged for price_par: double total_cost(int number_par, double price_par) const double TAX_RATE = 0.05; //5% sales tax double subtotal; subtotal = price_par * number_par; 17 The execution produces the following effect: double total_cost(int 2, double 10.10) const double TAX_RATE = 0.05; //5% sales tax double subtotal; subtotal = * 2; 18

4 Anatomy of a Function Call (part 3 of 3) 3 The body of the function is executed, i.e., the following is executed: const double TAX_RATE = 0.05; //5% sales tax double subtotal; subtotal = * 2; 4 When the return statement is executed, the value of the expression after the return is the value returned by the function. In this case, when is executed, the value of (subtotal + subtotal*tax_rate), which is is returned by the function call the value of bill is set to when the following statement ends: 19 PITFALL Arguments in the wrong order When a function is called, C++ substitutes the first argument given in the call for the first parameter in the definition, the second argument for the second parameter, and so on. There is no check for reasonableness. The only things checked are: i) that there is agreement of argument type with parameter type and ii) that the number of arguments agrees with the number of parameters. If you do not put correct arguments in call in the correct order, C++ will happily assign the wrong arguments to the right parameters. 20 Summary of Syntax for a Function that Returns a Value. 3.4 Procedural Abstraction Principle of Information Hiding body Function Prototype: Type_Returned Function_Name(Parameter_List); Prototype Comment function header Function Definitions Type_Returned Function_Name(Parameter_List) Declaration_1 Declaration_2... Must include one or Declaration_Last; more return statements. Executable_1; Executable_2;... Executable_Last David Parnas, in 1972 stated the principle of information hiding. A function s author (programmer) should know everything about how the function does its job, but nothing but specifications about how the function will be used. The client programmer -- the programmer who will call the function in her code -- should know only the function specifications, but nothing about how the function is implemented Procedural Abstraction The Black Box Analogy Based on the Principle of Information Hiding, the text describes the BLACK BOX analogy. When using a function, we behave as if we know nothing about how the function does its job, that we know only the specifications for the function. When writing a function, we behave as if we know nothing but the specifications about how the function is to be used. In this way, we avoid writing either application code that depends on the internals of the function or writing function code that depends in some way on the internal structure of the application Procedural Abstraction The Principle When applied to a function definition, the principle of procedural abstraction means that your function should be written so that it can be used like a black box. This means the user of a function should not need to look at the internals of the function to see how the function works. The function prototype and accompanying commentary should provide enough information for the programmer to use the function. To ensure that your function definitions have this property, you should adhere to these rules: The prototype comment should tell the programmer any and all conditions that are required of the arguments to the function and should describe the value returned by the function. All variables used in the function body should be declared in the function body. (The formal parameters are already declared in the function header.) 24

5 Global Constants and Global Variables Variables declared within the body of a function definition are said to be local to that function, or have that function s block as their scope. Variables declared within the body of the main function are said to be local to the main function, or to have that function s block as their scope. When we say a function is a local variable without further mention of a function, we mean that variable is local to some function definition. If a variable is local to a function, you can have another variable with the same name that is local to either main or some other function. 25 A named constant declared outside the body of any function definition is said to be a global named constant. A global named constant can be used in any function definition that follows the constant declaration. There is an exception to this we will point out later. Variables declared outside the body any function is said to be global variables or to have global scope. Such a variable can be used in any function definition that follows the constant declaration. There is also an exception to this we will point out later. Use of global variables ties functions that use the global variables in a way that makes understanding the functions individually nearly impossible. There is seldom any reason to use global variables. 26 Display 3.11 Global Named Constants (Part 1 of 2) Display 3.11 Global Named Constants (Part 2 of 2) //Computes the area of a circle and the volume of a sphere. //Uses the same radius for both calculations. #include <cmath> const double PI = ; double area(double radius); //Returns the area of a circle with the specified radius. double volume(double radius); //Returns the volume of a sphere with the specified radius. double radius_of_both, area_of_circle, volume_of_sphere; cout << "Enter a radius to use for both a circle\n" << "and a sphere (in inches): "; cin >> radius_of_both; 27 area_of_circle = area(radius_of_both); volume_of_sphere = volume(radius_of_both); cout << "Radius = " << radius_of_both << " inches\n" << "Area of circle = " << area_of_circle << " square inches\n" << "Volume of sphere = " << volume_of_sphere << " cubic inches\n"; double area(double radius) return (PI * pow(radius, 2)); double vo lume(double radius) return ((4.0/3.0) * PI * pow(radius, 3)); 28 Call-by-value Formal Parameters are Local Variables Formal parameters are more than just blanks to be filled in with values from arguments. We promised a more precise discussion of what value plugged in for the argument means. Here is that discussion. Formal parameters are actually local variables that are initialized by the call mechanism to the value of the argument. They may be used in any way that a local variable can be used. Display 3.12 (next slide) is an example of a formal parameter used as a local variable. 29 Display 3.12 Formal Parameter Used as Local Variable (Part 1 of 2) //Law office billing program. const double RATE = ; //Dollars per quarter hour. double fee(int hours_worked, int minutes_worked); //Returns the charges for hours_worked hours and //minutes_worked minutes of legal services. int hours, minutes; double bill; cout << "Welcome to the offices of\n" << "Dewey, Cheatham, and Howe.\n" << "The law office with a heart.\n" << "Enter the hours and minutes" << " of your consultation:\n"; cin >> hours >> minutes; 30

6 Display 3.12 Formal Parameter Used as Local Variable (Part 2 of 2) Namespaces Revisited (1 of 2) bill = fee(hours, minutes); The value of minutes is not changed by a call to fee. cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "For " << hours << " hours and " << minutes << " minutes, your bill is $" << bill << endl; minutes_worked is a local variable initialized to the value of minutes double fee(int hours_worked, int minutes_worked) int quarter_hours; minutes_worked = hours_worked*60 + minutes_worked; quarter_hours = minutes_worked/15; return (quarter_hours*rate); 31 All our use of namespaces has amounted to While this is correct, we are sidestepping the reason namespaces were introduced into C++, though we have done this for good teaching reasons. In short, we have been polluting the global namespace. So long as our programs are small, so this is not a problem. This won t always be the case, so you should learn to put the using directive in the proper place. 32 Namespaces Revisited (2 of 2) Placing a using directive anywhere is analogous to putting all the definitions from the namespace there. This is why we have been polluting the global namespace. We have been putting all the namespace std names from our header file in the global namespace. The rule, then is: Place the namespace directive, inside the block where the names will be used. The next slide is Display 3.13 with the namespace directive place correctly. 33 Display 3.13 Using Namespaces (1 of 2) //Computes the area of a circle and the volume of a sphere. //Uses the same radius for both calculations. #include <cmath>//some compilers may use math.h instead of cmath. const double PI = ; double area(double radius); //Returns the area of a circle with the specified radius. double volume(double radius); //Returns the volume of a sphere with the specified radius. double radius_of_both, area_of_circle, volume_of_sphere; cout << "Enter a radius to use for both a circle\n" << "and a sphere (in inches): "; cin >> radius_of_both; 34 Display 3.13 Using Namespaces (1 of 2) area_of_circle = area(radius_of_both); volume_of_sphere = volume(radius_of_both); cout << "Radius = " << radius_of_both << " inches\n" << "Area of circle = " << area_of_circle << " square inches\n" << "Volume of sphere = " << volume_of_sphere << " cubic inches\n"; C++ distinguishes two functions by examining the function name and the argument list for number and type of arguments. The function that is chosen is the function with the same number of parameters as the number of arguments and and that matches the types of the parameter list sufficiently well. This means you do not have to generate names for functions that have very much the same task, but have different types. double area(double radius) The behavior of this program is exactly the same as that of Display 3.11 return (PI * pow(radius, 2)); double volume(double radius) return ((4.0/3.0) * PI * pow(radius, 3)); 35 36

7 Display 3.15 Overloading a Function Name (1 of 2) Display 3.15 Overloading a Function Name (2 of 2) //Illustrates overloading the function name ave. double ave(double n1, double n2); //Returns the average of the two numbers n1 and n2. double ave(double n1, double n2, double n3); //Returns the average of the three numbers n1, n2, and n3. cout << "The average of 2.0, 2.5, and 3.0 is " << ave(2.0, 2.5, 3.0) << endl; double ave(double n1, double n2) Both these functions have the same name, but have parameter return ((n1 + n2)/2.0); lists that are have different numbers of parameters. double ave(double n1, double n2, double n3) return ((n1 + n2 + n3)/3.0); cout << "The average of 4.5 and 5.5 is " << ave(4.5, 5.5) << endl; Automatic Type Conversion We pointed out that when overloading function names, the C++ compiler compares the number and sequence of types of the arguments to the number and sequence of types for candidate functions. In choosing which of several candidates for use when overloading function names, the compiler will choose an exact match if one if available. An integral type will be promoted to a larger integral type if necessary to find a match. An integral type will be promoted to a floating point type if necessary to get a match. 39 Display 3.16 Overloading a function name (1 of 4) //Determines whether a round pizza or a rectangular pizza is the best buy. double unitprice(int diameter, double price); //Returns the price per square inch of a round pizza. //The formal parameter named diameter is the diameter of the pizza //in inches. The formal parameter named price is the price of the pizza. double unitprice(int length, int width, double price); //Returns the price per square inch of a rectangular pizza //with dimensions length by width inches. //The formal parameter price is the price of the pizza. int diameter, length, width; double price_round, unit_price_round, price_rectangular, unitprice_rectangular; 40 Display 3.16 Overloading a function name (2 of 4) cout << "Welcome to the Pizza Consumers Union.\n"; cout << "Enter the diameter in inches" << " of a round pizza: "; cin >> diameter; cout << "Enter the price of a round pizza: $"; cin >> price_round; cout << "Enter length and width in inches\n" << "of a rectangular pizza: "; cin >> length >> width; cout << "Enter the price of a rectangular pizza: $"; cin >> price_rectangular; unitprice_rectangular = unit_price_round = cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); unitprice(length, width, price_rectangular); unitprice(diameter, price_round); 41 Display 3.16 Overloading a function name (3 of 4) cout << endl << "Round pizza: Diameter = " << diameter << " inches\n" << "Price = $" << price_round << " Per square inch = $" << unit_price_round << endl << "Rectangular pizza: length = " << length << " inches\n" << "Rectangular pizza: Width = " << width << " inches\n" << "Price = $" << price_rectangular << " Per square inch = $" << unitprice_rectangular << endl; if (unit_price_round < unitprice_rectangular) cout << "The round one is the better buy.\n"; else cout << "The rectangular one is the better buy.\n"; cout << "Buon Appetito!\n"; 42

8 Chapter Summary( 1 of 2) Chapter Summary (2 of 2) A good plan of attack on a problem is to decompose the problem into smaller, more accessible problems, the decompose these into still more manageable problems. This is known as Top-Down Design. A function that returns value is like a small program. Arguments provide input and the return value provides the output. When a subtask for a program takes some values as input and produces a single value as its only output, then that subtask can be implemented as a function. A function should be defied so that it can be used as a black box. The program author should know no more than the specification of the client use, and the client author should know nothing more than the specification of the implementation of the function. This rule is the principle of procedural abstraction. A variable that is defined in a function is said to be local to the function. Global named constants are declared using the const modifier. Declarations of global named constants are normally placed at the start of a program after the include directives, before the function prototypes. Call by value formal parameters (the only kind we have discussed so far) are local variables to the function. Occasionally a formal parameter may be useful as a local variable. When two or more function definitions have the same name, this is called function name overloading. When you overload a name, the function definitions must have different numbers of formal parameters or formal parameters of different types

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design. Chapter 4 Procedural Abstraction and Functions That Return a Value Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables

More information

Chapter 4. Procedural Abstraction and Functions That Return a Value

Chapter 4. Procedural Abstraction and Functions That Return a Value Chapter 4 Procedural Abstraction and Functions That Return a Value Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables

More information

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables

More information

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value 1 Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions 4.4 Procedural Abstraction 4.5 Local Variables

More information

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names Chapter 4 In this chapter, you will learn about: Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names Top-Down Design Top-Down

More information

Procedural Abstraction and Functions That Return a Value. Savitch, Chapter 4

Procedural Abstraction and Functions That Return a Value. Savitch, Chapter 4 Procedural Abstraction and Functions That Return a Value Savitch, 2007. Chapter 4 1 Procedural Abstraction: Functions I Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction

More information

Programmer-Defined Functions

Programmer-Defined Functions Functions Programmer-Defined Functions Local Variables in Functions Overloading Function Names void Functions, Call-By-Reference Parameters in Functions Programmer-Defined Functions function declaration

More information

Function Call Example

Function Call Example Function Call Example A Function Call Example (1) ch 3-25 A Function Call Example (2) ch 3-26 Alternative Function Declaration Recall: Function declaration is "information for compiler Compiler only needs

More information

CSCI 1061U Programming Workshop 2. Function Basics

CSCI 1061U Programming Workshop 2. Function Basics CSCI 1061U Programming Workshop 2 Function Basics 1 Learning Objectives Predefined Functions Those that return a value and those that don t Programmer-defined Functions Defining, Declaring, Calling Recursive

More information

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Functions Read/Study: Reek Chapters 7 Gojko Babić 01-22-2018 Predefined Functions C comes with libraries of predefined functions E.g.:

More information

Chapter 3 Function Basics

Chapter 3 Function Basics Chapter 3 Function Basics Learning Objectives Predefined Functions Those that return a value and those that don t Programmer-defined Functions Defining, Declaring, Calling Recursive Functions Scope Rules

More information

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary Topic 2 1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary Arithmetic Operators C++ has the same arithmetic operators as a calculator:

More information

C++ Basics - 3 Rahul

C++ Basics - 3 Rahul C++ Basics - 3 Rahul Deodhar @rahuldeodhar www.rahuldeodhar.com rahuldeodhar@gmail.com Topics for today Func@ons Classwork Topics for today Homework Program Others Procedural Abstrac@on & Func@ons Top

More information

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout

More information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,

More information

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions. 6 Functions TOPICS 6.1 Focus on Software Engineering: Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5 Passing Data by Value 6.6 Focus

More information

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Review. Modules. CS 151 Review #6. Sample Program 6.1a: Review Modules A key element of structured (well organized and documented) programs is their modularity: the breaking of code into small units. These units, or modules, that do not return a value are called

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92 L E S S O N S E T 6.2 Functions that Return a Value PURPOSE PROCEDURE 1. To introduce the concept of scope 2. To understand the difference between static, local and global variables 3. To introduce the

More information

Programming Language. Functions. Eng. Anis Nazer First Semester

Programming Language. Functions. Eng. Anis Nazer First Semester Programming Language Functions Eng. Anis Nazer First Semester 2016-2017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities

More information

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School Objectives To understand the concepts of modularity and bottom up testing. To be aware of the use of structured

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

More information

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information. Chapter 3 Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus Lecture 03 - Introduction To Functions Christopher M. Bourke cbourke@cse.unl.edu 3.1 Building Programs from Existing

More information

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Copyright 2011 Pearson Addison-Wesley. All rights

More information

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved Chapter 1 C++ Basics Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment

More information

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM Contents 1. Statements 2. C++ Program Structure 3. Programming process 4. Control Structure STATEMENTS ASSIGNMENT STATEMENTS Assignment statement Assigns a value

More information

This watermark does not appear in the registered version - Slide 1

This watermark does not appear in the registered version -   Slide 1 Slide 1 Chapter 1 C++ Basics Slide 2 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output Program Style

More information

Programming with C++ as a Second Language

Programming with C++ as a Second Language Programming with C++ as a Second Language Week 2 Overview of C++ CSE/ICS 45C Patricia Lee, PhD Chapter 1 C++ Basics Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Introduction to

More information

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Stephen Scott (Adapted from Christopher M. Bourke) 1 / 41 Fall 2009 Chapter 3 3.1 Building Programs from Existing Information

More information

C++ Programming Lecture 11 Functions Part I

C++ Programming Lecture 11 Functions Part I C++ Programming Lecture 11 Functions Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Introduction Till now we have learned the basic concepts of C++. All the programs

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Functions Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Chapter 3 3.1 Building

More information

Chapter Two: Fundamental Data Types

Chapter Two: Fundamental Data Types Chapter Two: Fundamental Data Types Slides by Evan Gallagher Chapter Goals To be able to define and initialize variables and constants To understand the properties and limitations of integer and floating-point

More information

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5. Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.1) CS 1428 Fall 2014 Jill Seaman 1 2.14 Arithmetic Operators An operator is a symbol that tells the computer to perform specific

More information

Problem Solving with C++

Problem Solving with C++ GLOBAL EDITION Problem Solving with C++ NINTH EDITION Walter Savitch Kendrick Mock Ninth Edition PROBLEM SOLVING with C++ Problem Solving with C++, Global Edition Cover Title Copyright Contents Chapter

More information

Announcements. Lab 1 this week! Homework posted Wednesday (late)

Announcements. Lab 1 this week! Homework posted Wednesday (late) C++ Basics Announcements Lab 1 this week! Homework posted Wednesday (late) Avoid errors To remove your program of bugs, you should try to test your program on a wide range of inputs Typically it is useful

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

More information

LAB: INTRODUCTION TO FUNCTIONS IN C++

LAB: INTRODUCTION TO FUNCTIONS IN C++ LAB: INTRODUCTION TO FUNCTIONS IN C++ MODULE 2 JEFFREY A. STONE and TRICIA K. CLARK COPYRIGHT 2014 VERSION 4.0 PALMS MODULE 2 LAB: FUNCTIONS IN C++ 2 Introduction This lab will provide students with an

More information

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

Chapter 2: Overview of C++

Chapter 2: Overview of C++ Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman C++ Background Introduced by Bjarne Stroustrup of AT&T s Bell Laboratories in

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.

More information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents: Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language Dr. Amal Khalifa Lecture Contents: Introduction to C++ Origins Object-Oriented Programming, Terms Libraries and Namespaces

More information

Input and Expressions Chapter 3 Slides #4

Input and Expressions Chapter 3 Slides #4 Input and Expressions Chapter 3 Slides #4 Topics 1) How can we read data from the keyboard? 2) How can we calculate values? 3) How can we manage the type of a value? 4) How can we round or get random numbers?

More information

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, 2 0 1 0 R E Z A S H A H I D I Today s class Constants Assignment statement Parameters and calling functions Expressions Mixed precision

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,

More information

Chapter 3. Numeric Types, Expressions, and Output

Chapter 3. Numeric Types, Expressions, and Output Chapter 3 Numeric Types, Expressions, and Output 1 Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and Explicit Type Conversion Calling a Value-Returning

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

The C++ Language. Arizona State University 1

The C++ Language. Arizona State University 1 The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

Computing and Statistical Data Analysis Lecture 3

Computing and Statistical Data Analysis Lecture 3 Computing and Statistical Data Analysis Lecture 3 Type casting: static_cast, etc. Basic mathematical functions More i/o: formatting tricks Scope, namspaces Functions 1 Type casting Often we need to interpret

More information

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type- Lecture 3 Input and Output Review from last week Variable - place to store data in memory identified by a name should be meaningful Has a type- int double char bool Has a value may be garbage change value

More information

Introduction to Programming EC-105. Lecture 2

Introduction to Programming EC-105. Lecture 2 Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically

More information

LECTURE 02 INTRODUCTION TO C++

LECTURE 02 INTRODUCTION TO C++ PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION

More information

Chapter 2 C++ Fundamentals

Chapter 2 C++ Fundamentals Chapter 2 C++ Fundamentals 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates Goals Reuse existing code in your programs with #include Obtain input data from the user

More information

H.O.#2 Fall 2015 Gary Chan. Overview of C++ Programming

H.O.#2 Fall 2015 Gary Chan. Overview of C++ Programming H.O.#2 Fall 2015 Gary Chan Overview of C++ Programming Topics C++ as a problem-solving tool Introduction to C++ General syntax Variable declarations and definitions Assignments Arithmetic operators COMP2012H

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

On a 64-bit CPU. Size/Range vary by CPU model and Word size. On a 64-bit CPU. Size/Range vary by CPU model and Word size. unsigned short x; //range 0 to 65553 signed short x; //range ± 32767 short x; //assumed signed There are (usually) no unsigned floats or doubles.

More information

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski) Class 2: Variables and Memory Variables A variable is a value that is stored in memory It can be numeric or a character C++ needs to be told what type it is before it can store it in memory It also needs

More information

Understanding main() function Input/Output Streams

Understanding main() function Input/Output Streams Understanding main() function Input/Output Streams Structure of a program // my first program in C++ #include int main () { cout

More information

Expressions, Input, Output and Data Type Conversions

Expressions, Input, Output and Data Type Conversions L E S S O N S E T 3 Expressions, Input, Output and Data Type Conversions PURPOSE 1. To learn input and formatted output statements 2. To learn data type conversions (coercion and casting) 3. To work with

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 6: User-Defined Functions I In this chapter, you will: Objectives Learn about standard (predefined) functions and discover

More information

Announcements. Lab 1 this week! Homework posted Thursday -Due next Thursday at 11:55pm

Announcements. Lab 1 this week! Homework posted Thursday -Due next Thursday at 11:55pm C++ Basics Announcements Lab 1 this week! Homework posted Thursday -Due next Thursday at 11:55pm Variables Variables are objects in program To use variables two things must be done: - Declaration - Initialization

More information

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School Modular programming Program Design Stepwise refinement of main tasks into subtasks. Modules or subprograms that

More information

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Functions. Lecture 6 COP 3014 Spring February 11, 2018 Functions Lecture 6 COP 3014 Spring 2018 February 11, 2018 Functions A function is a reusable portion of a program, sometimes called a procedure or subroutine. Like a mini-program (or subprogram) in its

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6 Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #5 due today Lab #3

More information

4. Structure of a C++ program

4. Structure of a C++ program 4.1 Basic Structure 4. Structure of a C++ program The best way to learn a programming language is by writing programs. Typically, the first program beginners write is a program called "Hello World", which

More information

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */ cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */ #include #include /* The above include is present so that the return type

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines

More information

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords. Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object

More information

Introduction To Computer Programming C++ Mr. Clausen Program C11A, C11B

Introduction To Computer Programming C++ Mr. Clausen Program C11A, C11B Introduction To Computer Programming C++ Mr. Clausen Program C11A, C11B Program 11A: Cone Heads with Functions 30 points Write a program that calculates the volume of a right circular cone using functions.

More information

Lecture 4 Tao Wang 1

Lecture 4 Tao Wang 1 Lecture 4 Tao Wang 1 Objectives In this chapter, you will learn about: Assignment operations Formatting numbers for program output Using mathematical library functions Symbolic constants Common programming

More information

Introduction. What is function? Multiple functions form a larger program Modular programming

Introduction. What is function? Multiple functions form a larger program Modular programming FUNCTION CSC128 Introduction What is function? Module/mini program/sub-program Each function/module/sub-program performs specific task May contains its own variables/statements Can be compiled/tested independently

More information

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

VARIABLES & ASSIGNMENTS

VARIABLES & ASSIGNMENTS Fall 2018 CS150 - Intro to CS I 1 VARIABLES & ASSIGNMENTS Sections 2.1, 2.2, 2.3, 2.4 Fall 2018 CS150 - Intro to CS I 2 Variables Named storage location for holding data named piece of memory You need

More information

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8 More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #7 due today Lab #4 is due on Monday at 8:00

More information

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved Chapter Five: Functions by Cay Horstmann Chapter Goals To be able to implement functions To become familiar with the concept of parameter passing To appreciate the importance of function comments To develop

More information

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage: Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

More information

(created by professor Marina Tanasyuk) FUNCTIONS

(created by professor Marina Tanasyuk) FUNCTIONS FUNCTIONS (created by professor Marina Tanasyuk) In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

Chapter Four: Loops II

Chapter Four: Loops II Chapter Four: Loops II Slides by Evan Gallagher & Nikolay Kirov Chapter Goals To understand nested loops To implement programs that read and process data sets To use a computer for simulations Processing

More information

Functions, Arrays & Structs

Functions, Arrays & Structs Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier

More information

Review of Important Topics in CS1600. Functions Arrays C-strings

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

From Pseudcode Algorithms directly to C++ programs

From Pseudcode Algorithms directly to C++ programs From Pseudcode Algorithms directly to C++ programs (Chapter 7) Part 1: Mapping Pseudo-code style to C++ style input, output, simple computation, lists, while loops, if statements a bit of grammar Part

More information