Podcast
Questions and Answers
What is the purpose of functions in programming?
What is the purpose of functions in programming?
Procedural programming is another term for object-oriented programming.
Procedural programming is another term for object-oriented programming.
False
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 ______.
Signup and view all the answers
Match the following programming terms with their descriptions:
Match the following programming terms with their descriptions:
Signup and view all the answers
What does the symbol 'β' represent in a floating-point number system?
What does the symbol 'β' represent in a floating-point number system?
Signup and view all the answers
The normalized representation of a floating-point number can have multiple forms.
The normalized representation of a floating-point number can have multiple forms.
Signup and view all the answers
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?
Signup and view all the answers
In a floating-point representation, the number 0 has ______ normalized representation.
In a floating-point representation, the number 0 has ______ normalized representation.
Signup and view all the answers
Match the components of a floating-point number representation with their descriptions:
Match the components of a floating-point number representation with their descriptions:
Signup and view all the answers
Which of the following best describes normalized representation in floating-point numbers?
Which of the following best describes normalized representation in floating-point numbers?
Signup and view all the answers
The notation F (β, p, emin, emax) encompasses all the floating-point numbers.
The notation F (β, p, emin, emax) encompasses all the floating-point numbers.
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)?
What is the smallest possible exponent in a floating-point number defined by the notation F (β, p, emin, emax)?
Signup and view all the answers
Floating-point numbers can be represented in base ______.
Floating-point numbers can be represented in base ______.
Signup and view all the answers
How is a floating-point number structured according to its representation?
How is a floating-point number structured according to its representation?
Signup and view all the answers
What does the 32-bit representation of a floating point number include?
What does the 32-bit representation of a floating point number include?
Signup and view all the answers
The binary system is used internally by computers to perform calculations.
The binary system is used internally by computers to perform calculations.
Signup and view all the answers
What is the base used for binary representation?
What is the base used for binary representation?
Signup and view all the answers
Binary representations require conversion from the _____ system.
Binary representations require conversion from the _____ system.
Signup and view all the answers
Match the components with their correct descriptions in a floating-point number:
Match the components with their correct descriptions in a floating-point number:
Signup and view all the answers
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?
Signup and view all the answers
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.
Signup and view all the answers
What representation do computers use for internal calculations?
What representation do computers use for internal calculations?
Signup and view all the answers
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.
Signup and view all the answers
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?
Signup and view all the answers
What is the primary purpose of the IEEE Standard 754?
What is the primary purpose of the IEEE Standard 754?
Signup and view all the answers
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.
Signup and view all the answers
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?
Signup and view all the answers
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 ______.
Signup and view all the answers
How many bits are used for the significand in double precision format?
How many bits are used for the significand in double precision format?
Signup and view all the answers
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.
Signup and view all the answers
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?
Signup and view all the answers
Match the following terms with their descriptions related to IEEE Standard 754:
Match the following terms with their descriptions related to IEEE Standard 754:
Signup and view all the answers
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 ______.
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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.
Signup and view all the answers
What is the return type of the function 'bin_digits'?
What is the return type of the function 'bin_digits'?
Signup and view all the answers
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).
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) { ... }
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.
Related Documents
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.