A Computer Science Tapestry 2.1 Writing and Understanding C++ l Writing programs in any language requires understanding the syntax and semantics of the.

Slides:



Advertisements
Similar presentations
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Advertisements

Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Chapter 6: User-Defined Functions I
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 6: User-Defined Functions I
Basic Elements of C++ Chapter 2.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
COMPUTER SCIENCE I C++ INTRODUCTION
Genome Revolution: COMPSCI 004G 3.1 What is a program? What is code? l Instructions in a language a computer executes  Languages have different characteristics,
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
1 C++ Syntax and Semantics, and the Program Development Process.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
A Computer Science Tapestry 3.1 Programs that Respond to Input l Programs in chapters one and two generate the same output each time they are executed.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
CS201 Introduction to Sabancı University 1 Announcements l Midterm Dates ä First Midterm Exam: November 7, 2015, Saturday, 14:00 – 16:00 ä.
Genome Revolution: COMPSCI 004G 2.1 Bioinformatics Vocabulary l Processing, analyzing, experimenting with data  Where does the data come from?  How do.
A Computer Science Tapestry 2.1 Writing and Understanding C++ l There are language independent skills in programming (C++, Java, …) l However, writing.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Compsci 06/101, Fall Vocabulary l What's the Spanish word for 'boy'? 'eat'?  How do you know? l What about the Turkish equivalents?  How do.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
A Computer Science Tapestry 3.1 Programs that Respond to Input l Programs in chapters one and two generate the same output each time they are executed.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
CSE1222: Lecture 1The Ohio State University1. Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
A Sample Program #include using namespace std; int main(void) { cout
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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6: User-Defined Functions I
Topic Pre-processor cout To output a message.
Announcements Homework 1 is due on February 22nd, this Wednesday, at 19:00 Submit to SUCourse Strictly follow the submission guideline provided in the.
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Announcements Homework 1 is due on October 4th, this Wednesday, at 19:00 Submit to SUCourse Strictly follow the submission guideline provided in the homework.
Announcements General rules about homeworks
Announcements General rules about homeworks
Course websites CS201 page link at my website: Lecture slides
Chapter 6: User-Defined Functions I
Announcements General rules about homeworks
Programs that Respond to Input
Writing and Understanding C++
Announcements Homework 1 will be assigned this week
Presentation transcript:

A Computer Science Tapestry 2.1 Writing and Understanding C++ l Writing programs in any language requires understanding the syntax and semantics of the programming language as well as language-independent skills in programming. ä Syntax is similar to rules of spelling and grammar: i before e except after c The relationship between a command and a quote, “this is a fact,” or “this is a fact”, ä Semantics is what a program (or English sentence) means You ain’t nothing but a hound dog. La plume de ma tante est sur la porte. l At first it seems like the syntax is hard to master, but the semantics are much harder ä Natural languages are more forgiving than programming languages.

A Computer Science Tapestry 2.2 Toward an Understanding of C++ l Traditional first program, doesn’t convey power of computing but it illustrates basic components of a simple program #include using namespace std; // traditional first program int main() { cout << " Hello world " << endl; return 0; } l This program must be edited/typed, compiled, linked and executed. l Other languages don’t use compile/link phase, examples?

A Computer Science Tapestry 2.3 Anatomy of a C++ Program #include statements make libraries of classes and functions accessible to the program ä Compiler needs access to interface, what the functions look like, but not to implementation ä Linker/Loader needs access to implementations ä Helps programmers develop code independently l Comments make programs readable by humans ä The cost of program maintenance is often far greater than the cost of program development ä Use comments liberally, but make them meaningful

A Computer Science Tapestry 2.4 More C++ Anatomy l Programmer-defined functions ä Functions are abstractions that help you to reuse ideas and code ä The square root key on a calculator invokes a function ä The chorus of a song is a similar abstraction ä One word, e.g., “chorus”, takes the place of many or represents a concept l A program is a collection of functions and classes Programs may be implemented in more than one file, but there is only one main function  Execution of the program begins with main  The main function returns a value to the operating system or environment

A Computer Science Tapestry 2.5 Dennis Ritchie l Developed C and Unix l Shared 1983 Turing award and National Medal of Science in 1999 “We wanted to preserve not just a good environment in which to do programming, but a system around which a fellowship could form” l Unix was ä Free to Universities ä Expensive originally ä Linux precursor?

A Computer Science Tapestry 2.6 Execution and Flow Control l Execution of C++ programs is organized around statements ä A statement executes, it may cause another statement to execute ä Statements execute sequentially, or as governed by control that repeats a group of statements or selects one of several groups to execute Control statements covered later; for now sequential flow l Syntax determines what’s in a statement, semantics determines construction of program from statements l Output will be part of our programs  cout is the output stream, objects are placed on the stream ä Objects are strings, numbers, many other types

A Computer Science Tapestry 2.7 Stream output cout is the standard output stream, use cerr for errors and other streams later. Accessible via #include  Objects inserted onto stream with insertion operator <<  Different objects separated by insertion operator << cout << "yadda yadda yadda" << endl; cout << " gross = " << 12*12 << endl; cout << 5 << " in. = " << 5*2.54 << " cm. " << endl; l String literals in quotes, other expressions are evaluated before being output.  endl is the “end of line” object (IO manipulator)  Can also output " \n " or " \t " or " \ "" (escape sequences)

A Computer Science Tapestry 2.8 More about streams and syntax l C++ statements are terminated by a semi-colon cout << *10*10 << " = area " << " of circle with radius = " << 10 << ", circ = " << 2*10* << endl; l Thinking ahead: ä Repetition of radius, problems?  Repetition of , problems? ä What’s better, several statements, or one long statement? ä Evaluating expressions: rules of arithmetic?  Differences between 2*3 and 2*3.0 ?

A Computer Science Tapestry 2.9 Toward Using Functions #include using namespace std; int main() { cout << " |||||||||||||||| " << endl; cout << " | | " << endl; cout << " | o o | " << endl; cout << " _| |_ " << endl; cout << "|_ _|" << endl; cout << " | |______| | " << endl; cout << " | | " << endl; return 0; } l Prints head, but not as modular as program using functions ä Harder to modify to draw differently

A Computer Science Tapestry 2.10 Programmer-defined Functions #include using namespace std; // functions appear here int main() { Hair(); Sides(); Eyes(); Ears(); Smile(); Sides(); return 0; } What are advantages of this main over one in which several output statements appear in main. ä New hair style? Stretched head? ä Are these advantages? ä How is width of head determined? Drawbacks? Solutions?

A Computer Science Tapestry 2.11 Advantages of Functions #include using namespace std; // functions appear here int main() { Hair(); Sides(); Eyes(); Ears(); Smile(); Sides(); return 0; } l What about eyeglasses? Mustache? Big nose? Frown? ä Advantages in extending program rather than modifying program. ä Multiple heads (totem poles)

A Computer Science Tapestry 2.12 Totem Functions int main() { Head1(); Head2(); Head3(); return 0; } l What changed between the two runs of the program? Can you write Headxx() ?  Is Head1 a good name?  Does Headxx call other functions?  Suppose we used graphics instead of cout << ? |||||||||||||||| |||||||||||||||| | | | | | o o | | o o | _| |_ _| |_ |_ _| | |______| | | |______| | | | | | |||||||///////// |||||||///////// | | | | | | | | |---|o|--|o|---| |---|o|--|o|---| | | | | _| |_ _| 0 |_ |_ _| | | | | | | | | | | | | | | | | |||||||||||||||| |||||||||||||||| | | | | | o o | | o o | | | | o | | o | |-+ ||||||| +-| | ||||||| | |-+ +-| _| |_ | |______| | |_ _| | | | |______| | | |

A Computer Science Tapestry 2.13 Parameterized Functions l A square root function that only returns square root of 2 isn’t very useful ä F = sqrt(2), so 2 is a parameter/argument to the function ä Useful parameter to head-drawing functions? ä What about happy birthday printing argument/parameter? l Functions have parameters, arguments are passed to functions Birthday( " Fred " ); // sing to fred Birthday( " Ethel " ); // sing to ethel

A Computer Science Tapestry 2.14 Functions and Parameters (continued) #include using namespace std; void WinBigMoney(string name) { cout << "Hello " << name << " you may have " << " won $1,000,000" << endl; cout << name << ", please call IMN-IDIOT" << endl; } int main() { Hello( " owen " ); Hello( " susan " ); Hello( " bill gates " ); return 0; } l Parameter list provides type and name of parameter ä Argument type must match parameter type ä Function’s prototype based on types only, not names

A Computer Science Tapestry 2.15 Parameterized Functions for Songs l On his farm Old MacDonald had a X that that says Y ä pig, oink ä cow, moo void Verse( ); l Five bottles of Z on a wall, five bottles of Z ä cola ä lemonade void Verse( ); l Mama’s going to buy you a X, and if that X Y ä Mocking bird, don’t sing ä Looking glass, get’s broke void Verse( );

A Computer Science Tapestry 2.16 Calling Functions: where, when, how? l Some functions are imported from libraries ä Function prototypes specified in header files, implementations linked later ä Compiler “sees” prototype before client code calls function l Some functions are in the same file in which they’re called ä Function declaration is the prototype only ä Function definition includes the implementation void Verse(string name); void Verse(string name) { cout << " hi " << name << endl; } l Declaration or definition must appear before call ä Ok to put declaration before, definition after call  Ok to put main last, all definitions first (problems?)

A Computer Science Tapestry 2.17 Ada Lovelace, l Daughter of Byron, advocate of work of Charles Babbage, designer of early “computer” (the Analytical Engine) ä Made Babbage’s work accessible “ It would weave algebraic patterns the way the Jacquard loom weaved patterns in textiles ” l Tutored in mathematics by Augustus de Morgan l Marched around the billiard table playing the violin l Ada is a notable programming language

A Computer Science Tapestry 2.18 Program Style l People who use your program don’t read your code ä You’ll write programs to match user needs l People who maintain or modify your program do read code ä Must be readable, understandable without you next door ä Use a consistent programming style, adhere to conventions l Identifiers are names of functions, parameters, (variables, classes, …) ä Sequence of letters, numbers, underscore __ characters ä Cannot begin with a number (we won’t begin with __)  big_head vs. BigHead, we’ll use AlTeRnAtInG format ä Make identifiers meaningful, not droll and witty