Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Lecture 17

Inline Functions, random number generation built-in


function, command line arguments.
Inline Functions
• An inline function is a function that is expanded in line when it is called.
• When the inline function is called, the entire body of the function is
inserted at the point of the function call, and hence, function call overhead
is saved.
• The compiler replaces the function call with the corresponding function
code which can lead to faster execution of programs, especially when the
function is small and called frequently.
Key points of inline function
• Definition: Inline functions are defined using the inline keyword before
the function definition. For example:
Key points
• Advantages:
• Elimination of Function Call Overhead: There is no function call overhead (like
saving registers and stack frame creation), which makes the program run faster for
small, frequently called functions.
• Avoidance of Context Switching: Since the function body is expanded at the point
of call, the context switching time is saved.
Key points
• Disadvantages:
• Increased Binary Size: If inline functions are used extensively, especially
for large functions, it can lead to an increase in the binary size (code
bloat), as the function code is duplicated at each point of call.
• Compiler's Discretion: The inline keyword is just a request to the
compiler. The compiler can ignore this request if the function is too
complex or if it involves features like recursion, static variables, etc.
Key points
• Usage Guidelines:
• Use inline functions for small, performance-critical functions that are called frequently.
• Avoid using inline functions for complex or large functions as it can lead to code bloat.
• Examples of Non-Inlining Scenarios:
• Functions containing loops, static variables, recursive calls, and switch statements are
often not inlined.
• Virtual functions and functions not visible at the point of call (like those defined in a
different translation unit) generally cannot be inlined.
Random Numbers Generation
• Generating random numbers is a common task in programming, used for
everything from games to simulations to testing.
• Basic Concept
• At its core, random number generation is like rolling a dice or picking a
card from a shuffled deck. The goal is to get a number that is
unpredictable. In computers, we often use a "random number generator"
(RNG) to accomplish this.
Types of Random Number Generators

• Pseudo-Random Number Generators (PRNGs):


• These are algorithms that use mathematical formulas or pre-defined rules to produce
sequences of numbers that appear random.
• PRNGs are not truly random because they start from an initial value called a "seed".
If you use the same seed again, you'll get the same sequence of numbers.
• They are useful for tasks where you need a predictable sequence of "random"
numbers, like in simulations or games where you might want to replay the same
scenario.
• Luddo star game is the best example
Types of Random Number Generators

• True Random Number Generators (TRNGs):


• These rely on physical processes to generate randomness, such as electrical noise,
atmospheric noise, or other quantum phenomena.
• TRNGs are used in security applications where unpredictability is crucial, like in
cryptography(network security).
Key Points to Remember

• Seeding(srand function): Especially in PRNGs, the seed determines the


sequence of numbers. Seeding with the same value will produce the same
sequence, so often the current time is used as a seed to ensure different
sequences on each run.
• Uniformity: A good random number generator produces numbers that are
evenly distributed across the range.
• Security: For applications where security is important, make sure to use a
generator designed for cryptographic purposes, as standard PRNGs may be
predictable.
Buit-in-function
• In programming, a built-in function refers to a function that is pre-
defined and provided by a programming language or its standard libraries,
meaning the programmer can use it directly without needing to define it.
• These functions are an integral part of the language environment,
designed to perform common or complex tasks more easily and
efficiently.
Common Built-in-functions
• cout - Outputs data to the standard output (console); used with the insertion
operator (<<) for printing text and variables.
• cin - Reads data from the standard input (console); used with the extraction
operator (>>) to input data from the user.
• abs() - Returns the absolute value of an integer or floating point number.
• sqrt() - Computes the square root of a number, returning a floating-point
result.
• pow() - Calculates the power of a number (x raised to the power of y).
Common Built-in-functions
• sort() - Sorts elements in a range using either operator < for default sorting
or a specified comparison function.
• max() - Returns the larger of two values, or the maximum value in a range.
• min() - Returns the smaller of two values, or the minimum value in a range.
• string::size() - Returns the number of characters in a string.
• vector::push_back() - Adds an element to the end of a vector, increasing its
size by one.
command line arguments
• Command line arguments are a way to pass information to a program
when you start it.
• In many programming languages, including C++, you can write programs
that read arguments directly from the command line, allowing users to
specify options or provide inputs without the need for user interaction
while the program is running.
command line arguments
• argc (Argument Count): This is an integer that represents the number of
command line arguments passed to the program, including the name of the
program itself.
• argv (Argument Vector): This is an array of character strings (or an array
of pointers to char) representing the individual arguments provided on the
command line.
• The first element (argv[0]) is always the name of the executable program.
Example
Output

You might also like