C++.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
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.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
Chapter 6: User-Defined Functions
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Lecture 9 Functions – Part I 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.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
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.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Chapter 6: User-Defined Functions I
Introduction to Programming
IS Program Design and Software Tools Introduction to C++ Programming
C Functions -Continue…-.
Introduction to C++ Systems Programming.
The Three Attributes of an Identifier
Programming Fundamentals Lecture #7 Functions
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
6 Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Variables have attributes
Function.
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Function.
Presentation transcript:

C++

Functions built-in functions function prototype, function definition and use storage class and scope recursion inline function default argument, function overloading, template

Math Library Functions #include <cmath> // to use math // library On Unix, special compiler flag is needed gxx file.cpp -lm most math functions take double as argument and return double as value C.f. math.h

Math Functions ceil(x), cos(x), exp(x), fabs(x), floor(x), fmod(x,y), log(x), log10(x), pow(x,y), sin(x), sqrt(x), tan(x) For some compilers (such as GNU C++), there are also some special functions, such as err function, bessel function etc. C.f. Fig.3.2 h

Functions Organization of Large Program Typical Program Separate tasks into functions Group related functions in separate files. Typical Program Prototypes; main() { … ;} func1() { …; } func2(int i, …) { … }

Functions Function prototype function definition use of function argument passing in C++ C.f. Fig.3.3

Function Prototypes Format void maximum(int, int, int); Location of function prototype in file When can we omit function prototype? Advantage of prototype

Storage Classes Storage class determines the period during which an identifier exists in memory Four storage classes auto, register, extern, static C.f. Fig. 3.12

Auto Storage Class Local variables in functions or blocks. Auto storage class variables are created only when the block is active, and disappear when the block or function exits.

Register Storage Class Variable existence like auto. The register variable is a suggestion to the compiler to put the variable in a CPU register.

Static Storage Class Local static variable exists during the whole program executing, i.e., the variable retains its value between function calls. However, the reference to the variable is local in the function or block.

Extern Storage Class Global variables and function names have the storage class extern. Extern storage class variable exists during the whole program execution.

Scope Rules The places in code segment that an identifier is visible: function scope file scope block scope function-prototype scope class scope

Storage Class and Scope Storage class says when a variable exists scope says where in program segment the variable is valid for reference

Unary Scope Resolution Operator :: Using ::, one can access an global variable even if it is over- shadowed by a local variable of the same name.

Recursion A function can also call itself, this is known as recursion To avoid infinite recursion, one must have a terminating condition in the function recursion v.s. iteration C.f. Fig.3.14

Inline Functions Advantage: function call overhead is eliminated, thus faster and less memory consuming Disadvantage: the code is expanded during compilation so that executable file is large C.f. Fig. 3.19

Call by Value v.s. Call by Reference Call-by-value: the function takes/works on a copy of the original variable Call-by-reference: the function works on the original variable passed by caller. C.f. Fig. 3.20

Call by Reference int func(int &); // prototype int main() { func(x); // call as usual } int func(int &x) // x is ref to int x = ..; // use x as usual

Call by Reference Using Pointer int func(int *); // prototype int main() { func(&x); // call as usual } int func(int *x) // x is ref to int *x = ..; // use x as usual

Default Arguments The right-most arguments, if omitted, take the default value Default values are specified at the first occurrence (prototype or function definition) of the function C.f. Fig.3.23

Function Overloading Several functions of different argument types can use the same function name. E.g. we can define a function square to work on int as well as on double. In fact, we need to write two functions to handle two cases. C.f. 3.25

Function Template A function definition with unspecified data type. The type is determined according to its use at compile time.

Exercise, p.243, 3.22 Write a function that displays at the left margin of the screen a solid square of asterisks whose side is specified in integer parameter side. E.g. side = 4 displays ****

Exercise, p.245, 3.32 & p.248, 3.45 The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function gcd() that returns the greatest common divisor of two integers.