Announcements General rules about homeworks

Slides:



Advertisements
Similar presentations
CS201 – Introduction to Computing – Sabancı University 1 Announcements l General rules about homeworks ä Use of global variables (variables defined outside.
Advertisements

1 Chapter 9 Scope, Lifetime, and More on Functions.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Announcements HW1 grades are announced at SUCourse You may see Belal Amro at his office hour for homework grades at FENS 2014 on Wednesday 10:40-12:30.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
A Computer Science Tapestry 1 Announcements l Midterm 1 is on November 25, 2006, 10:40 – 12:20 l Homework 3 is due November 8, 2006, 21:00 ä Robot application.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
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.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Control Statements
Chapter 6: User-Defined Functions I
Jie(Jay) Wang Week1 Sept. 30
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 1.2 Introduction to C++ Programming
CSE 220 – C Programming C Fundamentals.
Computing and Statistical Data Analysis Lecture 2
Announcements Homework 1 is due on February 22nd, this Wednesday, at 19:00 Submit to SUCourse Strictly follow the submission guideline provided in the.
Bill Tucker Austin Community College COSC 1315
Computing Fundamentals
Basic Elements of C++.
Announcements General rules about homeworks
Introduction to C++ October 2, 2017.
User-Defined Functions
Announcements 2nd homework is due this week Wednesday (October 18)
Basic Elements of C++ Chapter 2.
User-defined Functions
Announcements Homework 1 is due on October 4th, this Wednesday, at 19:00 Submit to SUCourse Strictly follow the submission guideline provided in the homework.
Chapter 9 Scope, Lifetime, and More on Functions
User Defined Functions
2.1 Parts of a C++ Program.
Announcements General rules about homeworks
Programming Funamental slides
Announcements Midterm2 Grades to be announced NEXT Monday
User-defined Functions
Announcements 3rd homework is due this week Wednesday (March 15)
Course websites CS201 page link at my website: Lecture slides
Chapter 6: Repetition Statements
Summary Two basic concepts: variables and assignments Basic types:
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
CS150 Introduction to Computer Science 1
Announcements Homework 1 will be assigned this week,
Chapter 6: User-Defined Functions I
Chapter 2: Introduction to C++.
Summary of what we learned yesterday
Announcements Homework 1 will be assigned this week,
Predefined Functions Revisited
Announcements General rules about homeworks
CS 144 Advanced C++ Programming January 31 Class Meeting
(Dreaded) Quiz 2 Next Monday.
Single-Result Functions & Modularity
Subject:Object oriented programming
Announcements HW1 is due TODAY.
CMSC 202 Lesson 5 Functions I.
Announcements Homework 1 will be assigned this week
Presentation transcript:

Announcements General rules about homeworks HW1 deadline extended to Saturday. New (2nd) homework will be assigned next week will be due following week 22nd July Wednesday 19:00 About strings and loops Common Submission Mistakes In HW1, some students submitted empty/wrong files Make sure that you send the cpp file; otherwise we cannot grade Please submit the required files only, not the entire project folder 7z, rar and tar format is not allowed for compression; please use zip Do not use blanks, Turkish characters, special symbols in the filenames Only English alphabet letters, digits and underscore are allowed General rules about homeworks Use of global variables (variables defined outside of functions) prohibited No abrupt program termination in the middle of the program. Modularity and code duplication are important Code duplication must avoided Please write comments to your programs.

Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between function to be called and caller function by means of parameters currently one-way from caller function into function to be called We will see how to return data back to the caller function

Function Prototype (from 2.6) Functions definition has two parts function heading name, parameters, return type function body (local variables and statements within curly brackets) void display (string name) { cout << “Hello ” << name << endl; } Like variables, a function must be declared before its first call Problem of function declaration order You cannot call a function before you declare it SOLUTION: You may define function prototypes (a copy of the heading) at the beginning without function declarations

Function Prototype – Example Problem What is the problem below (program order.cpp) ? void Hi (string name) { cout << "Hi " << name << endl; Greetings(); } void Greetings() { cout << "Things are happening inside this computer" << endl; } int main() { Hi("Fred"); return 0; } Greetings() is called in Hi() but it is declared afterwards

Function Prototype – Solution Add function prototypes to the beginning (order2.cpp) #include <iostream> #include <string> using namespace std; void Hi(string); void Greetings(); void Hi (string name) { cout << "Hi " << name << endl; Greetings(); } void Greetings() { cout << "Things are happening inside this computer" << endl; } int main() { Hi("Fred"); return 0; } Prototypes Function Declarations

Function Prototypes *** Do not forget semicolon after the prototype definition *** no semicolon after the parameters in normal definition Sometimes prototypes are not necessary if the order of function calls allows but it is good programming practice to have them Parameter names are not needed in prototypes but it is OK if you have the parameter names In #included files we have the functions’ prototypes only implementations of function bodies are in libraries or in other cpp files they are linked together

Functions that return values Functions we’ve written so far are void functions They do a job and return back to caller, but without a value Parameters are used for one-way data transfer into the function to be called How about transfer a computed value out of a function? to the main program or to other function (the caller) Non-void functions can return values of any type function call becomes an expression when the function finishes, the function call is replaced by the returned value this way, values computed in functions can be transferred into the caller functions void function call is not used in an expression, i.e. no value is associated with a void function Head(); DoThat(); Verse("cow", "moo");

Functions that return values Example (see area_func_return.cpp): suppose circlearea function takes the radius as parameter and returns the area. In the program we call circlearea as an expression (you have to use the returned value somewhere) area = circlearea(r); cout << circlearea(12) << endl; if (circlearea(r/2) >= 100) { cout << “large circle” << endl; } circlearea(r); //syntax ok, but meaningless because //function call returned value is not used.

Math library functions Mathematical functions like square root, logarithm, sin, cos, etc. Prototypes are in header file cmath #include <cmath> Full list is in page 758 (Table F.1) – partial list is in table 4.5. correction in Table F.1: int abs (int x) Keep these math library functions on your cheat-sheet for the exam Example use of function sqrt see usemath.cpp how did we use sqrt function? in cout as an expression could we use sqrt in assignment? How? yes, let’s do it. what happens if value is negative? try and see! we can add some if statements to display an error message in case of negative value

Function Syntax return-type func-name(parameters) { local variables statements } Example: Function to calculate volume of a sphere double SphereVol(double radius) { return 4.0*radius*radius*radius*acos(-1)/3; } function body

Function Syntax double SphereVol(double radius) { return 4.0*radius*radius*radius*acos(-1)/3; } Function heading/prototype shows return type. return type can be any type (including string) Function body may have several statements in it return statement is used to determine the value returned from function, so the expression after it must be of the return type Function body must include at least one return statement The return statement causes the function to exit immediately and to return the value after return A function can have more than one return statements, but only one is executed when the function is called (see next example) Only one return is a good programming style to have control of bigger functions

Functions can return strings string WeekDay(int day) // precondition: 0 <= day <= 6 // postcondition: return "Sunday" for 0, // "Monday" for 1, // … "Saturday" for 6 { if (0 == day) return "Sunday"; else if (1 == day) return "Monday"; else if (2 == day) return "Tuesday"; else if (3 == day) return "Wednesday"; else if (4 == day) return "Thursday"; else if (5 == day) return "Friday"; else if (6 == day) return "Saturday"; } A program piece that uses that function string dayName; int dayNum; cout << " enter day (0-6): "; cin >> dayNum; dayName = WeekDay(dayNum); Which is/are correct use of WeekDay function? Why? cout << WeekDay(5) << endl; int j = WeekDay(0); cout << WeekDay(2.1) << endl; string s = WeekDay(22); WeekDay(3);

Function documentation Functions usually have a precondition What conditions (e.g. value of parameters) must be true for the function to work as intended? If there are no parameters, then no precondition Some functions work for every parameter value no precondition Functions always have a postcondition If precondition is satisfied what does the function do? What does the function return?

Example – Compare cost of pizza sizes Problem: Calculate and compare price per square inch of large and small size pizzas Solution: A function, say Cost, that takes the pizza radius and price as parameters and returns price per square inch In main() input radiuses and prices of large and small pizzas calculate the per square inch costs by calling the cost function display the results on screen compare the unit costs to find out which one is best value See pizza2.cpp

Example - When is a year a leap year? Every year divisible by four is a leap year Except years divisible by 100 are not Except years divisible by 400 are Alternatively: Every year divisible by 400 is a leap year Otherwise, years divisible by 100 are not leap years Otherwise, years divisible by 4 are leap years Otherwise, not a leap year Boolean function bool IsLeapYear(int year); // pre: year > 0 // post: return true if year is a leap year

Implementation and use of leap year function bool IsLeapYear(int year) // precondition: year > 0 // postcondition: returns true if year is a leap year, else returns false { if (year % 400 == 0) // divisible by 400 { return true; } else if (year % 100 == 0) // divisible by 100 { return false; } else if (year % 4 == 0) // divisible by 4 { return true; } return false; } int main() { int year; cout << "enter a year "; cin >> year; if (IsLeapYear(year)) { cout << year << " has 366 days, it is a leap year" << endl; } else { cout << year << " has 365 days, it is NOT a leap year" << endl; } return 0; } See isleap.cpp

There’s more than one way No if/else necessary in the function body bool IsLeapYear(int year) // precondition: year > 0 // post: return true if year is a leap year { return ( year % 400 == 0 ) || ( year % 4 == 0 && year % 100 != 0); } How does this work? Is this version more efficient? Are these two versions different from user perspective?