Download presentation
Presentation is loading. Please wait.
Published byCamilla Stevens Modified over 9 years ago
1
CSE 1341 Honors Note Set 2 1
2
Overview Java vs. C++ Functions in C++ First Programming Packet Development Environment 2
3
Review 3
4
Main Function Free floating main function in C++. Doesn’t have to be inside a class public class JavaProgram { public static void main(String [] args) { System.out.println(“Hello World!”); } int main() { cout << “Hello World!”; } 4
5
Compiling java compiles to bytecode that is later interpreted by the JVM Should theoretically be able to transfer even compiled code to a different machine – Platform Independence C++ compiles to machine code that is directly executable on the machine Compiled code will only run on one OS/Machine type Executable is not necessarily universally portable 5
6
Syntax Formatting No real major difference – More of coding conventions to deal with Where do we put braces? Usually on the next line Does it really matter? No int main() { for (int j = 0; j < 10; j++) { cout << “Hello World!” << endl; } 6
7
Using Libraries 7 What’s this #include stuff?? #include Anything with a # is handled by the C++ Preprocessor iostream is a library of io functionality there is actually a file in the file system named iostream the contents of the iostream file are effectively copied into the place of the #include other things can appear between the <> such as fstream, iomanip, etc. similar to an import in Java using namespace std; everything that is in the iostream file is part of the std namespace Namespaces allow grouping of entities like classes, objects and functions using one name Allows global scope to be subdivided, with each subdivision having its own name
8
Functions 8
9
9 int myfunction (int x, int y) { int sum = 0; for (int j = x; j < y; j++) sum += j; return sum; } int main() { int sum = myfunction(5, 10); cout << sum << endl; return 0; } arguments parameters
10
Pass By Value 10 int myfunction (int x, int y) { x = 60; y = 600; return x + y; } int main() { int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0; } 5 10 a b 5 x y x and y have copies of a and b changing x or y does not affect a or b
11
Pass By Reference - & 11 int myfunction (int& x, int& y) { x = 60; y = 600; return x + y; } int main() { int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0; } 5 10 a b x y x and y refer back to their arguments, a and b changing x or y DOES affect a or b
12
Pass By Reference - & 12 int myfunction (int& x, int& y) { x = 60; y = 600; return x + y; } int main() { int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0; } 60 10 a b x y x and y refer back to their arguments, a and b changing x or y DOES affect a or b
13
Pass By Reference - & 13 int myfunction (int& x, int& y) { x = 60; y = 600; return x + y; } int main() { int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0; } 60 600 a b x y x and y refer back to their arguments, a and b changing x or y DOES affect a or b What would main display?
14
Function Prototype 14 int myfunction(int&, int&); int main() { int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0; } int myfunction (int& x, int& y) { x = 60; y = 600; return x + y; } Before a function can be called, the compiler must be told about its legal format Function definition can be placed before the call, or we can use a prototype Function Prototype allows the compiler to determine if a function call is legal Sort-of like a function declaration (like a variable declaration) Prototype
15
15 Default Function Arguments Default Arguments are arguments that are passed to parameters automatically if no argument is provided in the function call void displayStars(int = 10, int = 1);
16
16 Default Arguments void displayStars(int=10, int=1); int main() { displayStars(); displayStars(5); displayStars(7,3); return 0; } void displayStars(int cols, int rows) { for (int down=0; down<rows; down++) { for (int across=0; across<cols; across++) cout << “*”; cout << endl; }
17
17 Default Arguments void displayStars(int=10, int=1); int main() { displayStars(); displayStars(5); displayStars(7,3); return 0; } void displayStars(int cols, int rows) { for (int down=0; down<rows; down++) { for (int across=0; across<cols; across++) cout << “*”; cout << endl; }
18
18 Default Arguments void displayStars(int=10, int=1); int main() { displayStars(); displayStars(5); displayStars(7,3); return 0; } void displayStars(int cols, int rows) { for (int down=0; down<rows; down++) { for (int across=0; across<cols; across++) cout << “*”; cout << endl; }
19
19 Default Function Arguments Rule When an argument is left out of a function call, all arguments that come after it must also be left out. displayStars(,3)//Illegal someFunc(1,,3)//Illegal
20
20 ?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.