More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Similar documents
Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Programmer-Defined Functions

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Chapter 4. Procedural Abstraction and Functions That Return a Value

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

C++ Basics - 3 Rahul

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Chapter void Functions. Overview. Functions for All Subtasks. void-functions. Using a void-function. void-function Definition

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Ch 4. Parameters and Function Overloading

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Pre- Defined Func-ons in C++ Review for Midterm #1

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Computer Science II Lecture 1 Introduction and Background

Void Functions Call-By-Reference Parameters Using Procedural Abstraction Testing and Debugging General Debugging Techniques

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

The following program computes a Calculus value, the "trapezoidal approximation of

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

CSCI 1061U Programming Workshop 2. Function Basics

Communication With the Outside World

File I/O CS 16: Solving Problems with Computers I Lecture #9

Dynamic Arrays Makefiles and Mul3ple File Compiles

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Function Call Example

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

Strings and Stream I/O

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Fundamentals of Programming CS-110. Lecture 2

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

CS101 PLEDGED SPRING 2001

CS2141 Software Development using C/C++ C++ Basics

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

C-String Library Functions

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

C++ Basics 1 CS 16: Solving Problems with Computers I Lecture #3

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

VARIABLES & ASSIGNMENTS

C++ Lab 02 - Command Line Arguments and Strings

Flow Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4

CSCI 123 Introduction to Programming Concepts in C++

Welcome to Solving Problems with Computers I

Lecture 7. Log into Linux New documents posted to course webpage

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

C++ for Java Programmers

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Binghamton University. CS-211 Fall Functions. The Basis of C

CSI33 Data Structures

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Lab: Supplying Inputs to Programs

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

C++ For Science and Engineering Lecture 12

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Structures and Classes CS 16: Solving Problems with Computers I Lecture #15

Chapter 13. Functions and Parameter Passing (Part 2)

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Chapter 3 Function Basics

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Ch 6. Functions. Example: function calls function

PROGRAMMING IN C++ CVIČENÍ

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

Program Organization and Comments

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

Introduction to Programming EC-105. Lecture 2

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Transcription:

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8 Ziad Matni Dept. of Computer Science, UCSB

Announcements Homework #7 due today Lab #4 is due on Monday at 8:00 AM! Homework Solu>ons are now online at: hap://cs.ucsb.edu/~zmatni/cs16/hwsolu>ons/ 10/20/16 Matni, CS16, Fa16 2

Lecture Outline Overloading func>on names in C++ void func>ons GeQng arguments from the OS command line 10/20/16 Matni, CS16, Fa16 3

Overloading Func>on Names C++ allows more than one defini*on for the same func*on name Very convenient for situa>ons in which the same func>on is needed for different numbers or types of arguments Overloading a func0on name: providing more than one declara>on and defini>on using the same func>on name 10/20/16 Matni, CS16, Fa16 4

Overloading Examples double ave(double n1, double n2) { return ((n1 + n2) / 2); } double ave(double n1, double n2, double n3) { return (( n1 + n2 + n3) / 3); } Compiler checks the number and types of arguments in the func>on call & then decides which func>on to use. So, with a statement like: cout << ave( 10, 20, 30); the compiler knows to use the second defini>on 10/20/16 Matni, CS16, Fa16 5

Overloading Details Overloaded func>ons Must have different numbers of formal parameters AND / OR Must have at least one different type of parameter Must return a value of the same type 10/20/16 Matni, CS16, Fa16 6

10/20/16 Matni, CS16, Fa16 8

More Overloading Func>ons Examples See textbook, Ch. 4.6, pp. 235 237 (Pizza buying program) There are two types of pizzas: circular and rectangle One overloaded func>on to calculate the unit price: unitprice Returns the unit price of a slice of pizza If you want to calculate the unit price of a circular pizza, call unitprice (diameter, price) If you want to calculate the unit price of a rectangular pizza, call unitprice (length, width, price) 10/20/16 Matni, CS16, Fa16 9

Automa>c Type Conversion C++ will automa>cally converts types between int and double in mul>ple examples Eg. If I divide integers, I get integers: 13/2 = 6 Eg. If I make on these a double, I a double: 13/2.0 = 6.5 It does the same with overloaded func>ons, for example, given the defini>on: double mpg(double miles, double gallons) { return (miles / gallons); } what will happen if mpg is called in this way? cout << mpg(45, 2) << miles per gallon ; The values of the arguments will automa>cally be converted to type double (45.0 and 2.0) 10/20/16 Matni, CS16, Fa16 10

Type Conversion Problem Let s keep the previous mpg func>on and then ADD the following defini>on in the same program (we ll overload the mpg func>on): int mpg(int goals, int misses) // returns the Measure of Perfect Goals { return (goals misses); } What happens if mpg is called this way now? cout << mpg(45, 2) << miles per gallon ; The compiler chooses the func>on that matches parameter types so the Measure of Perfect Goals will be calculated This can introduce confusion into the program! Do not use the same function name for unrelated functions 10/20/16 Matni, CS16, Fa16 11

10/20/16 Matni, CS16, Fa16 12

void Functions In a top-down design, we ll want to design subtasks, often implemented as functions. A subtask might produce: No value One value More than one value We ve know how to implement functions that return one value So what about the other cases? A void-function implements a subtask that returns no value or more than one value 10/20/16 Matni, CS16, Fa16 13

Simple void Func>on Example 10/20/16 Matni, CS16, Fa16 14

void Function Definition void function definitions vs. regular function definitions Keyword void replaces the type of the value returned void = no value is returned by the function The return statement does not include an expression Example: void show_results(double f_degrees, double c_degrees) { using namespace std; cout << f_degrees << degrees Fahrenheit is equivalent to << endl << c_degrees << degrees Celsius. << endl; } return; 10/20/16 Matni, CS16, Fa16 15

Calling void Functions void-function calls are executable statements They do not need to be part of another statement They end with a semi-colon Example: show_results(32.5, 0.3); NOT: cout << show_results(32.5, 0.3); 10/20/16 Matni, CS16, Fa16 16

Calling void Functions Same as the function calls we have seen so far Argument values are substituted for the formal parameters It is fairly common to have no parameters in void functions In this case there will be no arguments in the function call Statements in the function body are executed Optional return statement ends the function Return statement does not include a value to return Return statement is implicit if it is not included 10/20/16 Matni, CS16, Fa16 17

Example: Converting Temperatures Consider a function in a program that converts Fahrenheit temperatures to Celsius using the formula: C = (5/9) (F 32) What s the potential challenge here? Do you see the integer division problem? How do avoid the problem? 10/20/16 Matni, CS16, Fa16 18

10/20/16 Matni, CS16, Fa16 20

void-functions To Return or Not Return? Would we ever need a return-statement in a void-function if no value is returned? Yes: there are cases where we would! What if a branch of an if-else statement requires that the function ends to avoid producing more output, or creating a mathematical error? See example on next page of a void function that avoids division by zero with a return statement 10/20/16 Matni, CS16, Fa16 21

10/20/16 Matni, CS16, Fa16 22

The main Function The main function in a program is used like a void function So why do we have to end the program with a return statement? Because the main function is defined to return a value of type int, therefore a return is needed It s a matter of what is legal and not legal in C++ void main () is not legal in C++!! (this ain t Java) Most compilers will not accept a void main, but not all of them Solution? Stick to what s legal. The C++ standard also says the return 0 can be omitted, but many compilers still require it No compiler will complain if you have the return 0 statement in main Solution? Always include return 0 in the main function. 10/20/16 Matni, CS16, Fa16 23

10/20/16 Matni, CS16, Fa16 24

Command Line Arguments with C++ In C++ you can accept command line arguments These are arguments that are passed into the program from the OS command line See example in Lab 3 To use command line arguments in your program, you must add 2 special arguments in the main() func>on Argument #1 is the number of elements (argc) inside the next argument, which is an array (*argv[]) Argument #2 is a full list of all of the command line arguments: *argv[] In the OS, to execute the program, the command line form would be: $ program_name argument1 argument2 argumentn example: $ sum_of_squares 4 5 6 10/20/16 Matni, CS16, Fa16 25

Setup The main() func>on should be wriaen as either: int main(int argc, char* argv[]) or int main(int argc, char** argv) char* argv[] means: a pointer to an array of characters We ll be discussing pointers in more detail in another lecture 10/20/16 Matni, CS16, Fa16 26

DEMO: int main ( int argc, char *argv[] ) { cout << "There are " << argc << " arguments here:" << endl; for (int i = 0; i < argc; i++) cout << "argv[" << i << "] is : " << argv[i] << endl; return 0; } 10/20/16 Matni, CS16, Fa16 27

What If I Want an Argument That s a Number? All you get from the command- line is character arrays To treat an argument as another type, you have to convert it inside your program <cstdlib> library has pre- defined func>ons to help! Examples: atoi( ), atol( ), and atof( ) Convert a character array to an int, long, and double, respec>vely. Example: #include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) { for(int i = 1; i < argc; i++) cout << atoi(argv[i]) << endl; return 0; } 10/20/16 Matni, CS16, Fa16 28

TO DOs Homework #8 due Tuesday 10/25 Lab #4 Due Monday, 10/24, at 8 am Lab #5 will be posted by the end of the weekend 10/20/16 Matni, CS16, Fa16 29

10/20/16 Matni, CS16, Fa16 30