Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: User-Defined Functions II
 2006 Pearson Education, Inc. All rights reserved Functions.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2006 Pearson Education, Inc. All rights reserved Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Review of C++ Programming Part II Sheng-Fang Huang.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
C++ for Engineers and Scientists Third Edition
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
Review 2 - Chapter 3 and 4 Function 1 Function 2 Function 3 Function 4
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
1 Chapter 6 - Functions Outline 6.1Introduction 6.2Program Components in C++ 6.6Math Library Functions 6.4Functions 6.5Function Definitions 6.6Function.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
2Object-Oriented Program Development Using C++ 3 Function Declarations and Definitions Analogize functions to methods –Share same basic features –Unlike.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
Functions.
IS Program Design and Software Tools Introduction to C++ Programming
Functions and an Introduction to Recursion
C Functions -Continue…-.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
User-Defined Functions
6 Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Functions and an Introduction to Recursion
6 Functions.
Variables have attributes
1-6 Midterm Review.
Presentation transcript:

Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions, and modulus operations are calculated from left to right.

Operator Precedence Third all additions and subtractions are evaluated from left to right. –X = (1 + (( 2 + 4) * (5 + 2))) x = (1 + (6 * 7)) = (1 + 48) = 49

Operator Precedence Fourth stream insertions (‘ >’) are evaluated, left to right. Next all relational operators are evaluated left to right. (‘ ’, ‘>=‘) Equality is evaluated sixth from left to right. (‘==‘, ‘!=‘) Finally, assignment is evaluated. (‘=‘)

C++ Keywords There are many keywords in C++ that are not used in other languages. –Page 63, Figure 2.2

If/else Selection Structure Example Programs

If/else Selection Structures Multi-line statements in an if body require braces (‘{‘ and ‘}’). –Otherwise only the first statement in the body is executed as part of the if body.

Switch Statements Switch statements are able to test int and char variables to handle multiple decisions. Code example.

Looping Control Structures C++ provides three types of loop control structures. –While loop –Do/While loop The body of the loop is executed at least once. –For loop –Example code

Math Functions C++ provides predefined math functions in the Math Library. –You need to simply refer to the name of the function with the proper parameters. –The most common functions are listed on Page 161 in Figure 3.2.

Function Definitions C++ Functions are defined using the following format: Return-value-type function-name (parameter-list) { declarations and statements return XXX; }

Function Definitions –Parameters to functions are considered local variables to the function. If there are no parameters the list is empty. –The return type of a function must be defined. The return type for a function that does not return a data type is void. The Coding Standard requires that there be only one return statement per function at the end of the function.

Function Definitions –Functions can not be defined inside of other functions. Each function is an individual.

Function Prototypes C++ requires a function prototype for each function a class defines. –The function prototype includes the function name, the parameter names and data types, and the order of the parameters.

Header Files C++ requires each class have both a *.C file as well has a header file *.h. –The header file contains the function prototypes for all class functions as well as the definitions of the various data types and constants for the class.

Header Files C++ provides many predefined classes. To use these classes in your program you need to include them using: #include ; –Page 169 – 170 Figure 3.6 lists many of the common Standard Library header files.

Header Files To include a header file for a class you define use: #include “nameOfTheHeaderFile.h” –For now we will only use class definitions from the Standard Library.

Data Types C++ includes the standard data types as well as data types that are “unsigned”. –Unsigned data types include only positive numbers. –The range of int includes both positive and negative integers. –The range of unsigned int is as wide as int but includes only positive numbers.

Enumeration Enumeration is a data type that creates a set of integer constant identifiers. –The first identifier is set to zero (0), the second is assigned to one (1), etc. The identifiers can also be assigned a particular integer value. –Enumeration identifier names are all capital letters. –Enumeration identifiers make a program easier to understand. enum Status {CONTINUE, WON, LOST};

Storage Classes Storage classes are used to determine the period during which an identifier exists in memory. –The storage class specifies are: auto, register, extern, mutable, and static.

Storage Classes An identifier’s storage class specifier determines the storage class, scope, and linkage of the identifier. –Scope refers to where the identifier can be referenced in the program. –Linkage determines for a multiple-source- file program whether an identifier is known only in the current source file or in any source file with proper declarations.

Storage Classes Automatic storage class creates variables when the block in which they are declared is entered, they exist while the block is active, and they are destroyed when the block is exited. –Only variables can have automatic storage. –There are two types: auto and register. –By default, local variables in functions are auto.

Storage Classes –The register keyword can be used with local variables. Use register when the programmer wants to suggest that the compiler place the variable in one of the computer’s high-speed hardware registers rather than memory. The compiler might ignore this definition. –Only one storage class specifier can be applied to an identifier.

Storage Classes The extern and static keywords declare identifiers for variables and functions that are in the static storage class. –Variables of this type exist from the time the program begins execution. Memory is allocated and initialized when the program begins execution. –Functions of this type exist from the point where the program begins execution.

Storage Classes The extern class is associated with global variables and functions. –NOTE: The RIT CS C++ Coding Standard does not permit the use of global variables and functions. The only exception is the main function.

Storage Classes The static keyword is used for local variables and functions. –Static variables maintain their values when a function is exited so that the next time the function is executed the values are still available.

Scope Rules Scope refers to the portion of a program where identifiers have meaning. –Not all variables will be available to each function. –Scope Example

Recursion A recursive function is a function that calls itself, either directly, or indirectly. –Recursive functions must include the simplest case(s) called a base case(s).

Recursion A classic recursive example is the calculation of Fibonacci numbers. –fibonacci(0) = 0 –fibonacci(1) = 1 –fibonacci(n) = fibonacci(n – 1) + fibonacci(n –2)

Recursion Write a recursive function to calculate X N where X and N are both positive integers.

Inline Functions C++ allows the definition of inline functions. –Inline functions tell the compiler to generate a copy of the function’s code in place to avoid a function call. Multiple copies of the function code are inserted into the program. –Such functions must be preceded by the keyword inline. –The compiler can choose to ignore such definitions.

Inline Functions –Inline functions should only be used for very simple, frequently used functions. inline return-type functionName (parameter list) { statement;}

Call-By-Reference C++ allows functions calls to use Call-By- Reference. –The calling function gives the called function the ability to directly access and modify the caller’s data. –Call-By-Reference eliminates the overhead of copying large amounts of data. This method does weaken security. –To use this passing method, the reference parameter must be preceded by an ampersand (&).

References Variables can be defined as a reference to another variable. –Once a variable is defined as a reference, it can not be redefined to reference a different variable. int count = 1; int &countRef = count; ++countRef;

References C++ does permit functions to return references, but you should not do this. –The program quickly becomes very confusing.

Function Overloading Function Overloading permits multiple functions with the same name to be defined as long as each function has a different set of parameters. –The compiler will chose the proper version of the function based upon the parameter list. –By definition, an overloaded function should execute the same task using different data types.

Function Overloading –The overloaded functions do not have to have the same number of parameters. –The return type of the functions can be different. Simply changing the return type, without changing the parameter list does not create an overloaded function.

Function Overloading int square( int x) { return x * x; } double square (double x) {return x * x;}

Function Templates Function Templates can be used to create overloaded functions that have an identical function body. –Function templates will define a “family” of functions. –The compiler will execute the proper version of the function based upon the passed data types. –Template Example.

Arrays Arrays in C++ are very similar to arrays in other languages. –One main difference is that the range of values in an array (or the subscripts) begin with zero (0) and go to the length of the array – 1 (n – 1). –Accessing an element at position n is a run time error.

Passing Arrays C++ passes entire arrays to functions using call-by-reference. –This means that any changes to the array in the function will affect the original array data. C++ passes individual array elements to functions using call-by-value. –This means that any changes to the value in the function do not affect the original array element data.

Array Length Java users should note that there is no length parameter for arrays in C++. –Your program should pass the length of the array to each function.

Sorting Arrays Sorting arrays is a common task. –We will look specifically at the bubble sort. The bubble sort moves smaller values to the top of the array and larger values to the bottom of the array. The bubble sort is fairly slow and should only be used for small arrays.

Searching Arrays To find a particular element in an array a program will search an array. – A linear search will compare the search key to every element in the array. Use for small unsorted arrays. –A binary search compares the search key to the middle element in the array and then searches one-half of the remaining elements based upon the comparison. Much faster search but requires a sorted array.

Multi-dimensional Arrays C++ permits the development of m by n arrays. –m rows and n columns. –We will only work with two dimensional arrays. –2-D array example.