CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

Similar documents
Lab Instructor : Jean Lai

Using Free Functions

Programming Language. Functions. Eng. Anis Nazer First Semester

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

4. C++ functions. 1. Library Function 2. User-defined Function

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Introduction to Programming

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

CS3157: Advanced Programming. Outline

Functions. Lecture 6 COP 3014 Spring February 11, 2018

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Chapter 6 - Notes User-Defined Functions I

C++ Programming Lecture 11 Functions Part I

Part II Answer to all the questions (2 Marks):

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

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

Unit 6. Thinking with Functions

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

Chapter 3 - Functions

Downloaded from

Ch 6. Functions. Example: function calls function

Computing and Statistical Data Analysis Lecture 3

CSCE 110 PROGRAMMING FUNDAMENTALS

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

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

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

System Design and Programming II

Functions and Recursion

LAB: INTRODUCTION TO FUNCTIONS IN C++

What we will learn about this week:

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

Split up Syllabus (Session )

2. Functions I: Passing by Value

A SHORT COURSE ON C++

Strings and Library Functions

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

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

Chapter 10 Characters, Strings, and the string class

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

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

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

C++ Programming: Functions

C Programming for Engineers Functions

BITG 1113: Function (Part 2) LECTURE 5

A. Introduction to Function 1. Modular Programming input processing output functions library functions 2. Function 1. Benefit of Using Functions

Lecture 04 FUNCTIONS AND ARRAYS

today cs3157-fall2002-sklar-lect05 1

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

CSCE 206: Structured Programming in C++

(created by professor Marina Tanasyuk) FUNCTIONS

do { statements } while (condition);

C++ For Science and Engineering Lecture 15

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

3.1. Chapter 3: The cin Object. Expressions and Interactivity

CS2141 Software Development using C/C++ C++ Basics

Local and Global Variables

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

Chapter 6: Functions

Chapter 10: Characters, C- Strings, and More About the string Class

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

CSc Introduction to Computing

Engineering Problem Solving with C++, Etter/Ingber

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

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

Introduction to C++ Systems Programming

EECS402 Lecture 02. Functions. Function Prototype

Chapter 5 C Functions

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

Chapter 7 - Notes User-Defined Functions II

C Functions. 5.2 Program Modules in C

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

CSCE 110 PROGRAMMING FUNDAMENTALS

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Chapter 10: Characters, C- Strings, and More About the string Class Character Testing

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Objectives. In this chapter, you will:

LECTURE 06 FUNCTIONS

Chapter 15 - C++ As A "Better C"

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

CSCE Practice Midterm. Data Types

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Looping. Arizona State University 1

a. a * c - 10 = b. a % b + (a * d) + 7 =

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Transcription:

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++ Spring 2012 Instructor : K. Auyeung Email Address: Course Page: Class Hours: kenny@sci.brooklyn.cuny.edu hbp://www.sci.brooklyn.cuny.edu/~kenny/cisc1110 MW 2:40 4:45PM 4428N

Agenda funcqons built- in/library funcqons: cmath cctype User defined funcqons review Call by Value Call by Reference Summary

Library FuncQons we have already used some library funcqons iostream C++ library: iostream.cout iostream.cin string C++ library: string.length() string.find() string.replace() string.insert() stdlib C library: srand() rand()

cmath library there is a standard library of useful math funcqons defined in C that is commonly used in C++ the header file: #include <cmath> using namespace std; these include, for example: double sqrt( double x ) double pow( double x, double y ) double sin( double x ) these take arguments and return values, e.g.: double f = sqrt( 4.0 ); many other funcqons are defined in math.h, including trigonometry funcqons (like sin, cos, tan), and constants like MATH_PI on- line reference for cmath: hbp://www.cplusplus.com/reference/clibrary/ cmath/

example: #include <iostream>! #include <cmath>! using namespace std;! int main() { int x1 = 7, y1 = 5; int x2 = 1, y2 = 3; double dist;! dist = sqrt ( pow((double)(x2-x1),2) + pow((double)(y2-y1),2) );! cout << "the distance from point (" << x1 << "," << y1 << ") "!! << "to point (" << x2 << "," << y2 << ") is " << dist << endl;! return 0;! } // end of main()!

there is a standard library of useful character funcqons defined in C that is commonly used in C++ the header file: #include <cctype>! using namespace std;! int isalnum( int c )! int isalpha( int c )! int isdigit( int c )! int islower( int c )! int ispunct( int c )! int isupper( int c )! int tolower( int c )! int toupper( int c )! checks if character argument is alphanumeric checks if character argument is alphabeqc checks if character argument is a decimal digit checks if character argument is a lowercase leber checks if character argument is a punctuaqon character checks if character argument is an uppercase leber converts uppercase leber argument to lowercase converts lowercase leber argument to uppercase

on- line reference for cctype: hbp://www.cplusplus.com/reference/clibrary/cctype/ example: #include <iostream>! #include <cctype>! using namespace std;! int main() {! bool q = false;! char c;! while (! q ) {! cout << "enter a character (q to quit): ";! cin >> c; cout << "you entered: " << c << endl; if ( islower( c )) {! c = toupper( c );!! }! cout << "upper case = " << c << endl;! q = ( c == Q );! } // end while! return 0;! } // end of main()

WriQng Your Own FuncQons modularity we can divide up a program into small, understandable pieces (kind of like steps in a recipe) this makes the program easier to read and easier to debug (i.e., check and see if it works, and fix it if it doesn t work) write once, use many Qmes if we have a task that will be performed many Qmes, we only have to define a funcqon once; then we can call (or invoke) the funcqon as many Qmes as we need it also, we can use parameters (or arguments) to use the funcqon to perform the same task on or with different data values

How FuncQons Work how funcqons work funcqons must be declared and defined before they can be called (or invoked ); first you can declare a funcqon prototype (or header ) and then later list the funcqon definiqon; you can invoke the funcqon anywhere in the code aler you have listed the prototype example: #include <iostream>! using namespace std;! void sayhello(); // declare function (prototype or header)! int main() { sayhello(); // call function return 0;! return 0;! } // end of main()! void sayhello() { // define function! cout << "hello\n";! } // end of sayhello()!

User Defined FuncQons Review Two components to user defined funcqons FuncQon declaraqon (prototype) FuncQon definiqon

FuncQon Prototype The funcqon heading without the body of the funcqon [return type] functionname (parameter list); Parameters are variables which provide input to a funcqon [data type] identifier, [data type] identifier,

FuncQon DefiniQon Header Body Data type return or void FuncQon name Parameter (formal parameter) list The set of statements that consqtutes the funcqon The body is wriben within braces { } Uses a return statement to return a value to the calling funcqon

User Defined Example #include <iostream> using namespace std; // Declares function void whoami(); //function prototype int main() { whoami(); } return 0; void whoami() { // defines function whoami } cout << endl << "My name is John" << endl;

Call by Value Each argument in a funcqon call is evaluated and copied to the variables specified in the formal parameters list #include <iostream> using namespace std; // Declares function int square(int); //function prototype int main() { int n = 5; int answer = square(n); cout << The answer is << answer; return 0; } int square(int number) { // defines function square } int result = number * number; return result;

Call by Value ConQnued #include <iostream> using namespace std; // Declares function int square(int); int main() { int n = 5; int answer = square(n); cout << The answer is << answer; return 0; } n main 5 square int square(int number) { } int result = number * number; return result; number result 5 25

Call by Reference The address of the memory locaqon of an argument (actual parameter) is passed to the funcqon This allows a funcqon to change the values of arguments Why?! If the value of the argument needs to be changed If you want to return more than one value from the funcqon Passing a memory address can save Qme and memory

Call by Reference ConQnued Reference parameter a formal parameter that receives a memory address The funcqon prototype [return type] functionname (parameter list); Parameters are variables which provide input to a funcqon [data type] identifier[&], [data type] identifier[&],

Call by Reference Example // see reference.cpp #include <iostream> using namespace std; //Function Prototypes void sum(int, int, int&); int main() { } int a = 5, b = 3, answer; sum(a, b, answer); cout << a << " + " << b << " = " << answer; return 0; void sum(int num1, int num2, int& solution) { a b answer num1 num2 soluqon main 5 3 8 sum 5 3 } solution = num1 + num2;

Summary Call by Value - Each argument in a funcqon call is evaluated and copied to the variables specified in the formal parameters list Call by Reference - The address of the memory locaqon of an argument (actual parameter) is passed to the funcqon