cs102: functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · pass by reference: square(x)...
TRANSCRIPT
![Page 1: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/1.jpg)
CS102: Functions
Cuiyuan Wang
October 10, 2015
![Page 2: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/2.jpg)
![Page 3: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/3.jpg)
f(x) = x2
![Page 4: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/4.jpg)
Using Functions
![Page 5: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/5.jpg)
Using Functions
![Page 6: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/6.jpg)
Implementing square(x)
//prototype
int square(int x);
//implementation
int square(int x){
int y = x*x;
return y;
}
• function and return must have same type• parameters can be of various types• use descriptive names, like square instead of f
![Page 7: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/7.jpg)
Pass-by-Value
![Page 8: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/8.jpg)
Using square(x)
#include <iostream>
int square(int x); //function prototype
int main(){
int a;
std::cin>>a;
int b = square(a);
std::cout<<"square("<<a<<")="<<b<<std::endl;
return 0;
}
//computes xˆ2
int square(int x){
int y = x*x;
return y;
}
![Page 9: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/9.jpg)
practice
• Write a function named check() that has three parameters.The first parameter should accept an integer number, andthe second and third parameters should accept adouble-precision number. The function body should displaythe values of data passed to the function.
• Write a function named rightTriangle() that accepts thelengths of two sides of a right triangle as the arguments aand b. The function should determine and return thehypotenuse, c, of the triangle.
![Page 10: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/10.jpg)
Pass by reference: square(x)
//prototype
void square(int x, int &y);
//implementation
void square(int x, int &y){
y = x*x;
}
• reference parameters are pointers• a function of type void doesn’t return anything• a function can have pass by value and pass by reference
parameters
![Page 11: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/11.jpg)
Pass-by-Reference
![Page 12: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/12.jpg)
Using square(x)
#include <iostream>
void square(int x, int &y); //function prototype
int main(){
int a, b;
std::cin>>a;
square(a, b);
std::cout<<"square("<<a<<")="<<b<<std::endl;
return 0;
}
//function for computing xˆ2
void square(int x, int &y){
y = x*x;
}
![Page 13: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/13.jpg)
practice
• implement the function addn that adds some value n to aninput parameter x. The user can set n, but it defaults to 0.
• write a function to sort 3 values and return the sortedvalues
![Page 14: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/14.jpg)
Default Args
//prototype
int square(int x=0);
//implementation
int square(int x=0){
int y = x*x;
return y;
}
• function can have unlimited number of parameters• function can have unlimited number of default args• defaults must come after regular parameters
![Page 15: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/15.jpg)
Default Args: using square(x)
#include <iostream>
int square(int x=0);
int main(){
std::cout<<"square(0)="<<square()<<std::endl;
return 0;
}
int square(int x=0){
int y = x*x;
return y;
}
![Page 16: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/16.jpg)
practice
• modify check() so that the last value defaults to 42• modify rightTriangle() so that if a side isn’t intered, it’s set
as 0
![Page 17: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/17.jpg)
Overloading
//prototypes
int square(int x);
double square(double x);
//implementation
int square(int x){
int y = x*x;
return y;
}
double square(double x){
double y = x*x;
return y;
}
• can overload based onparameter type
• can also overload basedon parameter count
![Page 18: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/18.jpg)
Overloading: using square(x)
int square(int x);
double square(double x);
int main(){
int w = square(2);
std::cout<<"square(2)="<<w<<std::endl;
double v = square(2.5);
std::cout<<"square(2.5)="<<v<<std::endl;
return 0;
}
//implementations
![Page 19: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/19.jpg)
practice
• overload check to accept all ints• overload rightTriangle() to accept doubles
![Page 20: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/20.jpg)
Templates
template <class T>
T square(T x){
T y;
y = x*x;
return y;
}
• the function implementation is the prototype• custom function created on function call• T is a stand in for any built in data type• Can be pass-by-value and/or pass-by-reference
![Page 21: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/21.jpg)
Templates: Multiple types
template <class T1, class T2>
void square(T1 x, T2 &y){
y = x*x;
}
• T1 and T2 are different types• function doesn’t have to return a templated type• function can return template type• Can be pass-by-value and/or pass-by-reference• T1, T2...are a convention, can be anything
![Page 22: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/22.jpg)
Using Templates
#include <iostream>
template <class T>
T square(T x){
return x*x;
}
int main(){
int w = square(2);
std::cout<<"square(2)="<<w<<std::endl;
double v = square(2.5);
std::cout<<"square(2.5)="<<v<<std::endl;
return 0;
}
![Page 23: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/23.jpg)
Variable Scope
• local scope• only seen by function it’s defined in• name can be reused in other functions
• global scope• entire program can see the variable• defined outside main• bad practice-don’t use unless required
![Page 24: CS102: Functionsvenus.cs.qc.cuny.edu/~cwang/102/lecture09.pdf · Pass by reference: square(x) //prototype void square(int x, int &y); //implementation void square(int x, int &y){y=x*x;}](https://reader034.vdocuments.site/reader034/viewer/2022050411/5fa007edfdb8e008687e551e/html5/thumbnails/24.jpg)
Variable storage allocation
auto int var1;
static int var2;
extern int var3;
auto (default) storage allocated on executionstatic storage allocated on compilation
extern storage can be seen by other files