Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Written by: Dr. JJ Shepherd
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Guide To UNIX Using Linux Third Edition
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Written by: Dr. JJ Shepherd
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Hank Childs, University of Oregon
Values vs. References Lecture 13.
Test 2 Review Outline.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
4. Java language basics: Function
User-Written Functions
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
C Programming Tutorial – Part I
A Lecture for the c++ Course
Lecture 13 & 14.
Functions CIS 40 – Introduction to Programming in Python
Edvin von Otter & Todd Barker
Stack Data Structure, Reverse Polish Notation, Homework 7
Lecture 11 B Methods and Data Passing
CSC 253 Lecture 8.
Functions Inputs Output
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays & Functions Lesson xx
Arrays, For loop While loop Do while loop
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
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.
6 Chapter Functions.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Reference Parameters.
Functions Pass By Value Pass by Reference
Writing Functions.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
CS 144 Advanced C++ Programming January 31 Class Meeting
Functions Reasons Concepts Passing arguments to a function
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Class rational part2.
ITM 352 Functions.
Methods and Data Passing
Functions Chapter No. 5.
Presentation transcript:

Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya

What we hope to learn today ๏ Putting everything into a main file isn’t feasible ๏ Blocks of code that is easily referenced and usable ๏ What happens with repeated variable names? ๏ File I/O – Simple how do we read and write to a file?* * If we get to it

Functions ๏ A function is a group of statements that together perform a task. ๏ You have already come across at least one.

Functions ๏ Helps to separate out code instead of putting everything in one place ๏ Makes things more organized and easily referenceable ๏ Scope changes* help in keeping less cluttered memory 4 *We will come to what scope is later

Function definition Return value Returns only one thing at a time Can be anything (int, char, double) Function name has to be a new name never defined before with the same parameters Input parameters

When should we use functions? ๏ If you repeat code always use functions ๏ Always use function names that represent what the function does (good coding practice) - Int division_by_2 ( int num ) { return num / 2; } //Good code - Int abcd( int num ) { return num /2 ; } //Terrible code ๏ Naming doesn’t pertain only to functions though – variables as well 6

Mini Task Open Functions.cpp Make a new function add_me_three_times Make a new function add_me_four_times that uses add_me_twice 7

Passing multiple variables ๏ We’ve seen only one variable passed to a function ๏ Passing multiple variables is pretty much the same way - int func(int a, int b) { … } ๏ You can pass as many as you want ๏ Good coding practice is to use not more than 3 inputs 8

Functions and arrays ๏ Passing an array to a function - void func (int array[]) { … } ๏ There are other ways also - void func (int* array) { … } //using pointers ๏ For now we will use the first way only 9

Passing variables ๏ Variables can be passed to functions in two different ways - Pass by reference - Int func( int &a) { a = a * 2; return a; } - Pass by value - Int func (int a) {a = a * 2; return a; } ๏ The difference is the ‘&’ 10

Function defaults ๏ Sometimes you want the ability for functions to have default values - Void func (int a, int b = 2) { a = 3; b = b * 2;} ๏ Defaults have to be declared at the end of the function ๏ Check out what happens if you do it in the beginning 11

Mini Task Write a function that finds the average of a size 100 array of integers (int) unless otherwise specified 12

Scope ๏ Slightly complicated concept ๏ Born out of memory necessity ๏ Now useful to keep things easily organized

How does any program run? 14

Program stack ๏ Part of the memory where all things static are stored as and when needed ๏ Stack is LIFO 15

How does the code run? 16

Work our way through the program 17 X = 5 Y = 5 I = 0 Y = 5 X = 6

Mini Task We will walk through it together Break points and value checking

FILE I/O ๏ Add ways to read and write to files ๏ Why do we want to do that? ๏ Standard libraries help us out and do a lot of the heavy lifting 19

FILE I/O – Writing to a file 20 Variable name of the file handler File name Type of access Mode Flag Same way that we use cout can be used here

FILE I/O – Reading from a file ๏ Pretty much the same concept ๏ Only some things are flipped 21

File mode flag 22 Mode FlagDescription ios::appAppend mode. All output to that file to be appended to the end. ios::ateOpen a file for output and move the read/write control to the end of the file. ios::inOpen a file for reading. ios::outOpen a file for writing. ios::truncIf the file already exists, its contents will be truncated before opening the file.

Mini Task Execute the file part of Functions.cpp Does everything work? 23

Questions? 24

Assignment ๏ Do the functions assignment.cpp ๏ If you finish quickly enough do the advanced one ๏ If you finish that let me know and I will give you some more challenging stuff