5: Functions
39 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the purpose of functions in programming?

  • To encapsulate frequently used functionality (correct)
  • To manage memory allocation
  • To handle errors
  • To create complex algorithms
  • Procedural programming is another term for object-oriented programming.

    False

    What does the function intervals_overlap check for?

    It checks if two intervals overlap.

    The program computes the n-th power of a number using a loop, where the result is stored in a variable called ______.

    <p>result</p> Signup and view all the answers

    Match the following programming terms with their descriptions:

    <p>Function = Encapsulates a block of reusable code Procedural Programming = Structured programming based on procedures or functions Interval = A range between two numbers Loop = Repeatedly executes a block of code</p> Signup and view all the answers

    What does the symbol 'β' represent in a floating-point number system?

    <p>The base of the number system</p> Signup and view all the answers

    The normalized representation of a floating-point number can have multiple forms.

    <p>False</p> Signup and view all the answers

    What are the four natural numbers that define a floating-point number system?

    <p>β, p, emin, emax</p> Signup and view all the answers

    In a floating-point representation, the number 0 has ______ normalized representation.

    <p>no</p> Signup and view all the answers

    Match the components of a floating-point number representation with their descriptions:

    <p>d0 = Leading digit in normalized form p = Number of places in precision emin = Smallest possible exponent emax = Largest possible exponent</p> Signup and view all the answers

    Which of the following best describes normalized representation in floating-point numbers?

    <p>A unique form of representation with d0 not equal to 0</p> Signup and view all the answers

    The notation F (β, p, emin, emax) encompasses all the floating-point numbers.

    <p>True</p> Signup and view all the answers

    What is the smallest possible exponent in a floating-point number defined by the notation F (β, p, emin, emax)?

    <p>emin</p> Signup and view all the answers

    Floating-point numbers can be represented in base ______.

    <p>β</p> Signup and view all the answers

    How is a floating-point number structured according to its representation?

    <p>± d0 d1...dp−1 × β e</p> Signup and view all the answers

    What does the 32-bit representation of a floating point number include?

    <p>Sign bit, Exponent, Mantissa</p> Signup and view all the answers

    The binary system is used internally by computers to perform calculations.

    <p>True</p> Signup and view all the answers

    What is the base used for binary representation?

    <p>2</p> Signup and view all the answers

    Binary representations require conversion from the _____ system.

    <p>decimal</p> Signup and view all the answers

    Match the components with their correct descriptions in a floating-point number:

    <p>Sign bit = Indicates the sign of the number Exponent = Determines the range of values Mantissa = Represents the precision of the number Floating point = Number that can represent fractions</p> Signup and view all the answers

    During conversion, what is the first step in computing the binary representation of a number?

    <p>Identify the integer part</p> Signup and view all the answers

    The mantissa includes bits after the binary point in a floating-point number.

    <p>True</p> Signup and view all the answers

    What representation do computers use for internal calculations?

    <p>binary</p> Signup and view all the answers

    The mantissa in a floating point number is represented as _____ following the exponent.

    <p>1.xxxxxx</p> Signup and view all the answers

    What range does the exponent cover in the standard 32-bit floating-point format?

    <p>-126 to 127</p> Signup and view all the answers

    What is the primary purpose of the IEEE Standard 754?

    <p>To standardize floating-point number systems</p> Signup and view all the answers

    Single precision floating-point numbers in IEEE 754 have 32 bits in total.

    <p>True</p> Signup and view all the answers

    What is the minimum exponent value for double precision numbers in the IEEE Standard 754?

    <p>-1022</p> Signup and view all the answers

    The first number in the normalized representation F ∗ (β, p, emin , emax ) is called the ______.

    <p>base</p> Signup and view all the answers

    How many bits are used for the significand in double precision format?

    <p>52 bits</p> Signup and view all the answers

    In floating-point addition, the exponents of the numbers must always be the same before performing binary addition.

    <p>False</p> Signup and view all the answers

    What is the highest exponent value for single precision floating-point numbers in IEEE 754?

    <p>127</p> Signup and view all the answers

    Match the following terms with their descriptions related to IEEE Standard 754:

    <p>Sign bit = Indicates if the number is positive or negative Significand = Contains the significant digits of the number Exponent = Defines the scale or magnitude of the number Special values = Includes representations for 0 and infinity</p> Signup and view all the answers

    Normalized representation in floating-point numbers often involves scaling to the leading digit which is ______.

    <p>1</p> Signup and view all the answers

    When adding binary floating-point numbers, which of the following steps is NOT needed?

    <p>Convert to decimal before adding</p> Signup and view all the answers

    What must a function that does not have a return type of void include?

    <p>A return statement</p> Signup and view all the answers

    The return value of a function is always converted to its return type.

    <p>True</p> Signup and view all the answers

    What is the return type of the function 'bin_digits'?

    <p>int</p> Signup and view all the answers

    The function call used in programming examples is typically structured as _____(functionName)(parameters).

    <p>my_fun</p> Signup and view all the answers

    Study Notes

    Introduction to Computer Science

    • Course details: 252-0032, 252-0047, 252-0058
    • Authors: Manuela Fischer and Felix Friedrich
    • Department: Computer Science, ETH Zurich
    • Semester: Fall 2024

    Functions

    • Functions encapsulate frequently used code.
    • They partition programs into smaller subtasks.
    • Functions are also known as procedures.
    • Using functions for programming is called procedural programming.
    • A function's declaration is T fname (T₁ pname1, T2 pname2, ..., Tn pnamen) { ... } where T is the return type, pname are formal parameters, and the body is the code block.
    • A function is declared separately from its implementation, by giving the return type, name, and parameters. The implementation gives the function body.
    • Functions that return a value are different from functions that only execute side effects (like printing to the console).
    • Pre-conditions specify what the input and environment must be before a function is executed.
    • Post-conditions specify what the output and the state of the environment must be after a function execution, given valid preconditions.

    Example: Computing Powers

    • Example program computes the nth power of a number.
    • The code receives a double value for a and an integer n.
    • If n is negative, the code converts a to its reciprocal and n to its positive value to calculate a^n.
    • A for loop calculates the result.
    • The result is printed to the console.

    Defining Functions

    • Formal definition: T fname (T₁ pname1, T2 pname2, ..., Tn pnamen) { ... }
    • T is the return type of the function.
    • pname are the formal parameter names.
    • body is the code block executed when the function is called.

    Function Calls

    • Function calls provide functionality.
    • Arguments to a function must conform to the formal parameter types.
    • Function calls are R-values.
    • Function arguments are copied into formal parameters (pass-by-value).
    • Changes to the formal parameters do not alter the original arguments.
    • A function call is an expression (with the value returned by the function).

    Evaluation of Function Calls

    • Arguments are evaluated first.
    • Formal parameters are initialized with the evaluation results.
    • The function body executes.
    • A return statement (or reaching the end of the function body) ends function execution.
    • The return value (or the implicit return) is the function call's value.

    Scope of Formal Parameters

    • Formal parameters are only visible within the function.
    • Their values do not affect the original calling arguments.

    Pass by Value

    • Function arguments are copied into formal parameters.
    • Changes to formal parameters do not alter the original arguments.

    Pre- and Postconditions

    • Preconditions describe the state required before a function executes (e.g. e >= 0 or b != 0 for pow).
    • Postconditions describe the state after the function execution (assuming valid preconditions) (e.g. return value is be).
    • Pre- and post-conditions help with function documentation and correctness.
    • Pre- and post-conditions are used to specify the input (preconditions) and expected output (postconditions) of a function.

    Assertions

    • Assertions are a tool to check pre- and post-conditions, halting execution if a condition is false.
    • Assertions are used to validate function inputs and outputs (e.g., assert (e >= 0 || b != 0).
    • Assertions can be used to verify the preconditions or post-conditions of a function.

    Stepwise Refinement

    • Stepwise refinement is a program design technique.
    • Start with a high-level description of the program.
    • Divide the program into smaller, solvable, tasks.
    • Repeatedly refine the solution to smaller problems, ultimately implementing all the tasks.

    Libraries

    • Libraries package similar functions together.
    • Standard libraries provide essential functions (like mathematical functions, see cmath).
    • Separate compilation is used to leverage functions from other files.
    • Functions from libraries, like pow(), are called using their namespaces (e.g., std::pow).

    Name Spaces

    • Name spaces prevent naming conflicts between similar functions (e.g., pow() in std and informatik namespaces).
    • The standard namespace std is used for standard library functions.
      • Namespaces are used to group related identifiers (like functions) and prevent naming conflicts.
    • Header files (.h) contain declarations, not implementations (only function signatures)

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    This quiz covers the fundamental concepts of functions in computer science, highlighting their role in procedural programming. You'll learn about defining functions, encapsulating code, and executing operations like computing powers. Test your knowledge on function definitions and uses in programming.

    More Like This

    Use Quizgecko on...
    Browser
    Browser