Notes

  • To call a prodecure you have to use partheness and the parameter
  • To determine the result of a procedure or any code, you must follow the code line by line and see what each one does
  • Parameters can be used to make functions work with multipble inputs

Vocab

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.

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

Arguements: a way to provide information to a function, usually defined outside a function and then imported into a function with parameters

Hacks

3.12 A

Procedure: is like a bunch of intrusticions that can take in different values

Parameter: an indepentent var that is used in procedures to get a different result

quiz

Return Values: are the bottom part of the function that have the return next them

Output Values: the value of whatever is outputed

import math

# Function
def findsqrt(x):
    sq = math.sqrt(x)
    return sq

x = 16

# Function printed two different ways
print(findsqrt(x))
print(findsqrt(25))
print(findsqrt(int(input("input a number to find its sq root"))))
4.0
5.0
10.0

3.13 B

  • If you abstract your coding it makes you type less and have less code and make you a better more lazier coder

  • Since we just did math these functions find powers of a certin root, you can change the root and how much you want it for the next value

number = int(input("Pick a number"))
secondpower = int(input("What power do you want to also see"))

# main function combines the two
def main_function():
    result = sub_function1()
    print(result)
    result2 = sub_function2(result)
    return result2

# finds the squre
def sub_function1():
    result = number * number
    return result

# can change to whatever root you want but will find the cubed
def sub_function2(result):
    p = 0
    while p < (secondpower - 2):
        result = result * number
        p += 1
        if p == (secondpower - 2):
            break
    return result


print(main_function())
9
243

Hack 3

def count_words_starting_with(string, character):
    # Split the string into a list of words
    words = string.split()

    # Initialize a counter variable
    count = 0

    # Iterate over the words and check if each word starts with the given character
    for word in words:
        if word[0] == character:
            count += 1

    return count

3.13 C Hacks

  • procedure names are just the names that you give procedures
  • arugments are a value that is passed when a function is called

Addition

Subtraction

Multiply

Divide