Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 121:509-512 Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.

Similar presentations


Presentation on theme: "CSCE 121:509-512 Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup."— Presentation transcript:

1 CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup and Jennifer Welch

2 CSCE 121: Set 5: Functions Philosophy on Language Technicalities Don’t obsess about minor syntax and semantic issues Most software design and programming language concepts are widely used Language technicalities are specific to a given language – Many of those in C++ have counterparts in C, Java, C#, etc. 2

3 CSCE 121: Set 5: Functions What is a Function? A named sequence of statements Can return a result Standard library provides lots of functions We can write our own functions also 3

4 CSCE 121: Set 5: Functions Why Use Functions? Chop a program into manageable parts – Divide and conquer Model problem domain – Name logical operations – A function should do one thing well Improves readability of program Can be useful in many places in program – Avoid having to copy same code Ease testing, maintenance, division of labor 4

5 CSCE 121: Set 5: Functions Rules of Thumb for Functions Keep functions small (about one screen) Each function should do a single well-defined task Makes them easier to understand, specify, and debug 5

6 CSCE 121: Set 5: Functions Declaring and Defining a Function General form: –return_type name (formal arguments); // declaration –return_type name (formal arguments) body // definition Formal arguments are also called parameters, format is type1 name1, type2 name2, … Make return type void if you don’t want to return anything body is a block (or a try block – more later) Example: double f(int a, double d) { return a*d; } 6

7 CSCE 121: Set 5: Functions Calling a Function Recall: double f(int a, double d) { return a*d; } To call a function: name (actual arguments) Actual arguments format is argname1, argname2, … do not include types Example: int x = 2; double y = 5.0; cout << f(x,y); // prints out

8 CSCE 121: Set 5: Functions Stack Frames and Function Calls When function is called, a new area of memory, called stack frame, for the function call, is put on the top of the stack, with an entry for each formal argument When function finishes, its stack frame goes away – memory is recycled for later use – Potential source of programming bugs if you don’t understand how this works! 8

9 CSCE 121: Set 5: Functions Function Placement Functions are placed in your.cpp file before main (which is also a function); they are not inside any other construct You cannot define functions inside other functions (Shortly we’ll talk about defining functions inside classes) 9

10 CSCE 121: Set 5: Functions Call by Value When function is called, value of actual argument is copied into corresponding formal argument variable in stack frame – match according to order in the argument lists Function computes with formal argument No change is made to actual argument See call-by-val.cpp 10

11 CSCE 121: Set 5: Functions References A reference is an alternative name for an object (synonym) – Can have multiple names referring to the same object (location in memory) After a reference is initialized, you cannot change it to refer to a different object int i = 7; int& r = i; // r is a reference to an int r = 9; // i becomes 9 11

12 CSCE 121: Set 5: Functions Call by Reference In the function definition, indicate using “&” that a formal argument should be passed a reference int f(int& a) { a = a+1; return a; } Syntax for calling is the same as for call-by- value But formal argument is now a reference to (synomym for) the actual argument Actual argument can be changed by function call! (See call-by-ref.cpp) 12

13 CSCE 121: Set 5: Functions Call by Reference Pros and Cons Can lead to obscure bugs since arguments can be changed But very convenient, even necessary to – Change several variables in one function – Manipulating containers, like vector – Avoid overhead of copying very large arguments To get the advantage of no-copying and the advantage of no-changing, use call-by-const- ref… 13

14 CSCE 121: Set 5: Functions Call by Const-Reference When function is defined, the syntax for such an argument is const type& var Example: int f(const int& a) { int b; b = a+1; return b; } Very useful for passing large objects that we don’t want to change Compiler will not allow you to change the argument in the function! See call-by-const-ref1.cpp and call-by-const-ref2.cpp 14

15 CSCE 121: Set 5: Functions Guidance for Passing Arguments Use call-by-value for very small objects Use call-by-const-reference for large objects Better to make a change by returning a result instead of modifying an object through an argument Use call-by-reference only when you have to 15

16 CSCE 121: Set 5: Functions Acknowledgments Slides are based on those for the textbook: 16


Download ppt "CSCE 121:509-512 Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup."

Similar presentations


Ads by Google