Notes

  • A procedure is a set of instructions that can take in parameters and give values.

Quiz

questionNum = 3
correct = 0
questions = [
    "What is are correct names for a procedure? \n A) Method \n B) Function \n C) Both",
    "What is a procedure? \n A) Sequencing \n B) Selection \n C) Iteration \n D) All",
    "Use this for following question: \n def inchesToFeet(lengthInches): \n\t lengthFeet = lengthInches / 12 \n\t return lengthFeet \n\n What is the procedure name, the parameter, and what the procedure returns? \n A) feetToInches, lengthInches, lengthMeters \n B) inchesToFeet, lengthInches, lengthFeet \n C) inchesToFeet, lengthFeet, lengthInches \n D) lengthInches, inchesToFeet, lengthFeet"]
answers = ["c", "d", "b"]

def qna(question, answer):
    print("Question:", question)
    response = input()
    print("Answer:", response)
    
    if response.lower() == answer:
        print("Correct :) \n")
        global correct
        correct += 1
    else:
        print("Incorrect :( \n")
for x in range(questionNum):
    qna(questions[x], answers[x])
    
print("Score:", correct, "/ 3")

Determining the Result of a Procedure

  • You can determine function parameters and value and statements with syntax.
  • A return statement will exit the function and allow python to execute the program. Example:
    def divide(num1,num2):
        x = num1/num2
        return x
    
  • Function parameter are used when calling function for a result.

Keyword

  • Modularity - breaking a complex program into smaller, independent parts, modules.
  • Abstraction - hiding the details of how a system works and exposing only the essential features.
  • Duplication - having multiple duplicate codes, decreasing efficiency.
  • Logic - the sequence of steps a computer follows to execute a program.

Parameter

  1. First define a function of variables. Ex: def add(x, y)
  2. Define the "action" you want for the variable. Ex: result = x + y
  3. For the rest of the program, you may use the "action" Ex: result = add(1, 2) returns you 3.

Modularity

  1. Define a function, same as in the parameter.
  2. Define the variable.
  3. return you answer by the imported math library.

Shared Features

  • Create a function that is shared by multiple variables.
  • Instead of do two separate functions to do similar action, you can make a function that do the same action for both variable.

Vocabulary:

  • Procedure - a module of code that is created to complete a task(a function).
  • Procedure Name - the name that is given to a function.
  • Parameters - a variable that is used in a function to import data.
  • Arguments - a way to provide information to a function, import with parameter.

Collegeboard

collegeboard-format

Above, the function is defined as PROCEDURE. The function is then named with procName, the name of function as display. parameter1, parameter2,... are variables that can be predefined elsewhere and repeatedly used in the same function. Block of statements are just things that you would place inside a function to complete a certain task, such as print().

Javascript vs. Python

Python

def function(a,b): # function is defined
  print(a+b) # prints output of variables

function(1,2) # one instance that it can be used
function(2,3) # another instance
3
5

Javascript

function Function(a,b) {
  return a + b;
}

Function(1,2)
Function(2,3)
3
5

Parameters

Hacks

Topic 3.12 (3.A):

  1. Define procedure and parameter in your own words
  2. Paste a screenshot of completion of the quiz
  3. Define Return Values and Output Parameters in your own words
  4. Code a procedure that finds the square root of any given number. (make sure to call and return the function)

Topic 3.13 (3.B):

  1. Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
  2. Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
  3. Add another layer of abstraction to the word counter program (HINT: create a function that can count the number of words starting with ANY character in a given string -- how can we leverage parameters for this?)

Topic 3.13 (3.C):

  1. Define procedure names and arguments in your own words.
  2. Code some procedures that use arguments and parameters with Javascript and HTML (make sure they are interactive on your hacks page, allowing the user to input numbers and click a button to produce an output)
    • Add two numbers
    • Subtract two numbers
    • Multiply two numbers
    • Divide two numbers