Programming in c++ C++ Syntax and Semantics and the Program Development Process Dale/eems/Headington
1 C++ Syntax and Semantics, and the Program Development Process
Programming in C++ Chapter 2 Topics Syntax and Semantics s Programs Composed of Several Functions Syntax Templates s Legal C++ Identifiers x Data and Data Types Assigning values to Variables s Declaring Named Constants s String Concatenation Output Statements C++ Program Comments
2 Chapter 2 Topics ❖Syntax and Semantics ❖Programs Composed of Several Functions ❖Syntax Templates ❖Legal C++ Identifiers ❖Data and Data Types ❖Assigning Values to Variables ❖Declaring Named Constants ❖String Concatenation ❖Output Statements ❖C++ Program Comments
Programming in C++ Syntax and Semantics a programming language is a set of rules, and special words used to construct a program. There are rules for both syntax (grammar) and semantics(meaning) Syntax The formal rules governing how valid instructions are written in a programming language Semantics The set of rules that determines the meaning of Instructions written in a programming language
3 Syntax and Semantics ❖A programming language is a set of rules,and special words used to construct a program.There are rules for both syntax (grammar) and semantics (meaning). ❖Syntax The formal rules governing how valid instructions are written in a programming language. ❖Semantics The set of rules that determines the meaning of instructions written in a programming language
Programming in C++ Syntax Templates Let's look at the syntax template for the C++ main function Main Function int main o Statement
4 Syntax Templates Let’s look at the syntax template for the C++ main function. Main Function int main () { Statement . . . }
Programming in C++ A C++ program is a collection of one or more functions o there must be a function called main() % execution always begins with the first statement in function main() o any other functions in your program are subprograms and are not executed until they are called
5 A C++ program is a collection of one or more functions ❖there must be a function called main( ) ❖execution always begins with the first statement in function main( ) ❖any other functions in your program are subprograms and are not executed until they are called
Programming in C+ Program With Several Functions main function square function cube function
6 Program With Several Functions main function square function cube function
Programming in C+ Program With Three Functions include int Square( int )i / declares these two int Cube( int )i // value-returning functions using namespace std int main() cout < wThe square of 27 is w < Square(27)<< endl y£ unction ca11 cout < uThe cube of 2 is 1 < Cube(27)<< endl / function call return 0 7
7 Program With Three Functions #include int Square( int ); // declares these two int Cube( int ); // value-returning functions using namespace std ; int main( ) { cout << “The square of 27 is “ << Square(27) << endl; // function call cout << “The cube of 27 is “ << Cube(27) << endl; // function call return 0; }
Programming in C++ Rest of Program int Square( int n) return n"n; int Cube( int n return n*n*n
8 Rest of Program int Square( int n ) { return n * n; } int Cube( int n ) { return n * n * n; }
Programming in C++ Output of program The square of 27 is 729 The cube of 27 is 19683
9 Output of program The square of 27 is 729 The cube of 27 is 19683
Programming in C+ Every C++ function has 2 parts int main() heading body block return o
Every C++ function has 2 parts int main ( ) heading { body block return 0; }