CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Simplest program and Data Output Sen Zhang. The simplest program looks like a single person company! void main() { // this starts a comment line // here.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Introduction to Java.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Chapter 6: User-Defined Functions
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Basic Program Construction
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
CSI 3125, Preliminaries, page 1 Compiling the Program.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
A Sample Program #include using namespace std; int main(void) { cout
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
Lecture 3: Getting Started & Input / Output (I/O)
C++ Lesson 1.
A Lecture for the c++ Course
CSC113: Computer Programming (Theory = 03, Lab = 01)
Going from C++ to Java Jayden Navarro.
Addis Ababa Institute of Technology Yared Semu May 2012
Chapter 9 Scope, Lifetime, and More on Functions
User Defined Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Name: Rubaisha Rajpoot
More About Data Types & Functions
Anatomy of a Function Part 1
C++ Compilation Model C++ is a compiled language
Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.
Arrays Arrays A few types Structures of related data items
Functions Imran Rashid CTO at ManiWeber Technologies.
Anatomy of a Function Part 1
Presentation transcript:

CSE 1341 Honors Note Set 2 1

Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

Review 3

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

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

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

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

Functions 8

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

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

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

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; } a b x y x and y refer back to their arguments, a and b changing x or y DOES affect a or b

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; } 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?

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 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 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 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 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 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 ?