20TD02U_Functions and Methods - itnett/FTD02N GitHub Wiki

Remembering: "What is a function? List the parts of a function definition."

Function: A function is a block of code that performs a specific task, is reusable, and can be executed when called. Functions help to organize code, make it more readable, and allow for code reuse.

Parts of a Function Definition:

  1. Function Name: The name by which the function can be called.
  2. Parameters: Variables passed to the function.
  3. Body: The block of code that performs the task.
  4. Return Statement: The value or output that the function returns (optional).

Example Function Definitions: Se skriptet her

Understanding: "Describe the difference between a function and a method. Provide examples."

Difference Between Function and Method:

  • Function:
    • A function is a standalone block of code that can be called independently.
    • It is not bound to any object or class.

Example: Se skriptet her

  • Method:
    • A method is a function that is associated with an object or class.
    • It is called on an instance of a class and can access or modify the data within that instance.

Example: Se skriptet her

Applying: "Write a function to calculate the factorial of a number. Explain how it works."

Function to Calculate Factorial: Se skriptet her

Explanation:

  • The function factorial takes an integer n as an argument.
  • It checks if n is 0 or 1, in which case it returns 1 (base case).
  • If n is greater than 1, it returns n multiplied by the factorial of n - 1 (recursive case).
  • This process continues until the base case is reached, resulting in the product of all positive integers up to n.

Analyzing: "Analyze the efficiency of recursive vs. iterative function implementations."

Recursive Implementation: Se skriptet her

Iterative Implementation: Se skriptet her

Efficiency Analysis:

  • Recursive:

    • Space Complexity: O(n) due to call stack.
    • Time Complexity: O(n).
    • Pros: Simple and elegant for small n.
    • Cons: Can lead to stack overflow for large n.
  • Iterative:

    • Space Complexity: O(1) as no additional call stack is used.
    • Time Complexity: O(n).
    • Pros: More efficient in terms of memory.
    • Cons: Slightly more complex code compared to recursion.

Evaluating: "Evaluate the readability and maintainability of functions with different complexities."

Simple Function: Se skriptet her

Complex Function: Se skriptet her

Evaluation:

  • Readability:

    • Simple functions are easy to read and understand.
    • Complex functions require careful documentation and clear variable naming to maintain readability.
  • Maintainability:

    • Simple functions are easier to maintain due to their straightforward logic.
    • Complex functions should be broken down into smaller, manageable sub-functions to enhance maintainability.

Creating: "Design a set of reusable functions for a given application. Explain your design choices."

Application: Data Analysis

Reusable Functions:

  1. Load Data: Se skriptet her

  2. Clean Data: Se skriptet her

  3. Analyze Data: Se skriptet her

  4. Visualize Data: Se skriptet her

Design Choices:

  • Modularity: Each function performs a specific task, making the code easy to manage and reusable.
  • Clarity: Function names and parameters are self-explanatory, enhancing readability.
  • Documentation: Docstrings provide clear descriptions of each function’s purpose and usage.

Example Usage: Se skriptet her

This set of functions demonstrates a structured approach to data analysis, promoting code reuse and maintainability.