Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.

Similar presentations


Presentation on theme: "Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3."— Presentation transcript:

1 Week 2

2 Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3

3 Functions (cont.): Unlike other languages, the c++ compiler must be made aware of functions before you can use them. E.g., this won’t work: #include using namespace std; int main() { int x = max3(4,7,3); // problem cout << x << endl; return 0; } //main int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3

4 Functions (cont.): When calling a function, the compiler must be aware of the function. You must declare a function before calling it. #include using namespace std; int max3(int x, int y, int z); // Here is our function declaration int main() { int x = max3(4,7,3); cout << x << endl; return 0; } //main int max3(int num1, int num2, int num3) {int result=max(max(num1,num2),num3); return result; } //max3 Declaration: The compiler can now handle most uses of the function without needing the full definition of the function. Declaring a function lets you write code the compiler can understand without all of the details. Especially useful when one file is using functions in another file

5 Function Arguments:  Default is call by value  This means that a copy of the actual value is copied into the parameter. void swap (int j, int k); int main() { int x = 7; int y = 3; swap (x,y); cout << x << endl; //what gets printed? cout << y << endl; return 0; } //main void swap (int num1, int num2) {int temp = num1; num1 = num2; num2 = temp; cout << num1 << endl; //what gets printed? cout << num2 << endl; } //main  Works this way for all primitive types (including strings)

6 void swap(int *x, int *y); // function declaration int main () { int a = 100; int b = 200; /* calling a function to swap the values. * &a gets the address of a * &b gets the address of b */ swap(&a, &b);//we’re not sending in the values, we’re sending in addresses in memory cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; } //main void swap(int *x, int *y) // *x is a variable that holds an address of another variable that holds an int { int temp; temp = *x; // save the value at address x *x = *y; // put y into x *y = temp; // put x into y return; } //swap Now we can change parameter values and they will stay changed outside of the function! Call by Pointer:  Copies the address of an argument into the parameter.  Inside the function, the address is used to access the actual value.  This means that changes made to the parameter affect the value.

7 & versus *  int x = 4; // this actually sets aside an address in memory at a location large enough to hold an int. It then puts the value 4 into that address.  cout << x << endl; // prints out the value at location x. Compiler assumes you want to use the value at that location, not the location.  cout << &x << endl; // this gives you access to the address of x in memory  int y = &x; // can’t do. The address of x is not an int, and y is automatically making a new address at a location in memory that will hold an int.  int *y = &x; //can do. Now I’m saying the variable y is an address that holds the address of another variable that holds an int  …or y points to a location that holds an int,  cout << y << “ “ << *y << endl; //gives you 0x32ff1c 4

8 & versus *  int x = 4;  cout << x << endl;  cout << &x << endl;  int *y = &x;  cout << y << endl;  cout << &y << endl;  cout << *y << endl;  //y points to x – gives us the value at //the address inside of y, not the value //inside of y x ( 0x32ff3c ) y ( 0x4f3d21 ) 4 Space in memory for an int 0x32ff3c Space in memory for a memory address (for an int) (getting address)

9 Call by value vs. by reference (pointers):  Call by Reference:  Can change variables from one function within the confines of another function  Makes code more readable  Reduces or eliminates the need for global variables  Better form (I was taught never to use global variables)  Easier to read  Don’t have variables sticking around when you’re done with them  Smaller memory footprint  We’re not making a new copy of each value passed into the function as a parameter. Why not always use Call by Reference?  Call by Value:  Can’t accidentally change a value in another function  Make sure, if you’re working with others, they don’t accidentally change the value  Maintain “privacy”

10 Random Numbers: #include … int x = rand(); // generates a random number between 0 and a max number defined in stdlib  Problem: if not given a specific seed, it will always start with the same seed. So you will get the same sequence of random numbers.  We want to give it a seed, and each time we run our program, we want a new seed. So we often use the current time as our new seed.  To generate a seed using the current time: #include … srand(time(NULL)) // creates a seed based on the current time (down to the millisecond) // Only need to create a seed once in a program // Must create seed BEFORE you use rand for the first time. int x = rand(); // now rand uses this seed in the calculation of the random number. // Now you won’t always get the same sequence of random numbers Question: How would you generate a random number between 1 and 10?

11 Math stuff:  #include  …  double d = 200.374;  int i = 100;  int j = -100;  cout << sin(d) << endl; // -0.634939  cout << squrt(i) << endl; // 10  cout << pow(i,2) << endl; // 10000  cout << floor(d) << endl; // 200  cout << abs(j) << endl; //100 - only works with ints  cout << fabs(d) << endl; //200.374 – only works with doubles and floats

12 Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0; float b[3] = {3.2,1.4};  makes an array of spaces for 7 ints; the array at 3 holds 24, rest holds nothing (if you print, will print their memory addresses ) int c[7]; c[3] = 24; cout << c[2] << endl; // prints something like 0x32ff3c, the memory address  Makes an array of 3 ints, holding 4,1,2 int d[] = {4,1,2};  Next: int d[]; d[0] = 4;  yeah, no. d[] is space for an address that will be where we hold the first int in the array, but so far no space has been set aside for the int, so there’s no address for the int.  Can we set aside space? Hold that thought…

13 Passing arrays into functions:  In terms of memory space, do we want to make a new, local copy of the array when we call a function?  To get the address of the array (aka a pointer to the array): int x[4] = {3,2,1,4}; cout << x << endl; //address of array, of where first value in array is cout << &x[0] << endl; //same value, address of first value in array

14 Arrays and Functions:  Note: no straightforward way of getting size of an array. We most likely want to pass in the length of the array along with a pointer to the array (aka the address of the array), e.g., int main() { int x[4]= {3,2,4,1}; double k = getAverage(&x[0],4); // gets the address of the first value in the array // Or double l = getAverage(x,4); // the address of the first value in the array cout << k << endl; } // main Function definition: double getAverage(int *arr, int size) // a pointer, a variable that holds an address {int sum = 0; for (int i = 0; i < size; i++) {sum += arr[i]; // now, in essence, the value at (the address of arr + i) } //for double avg = double(sum) / size; return avg; }//main Or double getAverage(int arr[], int size) { //holds the address of the first value in the array }

15 Returning an array int * createArray(int size) // what is returned??? { int r[size] = {3,2,4,1}; return r; // or return &r[0]; } //createArray int main() { int *a; a = createArray(4); for (int x = 0; x < 4; x++) { cout << a[x] << endl; } //for return 0; }//main

16 Dynamically allocated arrays  Means we make a pointer and then allocate the space for the values in the array  Maybe later, when we find out how many things will be in the array  Quick info: If the size of something is known when the program is compiled, then memory is set aside for it on the stack  int x = 3;  float y;  If the size of something is determined when the program is run, then there’s something called the heap (still in memory, but in a different place) that holds data. Stuff that goes on the heap is “dynamically allocated”.  The operator new sets aside space in the heap.  We often use pointers  If we allocate it, we should free it up

17 New and delete new int; // sets aside memory space on the heap for an int (but no variable points to it int *x; x = new int; // now x points to (holds the address of) space for an int on the heap *x = 34; // now the x points to 34; What if there’s not enough space on the heap? int *x; if( !(x = new int )) { cout << "Error: out of memory." <<endl; // checking to see if there’s space on the heap exit(1); } //if else { *x = 2; } //else And when you’re done with it… int *x; if( !(x = new int )) { cout << "Error: out of memory." <<endl; // checking to see if there’s space on the heap exit(1); } //if else { *x = 2; } //else delete x; //deallocates the space in memory

18 What about arrays? int *x = NULL; // Pointer initialized with null (default is undefined) cout << “Enter the number of numbers you want” << endl; int y; cin >> y; x = new int[y]; // Request memory on heap for space for 20 ints (sequentially) for (int k = 0; k < y; k++) { x[k] = k; } //for And now to free an array? delete [] x;

19 What about multi-dimensional arrays? int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 4x3 array for (int i = 0; i < 4; i++) { x[i] = new int[3]; } //for for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { x[i][j] = i+j; } //for for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { cout << x[i][j]; } //for cout << endl; } //for

20 How about deleting? int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 4x3 array for (int i = 0; i < 4; i++) { x[i] = new int[3]; } //for for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { x[i][j] = i+j; } //for Why can’t we just do this? delete [] x; // compiles just fine…

21 Deleting a multi-dimensional array int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 3x4 array for (int i = 0; i < 4; i++) { x[i] = new int[3]; } //for for (int i = 0; i < 4; i++) { delete [] x[i]; } //for delete [] x;

22 Quick: int x = 3; int y = x; int *z = &x; cout << “x is “ << x <<endl; cout << “ y is “ << y << endl; cout << “ z is “ << *z << endl; x += 4; cout << “x is “ << x <<endl; cout << “ y is “ << y << endl; cout << “ z is “ << *z << endl;

23 Structs  Arrays group many objects of the same type (in order)  Structs group objects of different types  Structs are similar to classes  Struct properties are, by default, public.  Structs often hold different types together (that logically belong together)  Example: struct StudentInfo { string fname; string lname; int id; }; //StudentInfo int main() { StudentInfo Sam; Sam.fname = "Sam"; Sam.lname = "Stone"; Sam.id = 3241; StudentInfo studarr[5]; studarr[1].fname = "Taylor"; cout << studarr[1].fname << endl; return 0; } //main

24 What do you think? struct StudentInfo { string fname; string lname; int id; }; //StudentInfo void changeStud(struct StudentInfo x) { x.fname = "Samantha"; } //changeStud int main() { StudentInfo Sam; Sam.fname = "Sammy"; Sam.lname = "Stone"; Sam.id = 3241; changeStud(Sam); cout << Sam.fname << endl; // what do you think? return 0; } //main

25 Call by Pointer struct StudentInfo { string fname; string lname; int id; }; //StudentInfo void changeStud(struct StudentInfo *x) { x->fname = "Sammy"; //new, means go to the name property in the address in x } //changeStud int main() { StudentInfo Sam; Sam.fname = "Sam"; Sam.lname = "Stone"; Sam.id = 3241; changeStud(&Sam); cout << Sam.fname << endl; // what do you think? return 0; } //main

26 Classes class Rect { int len; //default for properties and methods: private int wid; public: // everything below this is now public (unless I change it back) int calcArea(); // method DECLARATION void setLen(int x) { //method definition (setter) len = x; } void setWid(int x) { wid = x; } int getLen() { // getter return(len); } int getWid() { return(wid); } }; int Rect::calcArea() { // Rect method definition (in the scope of Rect) return len * wid; } int main() { Rect r; r.setLen(2); r.setWid(3); cout << r.getLen() << endl; cout << r.calcArea() << endl; return 0; } //main

27 Constructors class Rect { int len; int wid; public: Rect(int x, int y); //CONSTRUCTOR DECLARATION Rect(); //CONSTRUCTOR DECLARATION int calcArea(); void setLen(int x) { len = x; } void setWid(int x) { wid = x; } int getLen() { return(len); } int getWid() { return(wid); } }; Rect::Rect() { //CONSTRUCTOR ( no input parameters) len = 3; wid = 5; } Rect::Rect(int x, int y) { // CONSTRUCTOR (2 input parameters) len = x; wid = y; } int Rect::calcArea() { return len * wid; } int main() { Rect r(7,2); cout << r.getLen() << endl; cout << r.calcArea() << endl; return 0; } //main

28 Overloading methods… #include using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; pd.print(5); // Call print to print integer pd.print(500.263); // Call print to print float pd.print("Hello C++"); // Call print to print character return 0; }


Download ppt "Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3."

Similar presentations


Ads by Google