Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECS 183.

Similar presentations


Presentation on theme: "EECS 183."— Presentation transcript:

1 EECS 183

2 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

3 Moving onto content…

4 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)

5 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:

6 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:

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

8 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

9 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

10 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

11 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

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

13 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

14 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

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

16 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!

17 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

18 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;

19 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;

20 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

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

22 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

23 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

24 Magic Numbers and Style
What is a magic number?

25 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

26 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

27 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

28 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

29 RME REVIEW

30 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

31 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

32 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

33 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

34 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!

35 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

36 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;

37 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;

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

39 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

40 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

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

42 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

43 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

44 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

45 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

46 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

47 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


Download ppt "EECS 183."

Similar presentations


Ads by Google