20
CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Embed Size (px)

DESCRIPTION

TIPS for After Midterm 1.Make sure that all of the marks in the midterm added up correctly.  Markers are humans after all. And they will make mistakes. 2.Make sure that you don’t forget to complete to bump up your mark.

Citation preview

Page 1: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

CPSC 217 T03Week VI

Part #1: A2 Post-Mortem and Functions

Hubert (Sathaporn) Hu

Page 2: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Today’s Tutorial

• Tips for what to do after the midterm.• A2 Post-Mortem• Functions

Page 3: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

TIPS for After Midterm

1. Make sure that all of the marks in the midterm added up correctly. Markers are humans after all. And they will

make mistakes.

2. Make sure that you don’t forget to complete to bump up your mark.

Page 4: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

A2 Post-Mortem

• Despite most people getting A in the assignment, I would like to go over for A2.

• I have found some bad programming and I would like to point out why they are bad.

Page 5: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

A2 Post-Mortem

1. Please make sure your code is functional before sending it. Some students have submitted codes that will crash when I execute it. I cannot stress the importance of this enough.

The consequence for submitting a non-functioning code can be quite severe.

Make sure that every change is tested.

Page 6: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

A2 Post-Mortem

2. Avoid complexity addiction. Some codes that I have seen are needlessly

complicated. In some case, students insisted on using for

loop even though while loop would make programs much simpler.

Some students use functions even if they aren’t required.

Don’t forget that longer code means I can penalize you more easily. Since you are bound to make more mistakes with longer code.

Page 7: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

A2 Post-Mortem

3. Variable naming: Use pothole_case for your variable:

• For instance, student_number is good while studentNumber is not.

• SimpleGraphics.py’s case is exception rather than the rule.

Some abbreviation is OK, but not extreme abbreviation:• prev_year is fine, but py is not.

Page 8: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

A2 Post-Mortem

4. Short variable names are also fine, sometimes: Although long variable names are encouraged,

sometimes short variable names are accepted:• Short variable names are fine for something that

would already have short names like coordinate, points, or variables in mathematical formulae.

• Short variable names are fine for variables used for loops and for data structure index.

Longer is generally better, but don’t go overboard with something like:• specific_dog_life_in_year

Page 9: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

A2 Post-Mortem

5. CONSTANT NAMING: Constant is a variable that is set once and

never changed again. In many programming language, your program

will fail if you attempt to reassign a constant. However, in Python, we use an honour system.

In Python, a constant uses ALL_CAP_POTHOLE_CASE. So FONT_SIZE is an OK name while font_size is not.

Page 10: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

A2 Post-Mortem

5. Space please: Please add some spaces between each blocks

of code. Also, add space between operators:

• x = x + 10 ** 9 - PI Like English, please add space between

commas. No space is needed for brackets though.• do_stuffs(100, 200, x, y)

Page 11: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• Did you know that you already used functions?• Examples of functions that you have seen:

setText(…) cos(…) abs(…) print(…)

• An act of using a function is called “calling a function.”

Page 12: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• You also get to create one!• But why do we need to create one?

We want to reuse a section of the code later. We want to reuse a section of the code with a bit

of a twist. We want to simplify complicated code for other

programmers. For instance, SimpleGraphics is a simplification of even more difficult graphic code.

Page 13: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• A function with no argument follows this body:def function_name():

// Code here// More code// More code

• Note that when you name your function, you use pothole_case. SimpleGraphics is using camelCase, but don’t listen to it.

• EXERCISE: Create a function that prints “Hello World!”

Page 14: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• A function can have one to multiple arguments as well. When you create such function, you can specify the arguments needed. An example:def say_hi_to(person_1, person_2):

print(“Hi,”, person_1, “and”, person_2)

• EXERCISE: Find out what this function does.• EXERCISE: Find out what happens if not

enough arguments are supplied. Find out what happens if too many arguments are supplied.

Page 15: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• You may notice that certain functions can accepts arbitrary amounts of arguments. The one that you often use is probably print.

• You can actually create such functions. However, this is too complicated for this course. Therefore, I won’t go over it.

Page 16: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• You may notice that you can get something out of certain functions. For instance, when you use cos(x), you get a cosine value of x.

• You can create such function as well with something called return.def find_c(a, b):

return (a ** 2 + b ** 2) ** (0.5)

• EXERCISE: Find out what this function does.

Page 17: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• You can add loops or if-statements, in your function as well. However, you are not allowed to: Nest a function inside a function. Nest a function inside a loop or if-statement.

Page 18: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• You can actually call a function inside itself. This is called recursion.

• However, since we haven’t talked about recursion yet, calling a function inside itself is strongly discouraged.

• When poorly done, calling a function inside itself can cause infinite recursion to happen. Infinite recursion is infinite loop’s evil cousin…

Page 19: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

Functions

• Don’t get me wrong, recursion is a good thing. It can be used to solve many problems and it’s quite fundamental to computer science.

• Why am I talking about recursion before I should talk about it? I saw some students inadvertently used recursion without knowing what it is.

• EXERCISE: Explain why this function will never stop running…def forever(x, y):

forever(x, y)

Page 20: CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu

What’s Next

• Next tutorial will be used for cramming Exercise #4.