3.12 and 3.13- Calling Procedures/Developing Procedures

  • title: 3.12 and 3.13- Calling Procedures/Developing Procedures
  • toc: true
  • badges: true
  • layout: base
  • categories: [.tri 2 learning]

3.12/3.13- Calling Procedures/Developing Procedures

  • A procedure is a named set of instructions that can take in parameters and return values.
    • May be called "method" or "function" in different programming languages.
  • Parameters are independent variables used in the procedure to produce a result. It allows a procedure to execute without initially knowing specific input values.
  • To call a procedure you would write the name of the procedure followed by the parentheses with the parameters of the procedure
  • Procedures do not require parameters, but the parentheses must be there
  • Return values are
  • A return statement exits a function and instructs python to continue executing the program and to return a certain value
  • Value can be string, a tuple, or any other type that is being sent back to the main program
  • Modularity - the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program
  • Abstraction - the practice of hiding the details of how a particular code or system works and exposing only the essential features or functions that are necessary for other parts of the program to use
  • Duplication - having multiple duplicate code blocks, often decreasing readability and efficiency
  • Logic - the sequence of steps and operations that a computer follows to execute a program, including the specific instructions and decision-making processes built into the code
  • Procedure- a module of code that is created to complete a certain task, this is basically a function
  • Procedure Name- the name that is given to a function/procedure
  • Parameters- a variable that is used in a function to allow for data to be imported into a function
  • Arguments- a way to provide information to a function, usually defined outside a function and then imported into a function with parameters
from math import sqrt

def find_sqrt(num):
    result = sqrt(num)
    return result

input = int(input())
result = find_sqrt(input)

print("sqrt(", input, ") = ", result)
sqrt( 4 ) =  2.0
  1. Abstracting away your program logic into seperate, modular functions is effective because it makes the code much easier to understand. Coding becomes much more organized and overall more functional.
questionnumber = 3
clothes= []
questions = [
    "What shirt are you going to wear?",
    "What pants are you going to wear?",
    "What hat are you going to wear"
]

def clothesask(question):
    print("Question: ", question)
    response = input()
    print("Your response: ", response)
    clothes.append(response)

for number in range(questionnumber):
    clothesask(questions[number])

for i in clothes:
    if i == "Jacket" or i == "Jeans" or i == "Beanie":
        print(i, "It must be cold")
    elif i == "T-Shirt" or i == "Shorts" or i == "Baseball Hat":
        print(i, "It must be hot")
    else:
        print(i, "The weather is nice")
Question:  What shirt are you going to wear?
Your response:  jeans
Question:  What pants are you going to wear?
Your response:  jacket
Question:  What hat are you going to wear
Your response:  beanie
jeans The weather is nice
jacket The weather is nice
beanie The weather is nice

This code above is using abstraction made me able to combine the outputs of different input to make the code more lazy and easy to program. This is very important for large projects.

import string

def split_string(s):
   
    words = s.split(" ")


    new_words = []
    for word in words:
        if word != "":
           
            new_words.append(word)
    
    return words


def count_words_starting_with_letter(words, letter):
    count = 0
    
   
    for word in words:

        if word.lower().startswith(letter):
            count += 1
    
    return count


def count_words_starting_with_a_in_string(s):
    
    words = split_string(s)
    
  
    count = count_words_starting_with_letter(words, "a")
    
    return count

def count_words_starting_with_d_in_string(s):
    words = split_string(s)
    count = count_words_starting_with_letter(words, "d")
    return count 

letter_tested = input("What letter would you like to count?")
def count_words_starting_with_input_in_string(s):
    words = split_string(s)
    count = count_words_starting_with_letter(words, letter_tested)
    return count

def count_words_starting_with_letter_in_string(s):
    words = split_string(s)
    count_dictionary = {}
    for c in string.ascii_lowercase:
        count_dictionary[c] = count_words_starting_with_letter(words, c)
    return count_dictionary



s = "   This is  a  test  string! Don't you think this is cool? "
a_count = count_words_starting_with_a_in_string(s)
d_count = count_words_starting_with_d_in_string(s)
letter_count = count_words_starting_with_input_in_string(s)
count_dictionary_print = count_words_starting_with_letter_in_string(s)
print("Words starting with a:", a_count)
print("Words starting with d:", d_count)
print("Words starting with", letter_tested,":", letter_count)
print("\nChecking all letters of alphabet:")
for letter in count_dictionary_print:
    if count_dictionary_print[letter] != 0:
        print("Words starting with", letter,":", count_dictionary_print[letter])
Words starting with a: 1
Words starting with d: 1
Words starting with k : 0

Checking all letters of alphabet:
Words starting with a : 1
Words starting with c : 1
Words starting with d : 1
Words starting with i : 2
Words starting with s : 1
Words starting with t : 4
Words starting with y : 1

Hacks 3

  1. Procedure Names are the names that are used to call a function or define a function. It overall makes the code much more organized. Arguments are inputs that are used to outide the function and then they are called with the function with parameters.
  2. Addition

Subtraction

Multiply

Divide

Vocab

  • Parameter: A characteristic or value that describes a particular aspect of something.
  • Modularity: The degree to which a system, such as a computer program, is composed of distinct, independent units that can be developed, tested, and maintained separately.
  • Abstraction: The ability to focus on essential features of an object or system, while ignoring or hiding unnecessary details.
  • Duplication: The occurrence of multiple copies of the same code, data, or other elements in a system.
  • Arguments: a piece of information that is passed to a function, procedure, or program when it is called or invoked.
def add(x: int, y: int) -> int:
  return x + y

result = add(5, 10)
print(result)
15
from abc import ABC, abstractmethod

class Animal(ABC):
  @abstractmethod
  def make_sound(self):
    pass

class Dog(Animal):
  def make_sound(self):
    print("Bark")

class Cat(Animal):
  def make_sound(self):
    print("Meow")

dog = Dog()
dog.make_sound()  

cat = Cat()
cat.make_sound() 
Bark
Meow
def calculate_area(width: float, height: float) -> float:
  return width * height

def calculate_volume(width: float, height: float, depth: float) -> float:
  return width * height * depth


area = calculate_area(10, 5)
print(area) 


volume = calculate_volume(10, 5, 2)
print(volume)


volume = calculate_volume(width=10, height=5, depth=2)
print(volume)  
50
100
100