5: Functions

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 (B)

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 (A)</p> Signup and view all the answers

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

<p>False (B)</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 (B)</p> Signup and view all the answers

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

<p>True (A)</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 (C)</p> Signup and view all the answers

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

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

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

<p>True (A)</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 (D)</p> Signup and view all the answers

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

<p>True (A)</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 (D)</p> Signup and view all the answers

What is the primary purpose of the IEEE Standard 754?

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

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

<p>True (A)</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 (D)</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 (B)</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 (D)</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 (D)</p> Signup and view all the answers

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

<p>True (A)</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

Flashcards

String

A sequence of characters enclosed in double quotes. Examples include: "Hello, world!" or "12345".

Function

A function is a block of code that performs a specific task. Functions improve code organization and reusability, making programs easier to understand and modify.

return Statement

The return statement is used to send a value back from a function to where it was called. It's like sending a message back to the caller, carrying the result of the function's work.

Procedural Programming

Functions are used to break down large programs into smaller, modular pieces, each performing a specific task. This makes programs easier to understand, debug, and maintain.

Signup and view all the flashcards

Postcondition

A function's postcondition describes what is expected to be true after the function is executed. It ensures the function behaves as intended and produces the desired outcome.

Signup and view all the flashcards

Floating-point Number System

A system for representing numbers using a base, precision, and exponent range.

Signup and view all the flashcards

Precision (p)

The number of digits used to represent a floating-point number.

Signup and view all the flashcards

Smallest Exponent (emin)

The smallest possible exponent value in a floating-point system.

Signup and view all the flashcards

Largest Exponent (emax)

The largest possible exponent value in a floating-point system.

Signup and view all the flashcards

Normalized Representation

A representation of a floating-point number where the first digit is non-zero. It ensures a unique representation.

Signup and view all the flashcards

Floating-point Number Notation

A representation of a number in base β, using an exponent and digits.

Signup and view all the flashcards

F(β, p, emin, emax)

The set of all possible floating-point numbers that can be represented using a specific floating-point number system.

Signup and view all the flashcards

Underflow

The number 0 and all numbers smaller than βemin cannot be represented in normalized form. These values are underflow cases.

Signup and view all the flashcards

Multiple Representations

Different representations for the same decimal number can occur because of the choice of exponent.

Signup and view all the flashcards

Limitations of Floating-point Arithmetic

Floating-point arithmetic operations can produce rounding errors due to limited precision.

Signup and view all the flashcards

Floating Point Number Representation

Represents a number using a combination of a sign, exponent, and mantissa.

Signup and view all the flashcards

Exponent Bits

The number of bits used to represent the exponent part of a floating-point number. For example, a 32-bit floating-point number often uses an 8-bit exponent.

Signup and view all the flashcards

Mantissa Bits

The number of bits used to represent the mantissa part of a floating-point number. It holds the digits of the number.

Signup and view all the flashcards

Base of the number system (β)

The base of the number system used for computation within the computer. Usually, this is 2, representing the binary system.

Signup and view all the flashcards

Input/Output Base

The number system used for input and output values, typically base 10 (decimal).

Signup and view all the flashcards

Conversion

Converting values from one base system to another, for example, converting decimal numbers to binary for internal processing.

Signup and view all the flashcards

Decimal to Binary Conversion

A method to convert a decimal number to its binary representation.

Signup and view all the flashcards

Exponent Range

The range of values that can be represented by a floating-point number with a given number of bits for the exponent.

Signup and view all the flashcards

Smallest Non-Zero Value

The smallest possible non-zero value that can be represented using a given floating-point format.

Signup and view all the flashcards

Largest Value

The largest possible value that can be represented by a given floating-point format.

Signup and view all the flashcards

Set of Normalized Numbers

A set of normalized numbers, represented by F*(β, p, emin, emax ), where β is the base, p is the number of digits in the significand, emin is the minimum exponent, and emax is the maximum exponent.

Signup and view all the flashcards

Calculations with Floating-point Numbers

The process of adding two floating-point numbers involves adjusting exponents, performing binary addition on the significands, renormalizing, and rounding to the specified precision.

Signup and view all the flashcards

The IEEE Standard 754

An international standard (IEEE 754) that defines floating-point number formats and their arithmetic operations. It is widely used in computers for representing and manipulating real numbers.

Signup and view all the flashcards

Single Precision (float)

A 32-bit floating-point number format used to represent real numbers on computers. It allows for a smaller range of values compared to double-precision numbers.

Signup and view all the flashcards

Double Precision (double)

A 64-bit floating-point number format used to represent real numbers on computers. It allows for a wider range of values and higher precision compared to single-precision numbers.

Signup and view all the flashcards

Rounding in Floating-point Operations

The process of taking an exact result of an operation and rounding it to the nearest representable number within the defined precision of the floating-point system.

Signup and view all the flashcards

Single Precision Format Breakdown

The structure of the single-precision floating-point number format in the IEEE 754 standard. It uses 32 bits, divided into one sign bit, 23 bits for the significand, and 8 bits for the exponent.

Signup and view all the flashcards

Double Precision Format Breakdown

The structure of the double-precision floating-point number format in the IEEE 754 standard. It uses 64 bits, divided into one sign bit, 52 bits for the significand, and 11 bits for the exponent.

Signup and view all the flashcards

Why the Specific Bit Allocations?

The reason for specific bit allocations in the single and double-precision floating-point formats (IEEE 754). They are chosen to provide a balance between precision and range of representable values, while being efficient in terms of storage and performance.

Signup and view all the flashcards

void Function

A special type of function that does not return a value. Instead, it performs an action, like displaying output or modifying data.

Signup and view all the flashcards

Arguments of a Function

The input values that a function takes when it is called. They provide the data the function needs to do its job.

Signup and view all the flashcards

Return Type

Indicates the type of data that a function will return. It determines what kind of value the calling code can expect.

Signup and view all the flashcards

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

More Like This

Use Quizgecko on...
Browser
Browser