C++ Functions. Procedural-based Programming. what s a functions

Similar documents
OBJECT ORIENTED PROGRAMMING USING C++

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Implementing Abstract Data Types (ADT) using Classes

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Programming in C and C++

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Introduction to Programming Using Java (98-388)

Fast Introduction to Object Oriented Programming and C++

Tokens, Expressions and Control Structures

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

Interview Questions of C++

Object Oriented Programming in C#

COMP6771 Advanced C++ Programming

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

G52CPP C++ Programming Lecture 13

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Instantiation of Template class

Object-Oriented Principles and Practice / C++

Chapter 2. Procedural Programming

G Programming Languages - Fall 2012

QUIZ Friends class Y;

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Object-Oriented Principles and Practice / C++

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

III. Classes (Chap. 3)

G52CPP C++ Programming Lecture 9

Object Oriented Software Design II

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Exceptions. CandC++ 7. Exceptions Templates. Throwing exceptions. Conveying information

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 7. Exceptions Templates 2/1. Throwing exceptions 14 }

Chapter 3 Function Overloading

C and C++ 7. Exceptions Templates. Alan Mycroft

QUIZ. What is wrong with this code that uses default arguments?

COMP 2355 Introduction to Systems Programming

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

CS304 Object Oriented Programming Final Term

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Cpt S 122 Data Structures. Introduction to C++ Part II

Object-Oriented Principles and Practice / C++

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

C The new standard

CS240: Programming in C

EL6483: Brief Overview of C Programming Language

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Chapter 7 Constructors and Other Tools. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Constants, References

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Copy Control 2008/04/08. Programming Research Laboratory Seoul National University

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

COEN244: Class & function templates

Operator Dot Wording

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

CPSC 427: Object-Oriented Programming

CS 247: Software Engineering Principles. ADT Design

SFU CMPT Topic: Class Templates

Introduction to Move Semantics in C++ C and C

CS201 Latest Solved MCQs

Chapter 4 Defining Classes I

EL2310 Scientific Programming

QUIZ. Can you find 5 errors in this code?

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

CE221 Programming in C++ Part 1 Introduction

AN OVERVIEW OF C++ 1

CS11 Intro C++ Spring 2018 Lecture 5

September 10,

VALLIAMMAI ENGINEERING COLLEGE

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Before we dive in. Preprocessing Compilation Linkage

EECS168 Exam 3 Review

CS110D: PROGRAMMING LANGUAGE I

2 ADT Programming User-defined abstract data types

Operator overloading. Conversions. friend. inline

Object-Oriented Programming, Iouliia Skliarova

ENERGY 211 / CME 211. Functions

10. Functions (Part 2)

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Template Issue Resolutions from the Stockholm Meeting

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Classes. Christian Schumacher, Info1 D-MAVT 2013

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2015 EOM, HYEONSANG ALL RIGHTS RESERVED

Transcription:

C++ Functions Fred Kuhns fredk@cse.wustl.edu Applied Research Laboratory, Department of Computer Science and Engineering, Washington University in St. Louis Washington WASHINGTON UNIVERSITY IN ST LOUIS Procedural-based Programming Every C++ program contains (starts with) a main() function. The main() function may in turn call other user defined funtions Functions encapsulates an algorithm which operates on data passed in as parameters or explicit access to globally scoped data An object s scope determines its visibility (where it can be used) C++ defines several useful facilities related to functions: overloaded function definitions function prototypes exceptions STL and generic algorithms Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 2 what s a functions Function prototype <return type> function_name(<parameter type list>); Function definition <return type> function_name(<parameter list>){<function body>} inline ed functions inline <return type> function_name(<parameter_list>){<function body>} Example protocype and definition int min(int, int); int min(int a, int b) {return (a < b? a : b);} may return any built-in or user defined type. Can not return and array or function (but may return a pointer to one) Example function return types: void print_all(myclass_t c); Date &today(void); Node *get_last(head &); float sq(float, float); Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 3

Function arguments C++ is strongly typed: compiler verifies arguments are of the proper type (at compile time, not run time) Arguments: Arguments (and local variables) are allocated space on the run-time stack (function specific storage: activation record) Non-reference arguments are initialized by copy. Default passed-by-value, but may explicitly declare parameters to be passed by references. int min1(int a, int b){a = a<b?a:b;return a} int min2(int& a, int b){a = a<b?a:b;return a} Arrays as arguments int sum(int a[]); but how many elements in a? can use a vector, pass size or protocol int sum(vector<int> vec); // can use vec.size() int sum(int *a, size_t len); // there are len elements in a int sum(int *a) { int sum=0; while(*a!= -1) sum += *a; return *a;} Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 4 More bout functions Return value is pass-by-value, unless return type is void return; // nothing is returned return f; // a copy of object f is returned What about this defintion? value_t &lookup(key_t key) { code to locate key in table ; return table{key}.value}; Is there anything wrong with the following? myclass &getval(void){myclass m; ; return m}; Watch for side effects int &getval(const vector<int> &v, size_type i){return v[i]}; Use const to guard against unexpected resultsconst int &getval(vector<int> &v, int i){return v[i]}; function pointers int getval(int *a, int i){return v[i]}; int (*fp)(int *, int), x[10]; fp = getval; fp(x, 10); Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 5 Argument Examples Functions specifying const arguments. Does the const for parameter a make a difference to the caller? When considering viable functions during overload resolution? int fn(const int a) {return 2 * a;} int x = 4, w = fn(x); // ok const variables int fn(int a) {a = 5 * 4; return a;} const int x = 5; int w = fn(x); // error Overhead in function calls struct X_t {int st_; size_t len_; int buf_[64];} int sum(x_t x); X_t xx; /* fill in xx */ ; int s = sum(xx); Default behavior is to make a copy of the argument, in the above case this would be very expensive. Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 6

Local Variables local variables are placed on the stack, consequently every function call receives its own copy of local variables int sum(int x) {int sum = 0; sum += x; return sum;} // always returns the value of x You can declare a local variable to be static: int sum(int x) { static int sum = 0; sum += x; return sum; } // keeps a running total Declaring a local variable to be static causes a single statically allocated object to be created and used. there is only one object it retains it s value across function calls only initialized once. Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 7 Call by Reference Attempt 2: struct X_t {int st; size_t len; int buf[64];} int sum(x_t &x); X_t xx = { }; int s = sum(xx); In this case the compile must only bind the functions local argument symbol to the address of the caller s argument. Much cheaper than the earlier copy. We could also do this with pointers struct X_t {int st; size_t len; int buf[64];} int sum(x_t *x); X_t xx = { }; int s = sum(&xx); Additional advantage to references is some classes may not permit the copy operation. We can use similar reasoning for using references (or pointers) for return values. Generally in C++ you should prefer references over pointers. Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 8 More on references Using const references int fn(const X_t x); Generally const references are the safest way to go for a few reasons: I can be called with both lvalues and rvalues, specifically it can be called with temporary objects. assures the caller that their object will not be modified therefore avoiding unexpected side effects. the function may be used for both const and non-const objects. Examples form the book: int incr(int &v) {return ++val;} int main() {short v1 = 0; const int v2 = 42; int v3 = incr(v1); v3 = incr(v2); v3 = incr(0); v3 = incr(v1+v2); int v4 = incr(v3); } Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 9

Default Argument values You may provide default initializers for argument values int fn(int x, int y=0, int z=0); int a = fn(2); // y and z initialized to 0 int b = fn(3, 4); // z initialized to 0 c = fn(2, 0, 0); // all values explicit int d = fn(); // error, at least the first arg // must be specified Arguments are associated with parameters by position int fn(int x, double y=0, char z=0); int a = fn(23, c, 5.6); // logic error Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 10 inlining (from ~cdgill s notes) A function call uses the program call stack 1. Stack frame pushed when the call is made 2. Execution jumps to the function s code block 3. Function s code block is executed 4. Execution returns to just after where call was made 5. Stack frame is popped This incurs a (small) performance cost Copying arguments, other info into the stack frame Stack frame management Copying function result back out of the stack fr Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 11 Inlining (~cdgill) Considerations: If the function body is small, Computer might do more work manipulating the stack frame than in the actual function Classic computer science tradeoff Alternatives: can ask the compiler to use function inlining Intention: a directive, sometimes just a request Compiler makes an in line copy of the code at each place where it s called Code is inserted once for every function call made Especially good for small repetitive function calls Class/struct member accessors and mutators Empty or very simple implementations Benefits and Costs of Inlining call frequency, space requirements, performance Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 12

Inlining (~cdgill) The inline keyword is just a suggestion to the compiler Increase portability: make inlining requests optional using preprocessor directives Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 13 Member Functions Similar to standard functions, you must specify the return type, name, parameter list and body. Function prototype must appear in the class body. The function body may be defined separately. If default constructor is not specified then one is generated for you. (synthesized default constructor) class Complex { private: double re_; double im_; public: // Constructor initializer list : re_(0), im_(0) Complex() : re_(0), im_(0) {}; // Default constructor Complex(const Complex& c) : re_(c.re_), im_(c.im_) {}; // copy constructor Complex(double a, double b = 0) : re_(a), im_(b) {}; double real() const {return re_;} void real(double r) {re_ = r;} }; double imag() const {return im_;} void imag(double i) {im_ = i;} Complex & operator+=(const Complex &rhs){re_ += rhs.re_; im_ += rhs.im_;return *this;} Complex & operator-=(const Complex &rhs) {this->re_ -= rhs.re_; this->im_ -= rhs.im_; return *this;} Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 14 Overloading Same operator or function name but with different arguments Both operators and functions can be overloaded Signature consists of operator/function name and the number and types of its arguments in order E.g., add(int, long) and add(long, int) have different signatures Overriding a base class method is also a form of overloading But definitions are separated by scopes Compiler can distinguish (dynamically or statically, depending on whether virtual or not) Overloading is resolved by Finding all possible matches based on passed arguments Finding the best match among those Signature does not depend on return type So, overloading can t be resolved by return type alone Functions declared in different non-namespace scopes do not overload with namespaces using declarations can be used to control overloading Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 15

Finding a match Overload resolution, return type is not considered so result is not context dependent: 1. Best match: finding only one meeting all criteria 2. No match: not finding any matching functions 3. Ambiguous: more than one match is found The 3 steps 1. Find candidates: same name, visible in the current scope 2. Viable functions: have the same number of parameters and the types either match or are convertible. 3. Determine the best match: of the viable functions select the one with the closest matching argument types. With multiple arguments there is a match if 1. match for each argument is no worse than match required for any other viable function and 2. there is at least one argument with a better match than all other viable functions. Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 16 Argument Type Conversions Ranking of conversions 1. Exact match: exact or trivial conversions 1. no conversion 2. array name to pointer 3. function name to function pointer 4. T to const T 2. Promotion: See text for a complete list 1. (bool, char, short) to int -- signed and unsigned 2. float to double 3. double to long double 3. Standard conversion: see book for a complete list 1. int to double 2. double to int 3. Derived* to Base* 4. T* to void* 5. int to unsigned int 4. Class-type conversion: user defined conversions 5. match using elipses in function declaration const parameters only matter for reference and pointer arguments. Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 17 Helping the Compiler out Overloaded function void f1(char); void f1(long); void f2(char*); void f2(int*); void k(int i) { f1(i); // ambiguous: i promotes to int, and // has conversions to both char and long. f2(0); // ambiguous: literal is int or void pointer with // conversions to both char* and int* } To resolve the ambiguity you could add a wrapper function or specify the type: inline void f1(int n) {f1(long(n));} f2(static_cast<int*>(0)); Fred Kuhns (2/9/2006) CS422 Operating Systems Concepts 18