EECS 183.

Slides:



Advertisements
Similar presentations
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Advertisements

Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
1 CS161 Introduction to Computer Science Topic #9.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Fundamental Programming Fundamental Programming Introduction to Functions.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Bill Tucker Austin Community College COSC 1315
Elementary Programming Concepts Hannah Westra
Katherine Kampf / kkampf
Review 1.
User-Written Functions
REPETITION CONTROL STRUCTURE
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Formatting Output.
Predefined Functions Revisited
Bill Tucker Austin Community College COSC 1315
CS161 Introduction to Computer Science
Suppose we want to print out the word MISSISSIPPI in big letters.
Why exception handling in C++?
A Lecture for the c++ Course
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Lecture 07 More Repetition Richard Gesick.
User-Defined Functions
Lecture 4B More Repetition Richard Gesick
Functions Inputs Output
Arrays & Functions Lesson xx
Object Oriented Programming COP3330 / CGS5409
Functions BIS1523 – Lecture 17.
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Introduction to C++ Programming
Announcements General rules about homeworks
Announcements General rules about homeworks
We’re moving on to more recap from other programming languages
Computing Fundamentals
CISC124 Labs start this week in JEFF 155. Fall 2018
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Announcements Homework 1 will be assigned this week,
Chapter 6: User-Defined Functions I
Objectives You should be able to describe: The while Statement
Fundamental Programming
A First Program.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Predefined Functions Revisited
Announcements General rules about homeworks
CMSC 202 Lesson 6 Functions II.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CS31 Discussion 1D Fall18: week 2
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Introduction to Methods and Interfaces
Presentation transcript:

EECS 183

Upcoming Due Dates: Zyante readings – due before each lecture Project 1: 9/23 (two days!) by 6:00 pm (accepted until 11:59 pm) Submit Projects early for extra credit + 5% 2 days early (still 6 pm) TODAY! + 2.5% 1 day early (6 pm) Always check the website for updates Zyante : (not as strict, but make sure you complete all reading and exercises for each week) Explain the accepted until 12 thing

Moving onto content…

Definition of a Function A function is a list of statements that can be executed by referring to the function’s name Functions often take in input values, do some work, and return a value that will be used by whoever called the current function There are library functions (functions that already exist in different libraries available to you in C++) There are also user-defined functions (functions you create to develop your program) Int main is a function Show on board Draw simple drawing of the visual idea of a function (no code)

Functions Functions are essentially mini-programs within the larger program that do a specific piece of work They help reduce duplication of code You can reuse them with different parameters to complete a specific task Most important elements of a function: name, parameters/inputs, output, return type, task/body In my words:

User-defined Functions Functions are essentially mini-programs within the larger program that do a specific piece of work They help reduce duplication of code You can reuse them with different parameters to complete a specific task Most important elements of a function: name, parameters/inputs, output, return type, task/body In my words:

Setting up a function What should the function do? What descriptive name should it have? Will it take inputs? Why? What kind? Should it return a value? Why? What kind? How will it do the necessary work?

Function Practice Finish the following user-defined function, which says hello to whoever (the name) we pass into it as input ____ say_hello ( string _______ ) { cout << _______ << _______ << endl; } In this case, how do we already know the return type? We know it is void, because we don’t return anything

Function Practice Finish the following user-defined function, which says hello to whoever (the name) we pass into it as input void say_hello ( string _______ ) { cout << _______ << _______ << endl; } distinguish between name_in and something created inside the function That’s why the _in is useful

Function Practice Finish the following user-defined function, which says hello to whoever (the name) we pass into it as input void say_hello ( string name_in ) { cout << _______ << _______ << endl; } distinguish between name_in and something created inside the function That’s why the _in is useful “Hello “ Don’t forget the space

Function Practice Finish the following user-defined function, which says hello to whoever (the name) we pass into it as input void say_hello ( string name_in ) { cout << “Hello “ << _______ << endl; } Don’t forget the space Name_in as variable with value

Function Practice Finish the following user-defined function, which says hello to whoever (the name) we pass into it as input void say_hello ( string name_in ) { cout << “Hello “ << name_in << endl; }

Function Practice Finish the following user-defined function, which says hello to whoever (the name) we pass into it as input void say_hello ( string name_in ) { cout << “Hello “ << name_in << endl; return; //ends the function } You can also say return; But you can never return an actual type or object SHOW THIS ON THE BOARD

Function Practice Now let’s call this function from main We need to have established: The variable we will pass in Our function call void say_hello ( string name_in ) ; //function declaration int main(){ ______ name = “Jimmy”; say_hello( ____ ); } Notice declaration above main

Function Practice Now let’s call this function from main We need to have established: The variable we will pass in Our function call void say_hello ( string name_in ) ; //function declaration int main(){ ______ name = “Jimmy”; say_hello(“Jimmy”); } Notice declaration above main Could we call the function this way too?

Function Practice Now let’s call this function from main We need to have established: The variable we will pass in Our function call void say_hello ( string name_in ) ; //function declaration int main(){ ______ name = “Jimmy”; say_hello(“Jimmy”); } Notice declaration above main Better style to make the variable first Kind of like the equivalent of magic numbers Could we call the function this way too? Yes!

Full program void say_hello ( string name_in ); //function declaration int main(){ string name = “Jimmy”; say_hello(name); return 0; } void say_hello ( string name_in ) { cout << “Hello “ << name_in << endl; Discuss function declarations Why do we need the function declaration? Above main Everything must match exactly

Full program void say_hello ( string name_in ); //function declaration int main(void){ string name = “Jimmy”; say_hello(name); return 0; } void say_hello ( string name_in ) { cout << “Hello “ << name_in << endl; Notice I added void in int main Void means nothing is taken in The parenthesis can also be left empty ----- Meeting Notes (9/22/15 12:40) ----- return 0;

Full program void say_hello ( string name_in ); //function declaration int main(void){ string name = “Jimmy”; say_hello(name); return 0; } void say_hello ( string name_in ) { cout << “Hello “ << name_in << endl; Notice I added void in int main Void means nothing is taken in The parenthesis can also be left empty ----- Meeting Notes (9/22/15 12:40) ----- return 0;

Scope A variable can either have a local scope or a global scope Exists only within current function Global scope Exists for all functions in the program

Full program && Scope void say_hello ( string name_in ); //function declaration int main(void){ string name = “Jimmy”; say_hello(name); } void say_hello ( string name_in ) { cout << “Hello “ << name_in << endl; What is the scope of the variable name? Notice I added void in int main Void means nothing is taken in The parenthesis can also be left empty What is the scope of the variable name_in?

Global constants A global variable is declared outside of any and all functions Must be named in ALL CAPS Must be declared const (in this course) const double NUM_BAGS_SUGAR = 35; //function declarations //int main() //function implementations Remember: const means the variables cannot be changed anywhere in the program Remember: const means it cannot be changed anywhere in the program NEXT SLIDE: scope of NUM_BAGS_SUGAR Important for P1

Global constants A global variable is declared outside of any and all functions Must be named in ALL CAPS const double NUM_BAGS_SUGAR = 35; //function declarations //int main() //function implementations Remember: const means the variables cannot be changed anywhere in the program What is the scope of the variable NUM_BAGS_SUGAR? Remember: const means it cannot be changed anywhere in the program

Magic Numbers and Style What is a magic number?

Magic Numbers and Style What is a magic number? A number that is seemingly unexplained and has no obvious context Any number without a completely clear meaning should be placed into a variable with a descriptive name You will lose style points if the graders find numbers they consider to be magic numbers Example on board If you are writing a program to calculate the percentage of people out of the entire group (input read in) could fit in your car and your car has 7 seats Don’t just say (7 / group) * 100 Say int car_can_hold = 7 Group /car_can_hold

Magic Numbers and Style What in this line of code is a magic number? cout << “total eggs = “ << num_batches * 12; Example on board If you are writing a program to calculate the percentage of people out of the entire group (input read in) could fit in your car and your car has 7 seats Don’t just say (7 / group) * 100 Say int car_can_hold = 7 Group /car_can_hold

Magic Numbers and Style What in this line of code is a magic number? cout << “total eggs = “ << num_batches * 12; It turns out I need a dozen eggs per batch, but if someone just looked at this line of code, the calculation with the 12 would not be immediately obvious The fix: const int EGGS_PER_BATCH = 12; cout << “total eggs = “ << num_batches * EGGS_PER_BATCH; Example on board If you are writing a program to calculate the percentage of people out of the entire group (input read in) could fit in your car and your car has 7 seats Don’t just say (7 / group) * 100 Say int car_can_hold = 7 Group /car_can_hold

RME’S REQUIRES: MODIFIES: EFFECTS: Put an RME above any user-defined function you create These are special types of comments specific to functions Functions given to you will have RME’s RME’s are meant to aid the user A requires clause is the function’s requirements: “These are my requirements. If you give me values that don’t fit my requirements, I am not responsible for anything that goes wrong”. The caller of the function is responsible for making sure to fulfill the requires clause Students should Review these from lecture slides The source code in my folder Key point: the requires is the function essentially saying These are my requirements. If you give me something else, I’m not responsible for what might go wrong. It is up to the user (CALLER OF THE FUNCTION) to make sure the function is called correctly ----- Meeting Notes (9/22/15 12:51) ----- add required clasue info to slide

RME REVIEW

Fix this function int code_master(string n){ cout >> “Hello“ >> n >> endl “You are the code master!”; return n; } -- Int needs to be string -- n needs to be more descriptive (ie name) -- Cout << not >> -- no space after Hello -- Missing semicolon -- No cout for the code master sentence -- Return the new variable name. its type string ----- Meeting Notes (1/21/15 00:00) ----- 20 minutes talking leave 4 minutes for the fix this function questions after

Fix this function int code_master(string n){ cout >> “Hello_“ >> n >> endl_ _____ “You are the code master!”; return n; } -- Int needs to be string -- n needs to be more descriptive (ie name) -- Cout << not >> -- Missing semicolon -- No cout for the code master sentence -- Return the new variable name. its type string ----- Meeting Notes (1/21/15 00:00) ----- 20 minutes talking leave 4 minutes for the fix this function questions after

Corrected function: string code_master(string name_in){ cout << “Hello_“ << name_in << endl; cout << “You are the code master!”; return name_in; } -- Int needs to be string -- n needs to be more descriptive (ie name) -- Cout << not >> -- Missing semicolon -- No cout for the code master sentence -- Return the new variable name. its type string ----- Meeting Notes (1/21/15 00:00) ----- 20 minutes talking leave 4 minutes for the fix this function questions after

Why use a function? Helps reduce duplicated code Call more than once with new parameters to do the same work, but with new values One approach: If you know all the details about a function (RME) you can implement it… assuming everything else, including main, already works (even if it doesn’t yet) You can assume the value passed in is correct, or the function will be used in the right spot, even if you havent thought about it yet

Why use a function? Another approach: plan out the logic of your main() first you can assume all the functions already do what they are supposed to do… even though you haven’t implemented them yet! Helps organize your project and your code This is good for you, and also for anyone that reads or uses your code Some people like to write all their functions and then write main which will work quickly Others like to plan out main first, and then implement functions Its whatever you prefer!

Let’s write: an Addition Program We want a function that is called from main, and adds two integers together It returns the result of the addition of these two integers And we print this result back in main, not in the function Don’t skip! Keep track of how long taken so far We’ll come back if we have time! Do this on the board, and have the students call out what to do next Tricks for exams: If you see a return is needed, immediately put the return type of your function, and a possible return statement

Our Addition Program -- one way to do it int add (int a, int b); //function declaration int main(){ int x = 4; int y = 6; int result = add(x, y); cout << result << endl; } int add (int a, int b){ return a + b;

Our Addition Program -- another way to do it int add (int a, int b); //function declaration int main(){ int x = 4; int y = 6; cout << add (x, y) << endl; } int add (int a, int b){ return a + b;

Function Signature what is the function signature of our add function? int add (int a, int b){ return a + b; } Don’t skip this

Function Signature int add(int, int) or int add(int a, int b) what is the function signature of our add function? int add (int a, int b){ return a + b; } int add(int, int) or int add(int a, int b) The first of these is ONLY a valid function signature, declaration or prototype, not a valid function definition (which is when you actually implement it) The signature of a function are the unique qualities of the function so the return type the name and the parameter types ----- Meeting Notes (9/29/15 13:05) ----- add that these are only valid function signatures

Function Signature int add(int, int) or int add(int a, int b) The signature of a function is the combination of the unique/defining elements of the function Return type Name Parameter types The signature of a function are the unique qualities of the function so the return type the name and the parameter types

Scope practice What is an example of Local scope? What is the difference between a global variable, and a const global variable? Which do we NEVER use in this course?

Scope practice What is the difference between a global variable, and a const global variable? Which do we NEVER use in this course? We NEVER use global variables We do use global constants If you are going to use a variable with a global scope, it MUST be declared const, it must be in ALL CAPS and never be changed throughout the program ----- Meeting Notes (9/29/15 13:05) ----- const in all caps

In class exercise: Write a function that calculates what yard line the football lands on Pseudocode? Loops Conditions Difference between pseudocode and source code Code editor Compiler Object code (370) Return type Semicolons Cout cin Endl #include Using namespace std Variables Data types Operators Int division modulo

In “main” prompt the user to enter a number Call function with distance the ball was kicked as a parameter Returns the yard line the ball landed on Have ‘main” print out what yard line the ball landed on If it landed in the endzone, print out “Landed in endzone” Bonus! Now print out how far Jabril Peppers will have to run for a touchdown Pseudocode? Loops Conditions Difference between pseudocode and source code Code editor Compiler Object code (370) Return type Semicolons Cout cin Endl #include Using namespace std Variables Data types Operators Int division modulo

Pseudocode? Loops Conditions Difference between pseudocode and source code Code editor Compiler Object code (370) Return type Semicolons Cout cin Endl #include Using namespace std Variables Data types Operators Int division modulo

Any general questions about lecture material? Good luck finishing up P1 Try the S’more portion! Pseudocode? Loops Conditions Difference between pseudocode and source code Code editor Compiler Object code (370) Return type Semicolons Cout cin Endl #include Using namespace std Variables Data types Operators Int division modulo

Feel free to ask me any questions after class! Usually they’ll be more examples! I’ll be able to stay for about 20 minutes outside of the room