Functions Lecture 5.

Slides:



Advertisements
Similar presentations
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C/c++ 4 Yeting Ge.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
1 CSC241: Object Oriented Programming Lecture No 27.
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.
ITEC 320 C++ Examples.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
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.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Functions CMSC 202. Topics Covered Function review More on variable scope Call-by-reference parameters Call-by-value parameters Function overloading Default.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
Generic Programming in C
C++ Lesson 1.
User-Written Functions
Functions.
Chapter 7: User-Defined Functions II
Topic Pre-processor cout To output a message.
Chapter 6 CS 3370 – C++ Functions.
Chapter 1.2 Introduction to C++ Programming
Motivation and Overview
Recursion Chapter 12.
Andy Wang Object Oriented Programming in C++ COP 3330
Compilation and Debugging
Command Line Arguments
Functions and Structured Programming
Why exception handling in C++?
Functions and an Introduction to Recursion
Pointers and Pointer-Based Strings
User-defined Functions
Separate Compilation.
Chapter 17 Templates and Exceptions Part 2
Name: Rubaisha Rajpoot
User-defined Functions
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Overview of C++ Overloading
When your program crashes
Pass by Reference.
Namespaces How Shall I Name Thee?.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS150 Introduction to Computer Science 1
Chapter 6: User-Defined Functions I
The Function Prototype
Functions and an Introduction to Recursion
Variables & Basic Types
Pointers and Pointer-Based Strings
Submitted By : Veenu Saini Lecturer (IT)
CMSC 341 C++ and OOP.
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers and dynamic objects
CMSC 202 Lesson 6 Functions II.
CS1201: Programming Language 2
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Class rational part2.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
各題答對人數.
SPL – PS1 Introduction to C++.
Functions Chapter No. 5.
Presentation transcript:

Functions Lecture 5

Functions A function is a block of code with a name. We execute the code by calling the function. A function may take zero or more arguments and (usually) yields a result. int fact (int v) { int ret = 1; while (v > 1) ret *= v--; return ret; }

Parameters 形式参数 & arguments 实际参数 int fact (int v) { int ret = 1; while (v > 1) ret *= v--; return ret; } int main ( ) { int a = 6; cout << fact(a) << endl; cout << fact(7) << endl; return 0; }

Calling a function Initializes the function’s parameters from the corresponding arguments. Transfers control to the function. Execution of the calling function is suspended and execution of the called function begins.

Function declaration A function declaration is like a function definition expect that a declaration has no function body (replaced by a semicolon).

Function declaration // declaration of fact( ) Ok. Ok. int fact (int v); // declaration of fact( ) int fact (int v) { int ret = 1; while (v > 1) ret *= v--; return ret; } int main ( ) { cout << fact(7) << endl; return 0; } int fact (int v) { int ret = 1; while (v > 1) ret *= v--; return ret; } int main ( ) { cout << fact(7) << endl; return 0; } Ok. Ok.

Separate compilation Fact.h Fact.cpp Main.cpp #ifndef FACT_H_ #define FACT_H_ int fact (int v); #endif #include <iostream> #include “Fact.h” using namespace std; int main ( ) { cout << fact(7) << endl; return 0; } Fact.h int fact (int v) { int ret = 1; while (v > 1) ret *= v--; return ret; } Fact.cpp Main.cpp

Write a makefile to compile multiple source files. all: run_fact run_fact: Fact.o Main.o g++ Fact.o Main.o -o run_fact Fact.o: Fact.cpp g++ -O3 -Wall -c Fact.cpp -o Fact.o Main.o: Main.cpp g++ -O3 -Wall -c Main.cpp -o Main.o clean: rm -f *.o makefile

Write a makefile to compile multiple source files. $ make all g++  -O3  -Wall  -c  Fact.cpp  -o  Fact.o g++  -O3  -Wall  -c  Main.cpp  -o  Main.o g++  Fact.o Main.o  -o  run_fact $ ./run_fact 5040 $ make clean rm  -f  *.o Tested on macOS High Sierra ver. 10.13.6 with 3.1 GHz Intel Core i7 [Oct 24, 2018]

Argument passing The function’s parameters are initialized from the corresponding arguments. Parameter initialization works the same way as variable initialization. 1. Passing by value r (int) ret (int) n (int) int v = n; fact int fact(int v); int r = fact(n);

Argument passing What if the argument is a large, complex data object? Copying the entire graph. Very time-consuming. int diameter(Graph g); Graph gr; int d = diameter(gr); d (int) ret (int) gr (Graph) Graph g(gr); diameter

Argument passing Pointer parameters / arguments d (int) ret (int) gr (Graph) Graph *g = p; diameter p (Graph*) int diameter(Graph *g); Graph gr; Graph *p = &gr; int d = diameter(p);

Argument passing Passing an array 回忆:数组名 / 指向数组首元素的指针 数组长度 double average(int *arr, int n); int a[ ] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; double v1 = average(a, 9); int b[ ] = {0, 1, 2, 3, 4, 5, 6}; double v2 = average(b, 7);

Argument passing 2. Passing by reference 形式参数被初始化为实际参数的别名。 int diameter(const Graph &g); d (int) ret (int) gr (Graph) const Graph &g = gr; diameter Graph gr; int d = diameter(gr);

Argument passing Reference parameters that are not changed inside a function should be references to const. int diameter(const Graph &g); 以下函数声明暗示着Graph对象g会在函数中被改变,尽管在实际实现时g完全可以保持不变。 int diameter(Graph &g);

Return types Most types can be used as the return type of a function. In particular, the return type can be void, which means that the function does not return a value. The return value is used to initialize a temporary at the call site, and that temporary is the result of the function call. int fact(int v); int diameter(const Graph &g); void append(vector<int> &vec, int n);

Return types What if the return type is complex? MyInt getMyInt(int n) { MyInt obj(n); return obj; }; What if the return type is complex? struct MyInt { int n; MyInt(int v): n(v) { } MyInt(const MyInt &obj): n(obj.n) { cout << “copy.\n”; } }; int main( ) { MyInt obj = getMyInt(4); cout << obj.n << endl; return 0; };

Return types What if the return type is complex? MyInt getMyObject(int n) { MyInt obj(n); return obj; }; What if the return type is complex? $ g++ -c Main.cpp -o Main.o $ g++ Main.o -o rvo $ ./rvo 4 int main( ) { MyInt obj = getMyInt(4); cout << obj.n << endl; return 0; }; Tested on macOS High Sierra ver. 10.13.6 with GCC ver. 4.2.1 [Oct 24, 2018]

Return types What if the return type is complex? 1st copy 2nd copy MyInt getMyInt(int n) { MyInt obj(n); return obj; }; What if the return type is complex? 1st copy $ g++ -c Main.cpp -o Main.o -fno-elide-constructors $ g++ Main.o -o rvo $ ./rvo copy. 4 2nd copy int main( ) { MyInt obj = getMyInt(4); cout << obj.n << endl; return 0; }; Tested on macOS High Sierra ver. 10.13.6 with GCC ver. 4.2.1 [Oct 24, 2018]

Copy elision & RVO return value optimization Return value optimization(RVO)是一种编译器优化技术,它旨在省略用于 保存函数返回值的临时对象。 MyInt* getMyInt(int n, MyInt *_r) { MyInt obj(n); *_r = obj; return _r; }; MyInt getMyInt(int n) { MyInt obj(n); return obj; }; compiler

Return types 建议不要过度依赖编译器优化。 使用非常量引用参数来接收函数的实际计算结果;保留函数返回值用于指 示函数的执行过程中是否存在异常。 int getMyInt(int n, MyInt &obj);

Return types 可以返回指针或引用。 int& add(int &a, int b) { a += b; return a; } int main( ) { int a = 3; add(a, 4) += 5; cout << a; return 0; 可以返回指针或引用。

Return types 不可以返回指向局部变量的指针或绑定局部变量的引用。 warning: reference to stack memory associated with local variable 'a' returned int& func(int n) { int a = 5; int &r = a; r += n; return r; } cout << func(4); -350296528 Tested on macOS High Sierra ver. 10.13.6 with GCC ver. 4.2.1 [Oct 24, 2018]

Throwing exceptions error type 2. int fact (int v) throw (int) { if (v < 0) throw -1; if (v > 12) throw 1; int ret = 1; while (v > 1) ret *= v--; return ret; } int main ( ) { try { cout << fact(20) << endl; } catch (int e) { if (e == -1) cout << “error type 1.\n”; else if (e == 1) cout << “error type 2.\n”; else cout << “unknown error.\n”; } return 0; error type 2.

Exceptions vs error code 这套机制不太好用。 利用函数返回值返回错误代码是常用做法。

Overloaded functions Functions that have the same name but different parameter lists and that appear in the same scope are overloaded. void print (const char *cp); void print (const int *beg, const int *end); void print (const int ia[ ], size_t size);

Function matching int ia[7] = {0, 1, 1, 2, 3, 5, 8}; void print (const char *cp); void print (const int *beg, const int *end); void print (const int ia[ ], size_t size); print (ia, 5); print (ia, ia + 4); print (“Hello”);

Overloading and const parameters int func (int n); int func (const int n); // error: redefinition of ‘func’ 实际上第二个函数的const没什么意义

Overloading and const parameters int func (int &n) { cout << "func1.\n"; n *= 2; return n; } int func (const int &n) { cout << "func2.\n"; int main() { int a = 4; const int b = 6; cout << func(a) << endl; cout << func(b) << endl; return 0; } func1. 8 func2. 6 按照constness做精确匹配 Tested on macOS High Sierra ver. 10.13.6 with GCC ver. 4.2.1 [Oct 24, 2018]

Overloading and const parameters int main() { int a = 4; const int b = 6; cout << func(a) << endl; cout << func(b) << endl; return 0; } func2. 4 6 int func (const int &n) { cout << "func2.\n"; return n; } 对变量a做了const conversion,然后再匹配 Tested on macOS High Sierra ver. 10.13.6 with GCC ver. 4.2.1 [Oct 24, 2018]

Overloading and const parameters int main() { int a = 4; const int b = 6; cout << func(a) << endl; cout << func(b) << endl; return 0; } int func (int &n) { cout << "func1.\n"; n *= 2; return n; } error: no matching function for call to ‘func’ Tested on macOS High Sierra ver. 10.13.6 with GCC ver. 4.2.1 [Oct 24, 2018]

Pointers to functions 写一个函数,根据用户指定的比较方法对二维空间中的点进行排序。 bool x_less (const Point &a, const Point &b); bool y_less (const Point &a, const Point &b); void mySort ( vector<Point> &points, bool (*p) (const Point &, const Point &) ) { // 用(*p)(a, b)来比较两个点a和b }

Recursion A function that calls itself, either directly or indirectly, is a recursive function. int fact (int n) { if (n == 0) return 1; return n * fact(n-1); } 𝒏!≝𝒏⋅ 𝒏−𝟏 ⋅ 𝒏−𝟐 ⋅⋯⋅𝟐⋅𝟏 𝒏!≝ 𝟏 𝐢𝐟 𝒏=𝟎 𝒏⋅ 𝒏−𝟏 ! 𝐢𝐟 𝒏>𝟎 必须有终止条件判断,否则会无限递归。

Function comment block 作为programmer,当我们使用一个已有function的时候,我们不希望关心它 的实现细节。 作为programmer,当我们使用一个已有function的时候,我们需要了解它的 实现细节。

Function comment block /** * Computes the factorial of a given number. * Input: integer n * Requirement: n >= 0 and n <= 12 * Output: n! * Algorithm: recursion. */ int fact (int n);

Function comment block /** * Sorts a vector of points based on a user-specified comparator. * Input: points, a vector of points; * p, a pointer to a comparator function * Output: points, sorted according to the specified comparator * Complexity: O(nlogn), where n is the number of points */ void mySort (vector<Point> &points, bool (*p)(const Point&, const Point&));

Next lecture C++ Primer, Chapter 7