Podcast
Questions and Answers
What is the purpose of functions in programming?
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.
Procedural programming is another term for object-oriented programming.
False (B)
What does the function intervals_overlap check for?
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 ______.
The program computes the n-th power of a number using a loop, where the result is stored in a variable called ______.
Match the following programming terms with their descriptions:
Match the following programming terms with their descriptions:
What does the symbol 'β' represent in a floating-point number system?
What does the symbol 'β' represent in a floating-point number system?
The normalized representation of a floating-point number can have multiple forms.
The normalized representation of a floating-point number can have multiple forms.
What are the four natural numbers that define a floating-point number system?
What are the four natural numbers that define a floating-point number system?
In a floating-point representation, the number 0 has ______ normalized representation.
In a floating-point representation, the number 0 has ______ normalized representation.
Match the components of a floating-point number representation with their descriptions:
Match the components of a floating-point number representation with their descriptions:
Which of the following best describes normalized representation in floating-point numbers?
Which of the following best describes normalized representation in floating-point numbers?
The notation F (β, p, emin, emax) encompasses all the floating-point numbers.
The notation F (β, p, emin, emax) encompasses all the floating-point numbers.
What is the smallest possible exponent in a floating-point number defined by the notation F (β, p, emin, emax)?
What is the smallest possible exponent in a floating-point number defined by the notation F (β, p, emin, emax)?
Floating-point numbers can be represented in base ______.
Floating-point numbers can be represented in base ______.
How is a floating-point number structured according to its representation?
How is a floating-point number structured according to its representation?
What does the 32-bit representation of a floating point number include?
What does the 32-bit representation of a floating point number include?
The binary system is used internally by computers to perform calculations.
The binary system is used internally by computers to perform calculations.
What is the base used for binary representation?
What is the base used for binary representation?
Binary representations require conversion from the _____ system.
Binary representations require conversion from the _____ system.
Match the components with their correct descriptions in a floating-point number:
Match the components with their correct descriptions in a floating-point number:
During conversion, what is the first step in computing the binary representation of a number?
During conversion, what is the first step in computing the binary representation of a number?
The mantissa includes bits after the binary point in a floating-point number.
The mantissa includes bits after the binary point in a floating-point number.
What representation do computers use for internal calculations?
What representation do computers use for internal calculations?
The mantissa in a floating point number is represented as _____ following the exponent.
The mantissa in a floating point number is represented as _____ following the exponent.
What range does the exponent cover in the standard 32-bit floating-point format?
What range does the exponent cover in the standard 32-bit floating-point format?
What is the primary purpose of the IEEE Standard 754?
What is the primary purpose of the IEEE Standard 754?
Single precision floating-point numbers in IEEE 754 have 32 bits in total.
Single precision floating-point numbers in IEEE 754 have 32 bits in total.
What is the minimum exponent value for double precision numbers in the IEEE Standard 754?
What is the minimum exponent value for double precision numbers in the IEEE Standard 754?
The first number in the normalized representation F ∗ (β, p, emin , emax ) is called the ______.
The first number in the normalized representation F ∗ (β, p, emin , emax ) is called the ______.
How many bits are used for the significand in double precision format?
How many bits are used for the significand in double precision format?
In floating-point addition, the exponents of the numbers must always be the same before performing binary addition.
In floating-point addition, the exponents of the numbers must always be the same before performing binary addition.
What is the highest exponent value for single precision floating-point numbers in IEEE 754?
What is the highest exponent value for single precision floating-point numbers in IEEE 754?
Match the following terms with their descriptions related to IEEE Standard 754:
Match the following terms with their descriptions related to IEEE Standard 754:
Normalized representation in floating-point numbers often involves scaling to the leading digit which is ______.
Normalized representation in floating-point numbers often involves scaling to the leading digit which is ______.
When adding binary floating-point numbers, which of the following steps is NOT needed?
When adding binary floating-point numbers, which of the following steps is NOT needed?
What must a function that does not have a return type of void include?
What must a function that does not have a return type of void include?
The return value of a function is always converted to its return type.
The return value of a function is always converted to its return type.
What is the return type of the function 'bin_digits'?
What is the return type of the function 'bin_digits'?
The function call used in programming examples is typically structured as _____(functionName)(parameters).
The function call used in programming examples is typically structured as _____(functionName)(parameters).
Flashcards
String
String
A sequence of characters enclosed in double quotes. Examples include: "Hello, world!" or "12345".
Function
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
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
Procedural Programming
Signup and view all the flashcards
Postcondition
Postcondition
Signup and view all the flashcards
Floating-point Number System
Floating-point Number System
Signup and view all the flashcards
Precision (p)
Precision (p)
Signup and view all the flashcards
Smallest Exponent (emin)
Smallest Exponent (emin)
Signup and view all the flashcards
Largest Exponent (emax)
Largest Exponent (emax)
Signup and view all the flashcards
Normalized Representation
Normalized Representation
Signup and view all the flashcards
Floating-point Number Notation
Floating-point Number Notation
Signup and view all the flashcards
F(β, p, emin, emax)
F(β, p, emin, emax)
Signup and view all the flashcards
Underflow
Underflow
Signup and view all the flashcards
Multiple Representations
Multiple Representations
Signup and view all the flashcards
Limitations of Floating-point Arithmetic
Limitations of Floating-point Arithmetic
Signup and view all the flashcards
Floating Point Number Representation
Floating Point Number Representation
Signup and view all the flashcards
Exponent Bits
Exponent Bits
Signup and view all the flashcards
Mantissa Bits
Mantissa Bits
Signup and view all the flashcards
Base of the number system (β)
Base of the number system (β)
Signup and view all the flashcards
Input/Output Base
Input/Output Base
Signup and view all the flashcards
Conversion
Conversion
Signup and view all the flashcards
Decimal to Binary Conversion
Decimal to Binary Conversion
Signup and view all the flashcards
Exponent Range
Exponent Range
Signup and view all the flashcards
Smallest Non-Zero Value
Smallest Non-Zero Value
Signup and view all the flashcards
Largest Value
Largest Value
Signup and view all the flashcards
Set of Normalized Numbers
Set of Normalized Numbers
Signup and view all the flashcards
Calculations with Floating-point Numbers
Calculations with Floating-point Numbers
Signup and view all the flashcards
The IEEE Standard 754
The IEEE Standard 754
Signup and view all the flashcards
Single Precision (float)
Single Precision (float)
Signup and view all the flashcards
Double Precision (double)
Double Precision (double)
Signup and view all the flashcards
Rounding in Floating-point Operations
Rounding in Floating-point Operations
Signup and view all the flashcards
Single Precision Format Breakdown
Single Precision Format Breakdown
Signup and view all the flashcards
Double Precision Format Breakdown
Double Precision Format Breakdown
Signup and view all the flashcards
Why the Specific Bit Allocations?
Why the Specific Bit Allocations?
Signup and view all the flashcards
void Function
void Function
Signup and view all the flashcards
Arguments of a Function
Arguments of a Function
Signup and view all the flashcards
Return Type
Return Type
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) { ... }
whereT
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 integern
. - If
n
is negative, the code convertsa
to its reciprocal andn
to its positive value to calculatea^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
orb != 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()
instd
andinformatik
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.