cse 1341 honors note set 2 1. overview java vs. c++ functions in c++ first programming packet ...
DESCRIPTION
Review 3TRANSCRIPT
1
CSE 1341 Honors
Note Set 2
2
Overview
Java vs. C++ Functions in C++ First Programming Packet Development Environment
3
Review
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!”;}
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
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; }}
7
Using Libraries What’s this #include stuff??
#include <iostream> 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 <iostream> 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
9
Functions
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
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
10
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 - &
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 - &
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 - &
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 bWhat would main display?
14
Function Prototypeint 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
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
Default Argumentsvoid 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
Default Argumentsvoid 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
Default Argumentsvoid 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
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
?