Functions

Functions Overview

  • Functions are essential building blocks in programming, particularly in Python.

  • They allow for modular programming by encapsulating code into callable units.

Built-in Functions

  • Characteristics of Built-in Functions:

    • Miniature programs that:

      • Receive input

      • Process the input

      • Return output

    • Output is typically a single value, referred to as the function's return value.

  • Examples of Python Built-in Functions:

    • int: Converts a float to an integer.

      • Example: int(2.6) returns 2.

    • chr: Converts an integer to its corresponding ASCII character.

      • Example: chr(65) returns 'A'.

    • ord: Returns the ASCII value of a given character.

      • Example: ord('A') returns 65.

    • round: Rounds a number to a specified decimal place.

      • Example: round(2.34, 1) returns 2.3.

User-defined Functions

  • Definition of User-defined Functions:

    • Created using a specific syntax.

    • Syntax includes a header ending with a colon and an indented block of statements.

  • Key Elements:

    • Parameters: variables in the function header (e.g., par1, par2).

    • Return Statements: optional, used to return a value from a function.

  • Passing Parameters:

    • Functions match arguments in the calling statement to parameters based on order.

  • Naming Functions:

    • Function names should be descriptive to reflect their purpose.

Functions with Parameters

  • Single Parameter Functions:

    • Functions can be defined with one parameter, e.g., fahrenheitToCelsius.

    • These handle a specific input and perform calculations.

  • Passing Values to Functions:

    • If a variable is passed as an argument, Python passes the object it points to, irrespective of the variable itself.

    • Distinction between immutable (e.g., tuples) and mutable (e.g., lists) data structures:

      • Immutable types are treated similarly to pass-by-value.

      • Mutable types adhere to pass-by-reference behavior.

Function Calls: Pass-by-Value vs. Pass-by-Reference

  • Pass-by-Value:

    • A copy of the variable is passed; changes do not affect the original.

  • Pass-by-Reference:

    • A reference to the memory address of the variable is passed, changes affect the original object (given objects are mutable).

  • In Python, it’s effectively "pass by object reference."

Scope of Variables

  • Local Scope:

    • Variables defined within a function exist only within that function and cease to exist outside of it.

  • Scope Example:

    • Different functions can have variables of the same name without conflict.

  • Global Variables:

    • If a variable is defined at the top of a program, it’s accessible globally:

      • Can be read by any function, but not altered without declaration.

Named Constants

  • Used to define specific unchangeable values, represented in uppercase.

  • Serves as a convention to enhance code readability.

Library Modules

  • Modules in Python:

    • Modules are files with the .py extension.

    • They contain functions and can be imported into other programs using import moduleName.

  • Common Modules:

    • os: File manipulation functions (e.g., delete/rename files).

    • pickle: Used for storing and retrieving objects.

    • random: Allows for random number generation.

Top-Down Design

  • Concept: Break down complex problems into manageable subproblems, known as stepwise refinement.

  • Criteria for Effective Top-Down Design:

    • Should focus on readability and small module size.

    • Tasks should be organized from general to specific.

    • Subtasks should be focused and independent.

Advantages of Structured Programming

  • Benefits:

    • Simplifies writing and debugging of code.

    • Enhances overall understanding and maintainability.

Object-Oriented Programming

  • Definition of Objects:

    • Objects encapsulate data and functions that operate on that data.

    • Key characteristics include properties, methods, and events.

Upcoming Due Dates

  • Exam 2: November 12

  • Assignment 3: Due November 17

  • Quiz 2: Due November 24