introduction to programming (in c++) subprograms ...jordicf/teaching/programming/pdf/ip05... ·...
TRANSCRIPT
![Page 1: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/1.jpg)
Introduction to Programming(in C++)
Subprograms:procedures and functions
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Dept. of Computer Science, UPC
![Page 2: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/2.jpg)
Subprograms
• Programming languages, in particular C++, not only provide a set of basic operations and statements, but also a means to define our ownoperations and statements.
• We call the operations and statements that we define functions and procedures, respectively.
• Procedures and functions (subprograms) may have parameters. These represent the objects from our program that are used in the subprogram.
Introduction to Programming © Dept. CS, UPC 2
![Page 3: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/3.jpg)
Subprograms
• Functions are defined as follows:
Introduction to Programming © Dept. CS, UPC 3
int times(int x, int y) {
// Code
}
ParametersType of result
Name of the function
It must include a return statement
![Page 4: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/4.jpg)
Subprogramsint times(int x, int y) {
int p = 0;
while (y > 0) {
if (y%2 == 0) {
y = y/2;
x = x2;
}
else {
p = p + x;
y = y – 1;
}
}
return p;
}
Introduction to Programming © Dept. CS, UPC 4
![Page 5: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/5.jpg)
Subprograms
• Procedures are defined similarly, but without delivering any result:
Introduction to Programming © Dept. CS, UPC 5
void factors(int x) {
// Code
}
No result
![Page 6: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/6.jpg)
Subprograms
void factors(int x) {
int f = 2;
while (x != 1) {
if (x%f == 0) {
cout << f << endl;
x = x/f;
}
else f = f + 1;
}
}
Introduction to Programming © Dept. CS, UPC 6
![Page 7: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/7.jpg)
Subprograms
• Subprogram definitions may appear before or after the main program.
Introduction to Programming © Dept. CS, UPC 7
#include <iostream>using namespace std;
int f() {// Code for f
}
int main() {// Code for the main program
}
void p(int a) {// Code for p
}
![Page 8: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/8.jpg)
Subprograms
• A function can only be used if previously declared. A function can be declared and used before its code is defined.
Introduction to Programming © Dept. CS, UPC 8
double volume_sphere(double radius);
void some_geometry() {... double V = volume_sphere(1.0);...
}
double volume_sphere(double radius) {return 4Piradiusradiusradius/3;
}
![Page 9: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/9.jpg)
Subprograms
• Once a subprogram has been declared, it can be used.
– Functions are used as operations within expressions.
– Procedures are used as statements.
Introduction to Programming © Dept. CS, UPC 9
i = times(3, i + 2) + 1; // function
...
factors(i); // procedure
...
![Page 10: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/10.jpg)
Subprograms
• Appropriate use of subprograms:
– Increases readability: programs are better structured and easier to understand.
– Enables the use of abstraction in the program design.
– Facilitates code reuse.
Introduction to Programming © Dept. CS, UPC 10
![Page 11: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/11.jpg)
Subprograms
• Evaluating the expression
times(3, i + 2) + 1
means executing the code of times over the arguments 3 and i+2 and then adding 1 to the result returned by the function.
Introduction to Programming © Dept. CS, UPC 11
![Page 12: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/12.jpg)
Subprograms
• Evaluating the statement
factors(i);
means executing the code of factors over the argument i.
Introduction to Programming © Dept. CS, UPC 12
![Page 13: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/13.jpg)
Subprograms: parameter passing
• When a subprogram is called, the arguments are passed to the subprogram, so that its code can be executed:
times(3, i + 2) + ...
int times(int x, int y) { … }
• Each argument must have the same type as its corresponding parameter.
Introduction to Programming © Dept. CS, UPC 13
![Page 14: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/14.jpg)
Subprograms: parameter passing
• In general, any expression can be the argument of a subprogram:
Introduction to Programming © Dept. CS, UPC 14
double maximum(double a, double b);
...
z = maximum(x, y);...
r = maximum(3, gcd(s - 4, i) + alpha);
...
m = maximum(x, maximum(y + 3, 2Piradius));
![Page 15: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/15.jpg)
Subprograms: parameter passing• An object (a variable) is associated with a value and a
memory location. In C++, there are two methods for parameter passing:
– Passing the value (call-by-value). This is denoted by just declaring the type and the name of the parameter.
– Passing the memory location (call-by-reference). This is denoted by adding the symbol & next to the parameter type.
void p(int x, int& y) { ... }
Introduction to Programming © Dept. CS, UPC 15
Call-by-value Call-by-reference
![Page 16: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/16.jpg)
Subprograms: parameter passing• Call-by-value makes a copy of the argument at the
beginning of the subprogram. It is equivalent to having, a statement that assigns the value of each argument to the corresponding parameter:
Introduction to Programming © Dept. CS, UPC 16
times(3, i + 2)
is equivalent to:
int times(int x, int y) {
x = 3; y = i + 2;
int p = 0;
...
}
![Page 17: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/17.jpg)
Subprograms: parameter passing
• The effect of call-by-reference is that the parameter becomes the same object (variable) as the argument, i.e., the parameter becomes an alias of the argument.
• Example: procedure to swap the value of two variables
Introduction to Programming © Dept. CS, UPC 17
void exchange(int& x, int& y) {
int z = x;
x = y;
y = z;
}
![Page 18: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/18.jpg)
Subprograms: parameter passing
Introduction to Programming © Dept. CS, UPC 18
exchange(a, b)
Is equivalent to having:
void exchange(int& x, int& y) {
int z = a;
a = b;
b = z;
}
![Page 19: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/19.jpg)
Subprograms: parameter passing
int x, divisor;
bool p;
...
cin >> x;
p = is_prime(x + 3, divisor);
...
Introduction to Programming © Dept. CS, UPC 19
bool is_prime(int n, int& d) {
d = 2;
bool prime = (n != 1);
while (prime and d < n) {
if (n%d == 0) prime = false;
else d = d + 1;
}
return prime;
}
n: d:
prime:x:
divisor:
p:
6
9
2
true
3
false
false
// Pre: n >= 1// Post: returns whether n is prime.// If it is not prime, d is a divisor.
Warning: we do not recommend the use of non-voidfunctions with reference parameters in this course.
false
![Page 20: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/20.jpg)
Subprograms: parameter passing
• Use call-by-value to pass parameters that must not be modified by the subprogram.
• Use call-by-reference when the changes made by the subprogram must affect the variable to which the parameter is bound.
• In some cases, call-by-reference is used to avoid copies of large objects, even though the parameter is not modified.
Introduction to Programming © Dept. CS, UPC 20
![Page 21: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/21.jpg)
Subprograms: parameter passing
• To define a subprogram that, given two integers x and y, returns their quotient and remainder, we can write:
Introduction to Programming © Dept. CS, UPC 21
void div(int x, int y, int& q, int& r) {
q = x/y;
r = x%y;
}
![Page 22: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/22.jpg)
Subprograms: parameter passing
• For instance, if the parameters would be passed by reference in the function times, after the execution of the statements:
int a = 4;int b = 2;int c = times(a, b);
the value of a would be 0 and the value of b would be 8 (and the value of c would be 8).
Introduction to Programming © Dept. CS, UPC 22
![Page 23: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/23.jpg)
Subprograms: parameter passing
• For instance, after the definition:
void exchange(int x, int y) {int z = x;x = y;y = z;
}
the statement exchange(a,b) would not have any effect on a and b.
Introduction to Programming © Dept. CS, UPC 23
![Page 24: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/24.jpg)
Subprograms: parameter passing
• A call-by-value parameter can receive any expression as an argument.
• A call-by-reference parameter can only be bound to variables.
Introduction to Programming © Dept. CS, UPC 24
void exchange (int& a, int& b);
...
exchange(a, b + 4);
Incorrect parameter passing.
![Page 25: Introduction to Programming (in C++) Subprograms ...jordicf/Teaching/programming/pdf/IP05... · •Programming languages, in particular C++, not only provide a set of basic operations](https://reader035.vdocuments.site/reader035/viewer/2022081802/5cb695e588c993a7738bac43/html5/thumbnails/25.jpg)
The Least Common Multiple (LCM)
• Design a function that calculates the LCM of two numbers. Assume that we can use a function gcd(a,b) that calculates the greatest common divisor.
Introduction to Programming © Dept. CS, UPC 25
// Pre: a>0, b>0
// Returns the LCM of a and b
int lcm(int a, int b) {
return (a/gcd(a,b))b;
}