Functions Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Procedures and Control Flow CS351 – Programming Paradigms.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
C++ Training Datascope Lawrence D’Antonio Lecture 6 An Overview of C++: What is Polymorphism? - Coercion.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
C++ Pointer and Functions
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : … Suppose we need.
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Pointers Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
C++ Control Flow Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th -22 nd Sept 2006.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Basic Elements of C++ Chapter 2.
C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
C++ Workshop Sujana Jyothi Dept. of Computer Science.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 Functions in C++ Default arguments Overloading Inlining Call-by-reference Scope of variables, static, extern namespace new and delete assert.
SNU OOPSLA Lab. 7. Functions © copyright 2001 SNU OOPSLA Lab.
Unit 2 Test: Tues 11/3. ASCII / Unicode –Each letter, symbol, etc has a # value –See ascii table (in folder) –To convert a char into its ascii value,
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
 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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Lesson 1.
Chapter Topics The Basics of a C++ Program Data Types
Programming with ANSI C ++
Programming with ANSI C ++
IS Program Design and Software Tools Introduction to C++ Programming
Introduction to C++ Systems Programming.
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Conversions of the type of the value of an expression
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Functions 2: Stupid Function Tricks
Miscellaneous C++ Topics
Default Arguments.
Variables have attributes
CMSC 202 Lesson 6 Functions II.
Functions Reasons Concepts Passing arguments to a function
Presentation transcript:

Functions Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Functions: Types of arguments and return values Types of return values conversion rules also apply to return-statements int g(double x, double y) { return x * x - y * y + 1; } the value returned is int and truncation takes place It would be better to explicitly acknowledge this with a cast int g(double x, double y) { return int (x * x - y * y + 1); }

Functions: initialization #include void f() { static int i=1; std::cout << i++ << std::endl; } int main() { f(); return 0; }

A static variable can be used as a flag void f() { static bool first_time = true; if (first_time) { cout << “f called for the first time\n”; first_time = false; // false } cout << “f called (every time)\n”; }

Functions: initialization Default arguments C++ allows a function to be called with fewer arguments than there are parameters Once a parameter is initialized, all subsequent parameters must also be initialized void f(int i, float x=0; char ch=‘A’) {.. }

Functions: initialization void f(int i, float x=0; char ch=‘A’) {... }... f(5, 1.23, ‘E’); f(5, 1.23); // equivalent to f(5,1.23,‘A’); f(5); // equivalent to f(5,0,‘A’);

Function overloading two or more functions with the same name The number or types of parameters must differ: void writenum(int i) { std::cout “i is “ << << i << std::endl; } void writenum(float x) { std::cout << “x is: “ << x << std::endl; }

Functions: o verloading int g(int n) {... } float g(int n) {... }

Functions: References as return values A value can be returned from a function using any of the 3 transmission modes. This is especially important when passing objects.

Functions: Inline functions and macros A function call causes a jump to a separate and unique code segment the passing and returning of arguments and function values saving the state Inline functions cause no jump or parameter passing no state saving duplication of the code segment in place of the function call

Which is safer: macro or inline? which faster? which smaller? #define max(a, b) (a < b) ? a : b) inline int max(int a, int b) { return a > b ? a : b; }

Use inlining judiciously Inlining is safer, provides opportunity for compiler to optimize, frequently smaller and faster code! overzealous inlining = code bloat ==> pathological paging, reduce instruction cache hit rate if function body is short, inlined function may be shorter than code generated for the call the inline directive is a compiler hint, not a command. Compilers are free to ignore