Chapter 5 Functions.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
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.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Programming in C++ 1. Learning Outcome  At the end of this slide, student able to:  Understand the usage of classes and functions.  Understand static.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 6 Functions.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
Chapter 5 Functions §5.1 Basics of Function §5.2 Parameter Passing §5.3 Function Overloading §5.4 Prototype and Reuse §5.5 The Scope of Variables §5.6.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 5 Functions Lecturer: Mrs Rohani Hassan.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Functions.
Reference: COS240 Syllabus
Chapter 7: User-Defined Functions II
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 5 Function Basics
Chapter 5 Methods.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 6 Functions.
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
Chapter 6 Methods 1.
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-defined Functions
Chapter 5 Function Basics
6 Chapter Functions.
User-defined Functions
Group Status Project Status.
Chapter 5 Function Basics
Classes and Objects.
Chapter 6 Methods.
Chapter 5 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Predefined Functions Revisited
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Chapter 5 Functions

Objectives To create functions, invoke functions, and pass arguments to a function (§5.2-5.4). To understand the differences between pass-by-value and pass-by-reference (§§5.5-5.6). To use function overloading and understand ambiguous overloading (§5.7). To use function prototypes for declaring function headers (§5.8). To know how to use default arguments (§5.9). To create header files for reusing functions (§5.11). To determine the scope of local and global variables (§5.13). To develop applications using the C++ mathematical functions (§5.14). To design and implement functions using stepwise refinement (§5.15). (Optional) To improve runtime efficiency using inline functions (§5.16).

Introducing Functions A function is a collection of statements that are grouped together to perform an operation.

Introducing Functions, cont. Function signature is the combination of the function name and the parameter list. The variables defined in the function header are known as formal parameters. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.

Introducing Functions, cont. A Function may return a value. The returnValueType is the data type of the value the function returns. If the function does not return a value, the returnValueType is the keyword void.

Calling Functions Listing 5.1 Testing the max Function This program demonstrates calling a Function max to return the largest of the int values TestMax

int main() { int i = 5, j = 2; int largest; if (i > j) largest = i; else largest= j; cout << "The maximum between " << i <<" and " << j << " is " << largest; return 0; } int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } int largest = max(i, j);

#include <iostream> using namespace std; //function definition int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } int main() { int i = 5, j = 2; int largest = max(i, j); cout << "The maximum between " << i <<" and " << j << " is " << largest; return 0; }

Calling Functions, cont. animation Calling Functions, cont.

Trace Function Invocation animation Trace Function Invocation i is now 5

Trace Function Invocation animation Trace Function Invocation j is now 2

Trace Function Invocation animation Trace Function Invocation invoke max(i, j)

Trace Function Invocation animation Trace Function Invocation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2

Trace Function Invocation animation Trace Function Invocation declare variable result

Trace Function Invocation animation Trace Function Invocation (num1 > num2) is true since num1 is 5 and num2 is 2

Trace Function Invocation animation Trace Function Invocation result is now 5

Trace Function Invocation animation Trace Function Invocation return result, which is 5

Trace Function Invocation animation Trace Function Invocation return max(i, j) and assign the return value to k

Trace Function Invocation animation Trace Function Invocation Execute the print statement

i is declared and initialized animation Trace Call Stack i is declared and initialized

j is declared and initialized animation Trace Call Stack j is declared and initialized

animation Trace Call Stack Declare k

animation Trace Call Stack Invoke max(i, j)

pass the values of i and j to num1 and num2 animation Trace Call Stack pass the values of i and j to num1 and num2

animation Trace Call Stack (num1 > num2) is true

animation Trace Call Stack Assign num1 to result

Return result and assign it to k animation Trace Call Stack Return result and assign it to k

Execute print statement animation Trace Call Stack Execute print statement

void Functions The preceding section gives an example of a nonvoid function. This section shows how to declare and invoke a void function. Listing 5.2 gives a program that declares a function named printGrade and invokes it to print the grade for a given score. TestVoidFunction

#include <iostream> using namespace std; void printGrade(double score) { if (score < 0 || score > 100) cout << "Invalid score"; return; } if (score >= 90.0) cout << 'A'; else if (score >= 80.0) cout << 'B'; else if (score >= 70.0) cout << 'C'; else if (score >= 60.0) cout << 'D'; else cout << 'F'; } int main() { cout << "Enter a score: "; double score; cin >> score; cout << "The grade is "; printGrade(score); return 0;

Pass by Value When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as pass-by-value. If the argument is a variable rather than a literal value, the value of the variable is passed to the parameter. The variable is not affected, regardless of the changes made to the parameter inside the function. We will examine an interesting scenario in the following example, in which the parameters are changed in the function but the arguments are not affected. TestPassByValue

#include <iostream> using namespace std; void increment(int n) { n++; cout << "n inside the function is " << n << endl; } int main() { int x = 1; cout << "Before the call, x is " << x << endl; increment(x); cout << "after the call, x is " << x << endl; return 0;

Pass by Value, cont.

TestReferenceVariable Reference Variables C++ provides a special type of variable, called a reference variable, which can be used as a function parameter to reference the original variable. A reference variable is an alias for another variable. Any changes made through the reference variable are actually performed on the original variable. To declare a reference variable, place the ampersand (&) in front of the name. For example, see Listing 5.4. TestReferenceVariable

#include <iostream> using namespace std; int main() { int count = 1; int &refCount = count; refCount++; cout << "count is " << count << endl; cout << "refCount is " << refCount << endl; return 0; }

Pass By Reference You can use a reference variable as a parameter in a function and pass a regular variable to invoke the function. The parameter becomes an alias for the original variable. This is known as pass-by-reference. When you change the value through the reference variable, the original value is actually changed. TestPassByReference

#include <iostream> using namespace std; void increment(int &n) { n++; cout << "n inside the function is " << n << endl; } int main() { int x = 1; cout << "Before the call, x is " << x << endl; increment(x); cout << "after the call, x is " << x << endl; return 0;

using namespace std; // Swap two variables void swap(int &n1, int &n2) { int temp = n1; n1 = n2; n2 = temp; } int main() int num1 = 1; int num2 = 2; // Invoke the swap function to attempt to swap two variables swap(num1, num2); system("pause"); return 0;

Overloading Functions The max function that was used earlier works only with the int data type. But what if you need to find which of two floating-point numbers has the maximum value? The solution is to create another function with the same name but different parameters, as shown in the following code: TestFunctionOverloading

#include <iostream> using namespace std; /** Return the max between two int values */ int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; } /** Find the max between two double values */ double max(double num1, double num2) { return num2;} /** Return the max among three double values */ double max(double num1, double num2, double num3) { return max(max(num1, num2), num3); } int main() { // Invoke the max function with int parameters cout << "The maximum between 3 and 4 is " << max(3, 4) << endl; // Invoke the max function with the double parameters cout << "The maximum between 3.0 and 5.4 is " << max(3.0, 5.4) << endl; // Invoke the max function with three double parameters cout << "The maximum between 3.0, 5.4, and 10.14 is " << max(3.0, 5.4, 10.14) << endl; return 0; }

TestFunctionPrototype Function Prototypes Before a function is called, it must be declared first. One way to ensure it is to place the declaration before all function calls. Another way to approach it is to declare a function prototype before the function is called. A function prototype is a function declaration without implementation. The implementation can be given later in the program. TestFunctionPrototype

#include <iostream> using namespace std; // Function prototype int max(int num1, int num2); double max(double num1, double num2); double max(double num1, double num2, double num3); int main() { // Invoke the max function with int parameters cout << "The maximum between 3 and 4 is " << max(3, 4) << endl; // Invoke the max function with the double parameters cout << "The maximum between 3.0 and 5.4 is " << max(3.0, 5.4) << endl; // Invoke the max function with three double parameters cout << "The maximum between 3.0, 5.4, and 10.14 is " << max(3.0, 5.4, 10.14) << endl; return 0; }

/** Return the max between two int values */ int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; } /** Find the max between two double values */ double max(double num1, double num2) /** Return the max among three double values */ double max(double num1, double num2, double num3) return max(max(num1, num2), num3);

Default Arguments C++ allows you to declare functions with default argument values. The default values are passed to the parameters when a function is invoked without the arguments. DefaultArgumentDemo

#include <iostream> using namespace std; void printArea(double radius = 1) { double area = radius * radius * 3.14159; cout << "area is " << area << endl; } int main() printArea(); printArea(4); return 0;

Reusing Functions by Different Programs One of the benefits of functions is for reuse. In the preceding sections, you declared functions and used them from the same program. To make the functions available for other programs to use, you need to place the functions in a separate file, called header file. By convention, the file has a .h extension. Programs use #include preprocessor directives to include header files in order to reuse the functions defined in the header file.

#include <iomanip> using namespace std; void PrintTitle() { cout<<"\n "; for(int i=1;i<=50;i++) cout<<"*"; //for(int j=1;j<=15;j++) cout<<endl<<setw(17)<<"**"<<setw(48)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(33)<<"C++ Programming Language"<<setw(15)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(30)<<" name: Rose J. Tomas"<<setw(18)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(30)<<" St. number: 20082293"<<setw(18)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(31)<<" Programming Exercise"<<setw(17)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(33)<<" Introduced To: Dr. Robinson"<<setw(15)<<"**"; cout<<"\n"; //system("pause"); }

#include <iostream> #include"PrintTitle #include <iostream> #include"PrintTitle.h" using namespace std; int main() { PrintTitle(); cout<<"\n ********* This program is to calculate the factorial of an integer **********\n"; // Prompt the user to enter an integer cout << "\n Please Enter an integer: "; int n; cin >> n; double fact=1; for (int i=1; i<=n;i++) fact=fact*i; cout<< "\n "<<n <<" ! = "<<fact<<endl<<endl; system("pause"); return 0; }

Case Study: Generating Random Characters, cont. RandomCharacter.h TestRandomCharacter

Scope of Variables A local variable: a variable defined inside a function. Scope: the part of the program where the variable can be referenced. The scope of a variable starts from its declaration and continues to the end of the block that contains the variable.

Scope of Local Variables, cont. You can declare a local variable with the same name multiple times in different non-nesting blocks in a function, but you cannot declare a local variable twice in nested blocks.

Scope of Local Variables, cont. A variable declared in the initial action part of a for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope limited in the loop body from its declaration and to the end of the block that contains the variable.

Scope of Local Variables, cont.

Global Variables C++ also allows you to use global variables. They are declared outside all functions and are accessible to all functions in its scope. Local variables do not have default values, but global variables are defaulted to zero. VariableScopeDemo

#include <iostream> using namespace std; int y; // Global variable, default to 0 void t1(); // function prototype void t2(); // function prototype int main() { t1(); t2(); system("pause"); return 0; } void t1() { int x = 1; cout << "x is " << x << endl; cout << "y is " << y << endl; x++; y++; } void t2()

Unary Scope Resolution If a local variable name is the same as a global variable name, you can access the global variable using ::globalVariable. The :: operator is known as the unary scope resolution. For example, the following code: #include <iostream> using namespace std; int v1 = 10; int main() { int v1 = 5; cout << "local variable v1 is " << v1 << endl; cout << "global variable v1 is " << ::v1 << endl; return 0; }

Static Local Variables After a function completes its execution, all its local variables are destroyed. Sometimes, it is desirable to retain the value stored in local variables so that they can be used in the next call. C++ allows you to declare static local variables. Static local variables are permanently allocated in the memory for the lifetime of the program. To declare a static variable, use the keyword static. StaticVariableDemo

#include <iostream> using namespace std; void t1(); // function prototype int main() { t1(); return 0; } void t1() static int x = 1; int y = 1; x++; y++; cout << "x is " << x << endl; cout << "y is " << y << endl;

Example Generate random character

#include <iostream> using namespace std; char getRandomCharacter(char ch1, char ch2) { return static_cast<char>(ch1 + rand() % (ch2 - ch1 + 1)); } int main() for(int k=1;k<=5;k++) {for(int i=1;i<6;i++) cout<< getRandomCharacter('A', 'Z'); cout<<endl;} system("pause"); return 0;

Math Functions

Benefits of Functions Write a Function once and reuse it anywhere. Information hiding. Hide the implementation from the user. Reduce complexity.

Inline Functions Implementing a program using functions makes the program easy to read and easy to maintain, but function calls involve runtime overhead (i.e., pushing arguments and CPU registers into the stack and transferring control to and from a function). C++ provides inline functions to avoid function calls. Inline functions are not called; rather, the compiler copies the function code in line at the point of each invocation. To specify an inline function, precede the function declaration with the inline keyword, as shown in Listing 5.18. InlineDemo

#include <iostream> using namespace std; inline void f(int month, int year) { cout << "month is " << month << endl; cout << "year is " << year << endl; } int main() { int month = 10, year = 2008; f(month, year); return 0;

Short Functions Not for long Functions Compiler Decision Inline functions are desirable for short functions, but not suitable for long functions that are called in multiple places in a program, because long inline functions will dramatically increase the executable code size when it is copied in multiple places. For this reason, C++ allows the compilers to ignore the inline keyword if the function is too long. So, the inline keyword is merely a request to the compiler, and it is up for the compiler to make the decision whether to honor it or ignore it.

Assignment Review Questions: 3, 4, 5, 6, 9(b,d), 11, 14, 15, 17, 20,. Programming Exercises: 2, 3, 5, 6, 10, 16, 21.