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.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Introduction to C Programming
User Defined Functions
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
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.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 6: User-Defined Functions I
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Introduction to Methods
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 8 Technicalities: Functions, etc. John Keyser’s Modifications of Slides by Bjarne Stroustrup
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
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.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 8 Technicalities: Functions, etc. Hartmut Kaiser
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
User-Written Functions
Chapter 6: User-Defined Functions I
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions Inputs Output
Anatomy of a Function Part 2
User Defined Functions
Chapter 8 Technicalities: Functions, etc.
Chapter 8 Technicalities: Functions, etc.
Functions Overview CSCE 121 J. Michael Moore
Anatomy of a Function Part 1
Anatomy of a Function Part 3
How Functions Work Part 1
Functions Pass By Value Pass by Reference
Function Overloading CSCE 121 J. Michael Moore
Chapter 6: User-Defined Functions I
Chapter 8 Technicalities: Functions, etc.
Guidelines for Writing Functions
Anatomy of a Function Part 1
Corresponds with Chapter 5
Introduction to Methods and Interfaces
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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