1/91
Functions, Subroutines, Calling Sequence
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Perform computations and return a value.
Functions
Execute a series of instructions but do not return a value.
Subroutines
Advantages of Functions and Subroutines
Modularity
Reusability
Easier debugging
Readability
FUNCTION or SUBROUTINE?
Calculate the square of a number.
FUNCTION
FUNCTION or SUBROUTINE?
Displaying messages
SUBROUTINE
Anatomy of a Function
Parameters
body
return statement
Types of functions
Void
Value-returning
Scope and Lifetime of Functiosn
Local
Global
Static
Return Values of Functions
Single
Multiple
Standalone blocks of reusable code.
Subroutines
Simplifying complex logic by splitting tasks into subroutines.
Procedural Abstraction
When to Use Subroutines?
For repetitive tasks (e.g., input/output operations).
To increase maintainability and clarity.
Subroutines and Procedural Abstraction Examples
Subroutine for data validation.
Recursive subroutines for algorithms like factorial or Fibonacci.
Defining functions inside other functions.
Nested Functions/Subroutines
Two cases inside when performing recursive call
Base Case
Recursive Case
Functions that take other functions as parameters or return them
Higher Order Functions
Examples of Higher-Order Functions
Map
Filter
Reduce
Reducing overhead with inline functions or efficient recursion.
Optimization
Best Practices for Functions and Subroutines
Naming conventions
Modular design principles
Avoiding side effects
The set of actions taken by a program to transfer control to a subroutine and back.
Calling sequence
Calling sequence includes managing?
Function arguments
return values
call stack
Steps in a Calling Sequence:
Caller Responsibilities
Save necessary registers and program state.
Push arguments onto the stack or into registers.
Jump to the function's address.
Steps in a Calling Sequence:
Callee Responsibilities
Allocate space for local variables.
Execute the function body.
Store the return value.
Restore the caller's state and return control.
Specifies how arguments are passed, return values are handled, and the stack is managed.
Calling Conventions
Common conventions for Calling Sequence
CDECL
STDCALL
FASTCALL
Caller cleans up the stack.
CDECL
Callee cleans up the stack.
STDCALL
Uses registers for argument passing.
FASTCALL
are variables that act as placeholders for the input data a function or method needs to operate.
Parameters
They allow a function to receive and process information dynamically, making it reusable and versatile.
Parameters
Types of Parameters
Formal Parameters
Actual Parameters
These are defined in the function's declaration or definition.
Formal Parameters
They specify the input that the function expects when it is called.
Formal Parameters
These are the actual values or data passed to the function when it is called.
Actual Parameters/Arguments
Kinds of Parameters
Positional Parameters
Default Parameters
Keyword Parameters
Variable-Length Parameters
Arguments are matched to parameters based on their position in the function call.
Positional Parameters
Parameters with default values that are used if no argument is provided for them.
Default Parameters
Arguments are matched to parameters by name, regardless of their position.
Keyword Parameters
Functions can accept a variable number of arguments using special syntax.
Variable-Length Parameters
Functions can accept a variable number of arguments using special syntax. What are these?
*args for positional arguments
**kwargs for keyword arguments
Functions can accept a variable number of arguments using special syntax for positional arguments?
*args
Functions can accept a variable number of arguments using special syntax for keyword arguments?
**kwargs
Importance of Parameters
Reusability
Modularity
Flexibility
Parameter Passing Mechanisms
Pass by Value
Pass by Reference
Pass by Pointer
Pass by Name
A copy of the argument is passed.
Pass by Value
A reference to the argument is passed.
Pass by Reference
When using this passing mechanism, modifying the parameter does not affect the original value.
Pass by Value
When using this passing mechanism, changes to the parameter reflect in the original variable.
Pass by Reference
A pointer to the memory address of the argument is passed.
Pass by Pointer
Expressions are re-evaluated each time they are accessed.
Pass by Name
Which of these is least commonly used?
Pass by Value
Pass by Reference
Pass by Pointer
Pass by Name
Pass by Name
Mechanism for writing generic, reusable code.
Templates
Useful for functions and classes that operate on different data types.
Templates
Types of Templates
Function Templates
Class Templates
Define functions with generic types.
Function Templates
Define classes with generic types.
Class Templates
What language is this?
Generic programming using type hints (typing).
Python
What language is this?
Generics with for collections (ArrayList).
Java
What language is this?
template T add(T a, T b) { return a + b;}
C++
Advantages of Templates
Type safety and reusability.
Reducing code duplication.
refers to the practice of writing programs that can manipulate, generate, or analyze other programs (or themselves) as their data. It enables developers to create highly dynamic and adaptable systems.
Metaprogramming
Programs can inspect and modify their own structure and behavior during runtime.
Reflection
Code can be treated as data structures that can be manipulated programmatically.
Code as Data
In Lisp, code is written in the same form as data structures, making it easy to manipulate. What concept is portrayed here?
Code as Data
Using reflection APIs in Java to dynamically call methods or change fields. What concept is portrayed here?
Reflection
Programs can write or modify other programs, often at compile time or runtime.
Code Generation
Using templates in C++ or macros in Lisp to generate boilerplate code. What concept is portrayed here?
Code Generation
A feature of many languages (e.g., Lisp, Rust) that allows compile-time code transformations.
Macros
Programs can execute code constructed as strings or data structures during runtime.
Dynamic Evaluation
Types of Metaprogramming
Compile-Time Metaprogramming
Runtime Metaprogramming
Code manipulation occurs during compilation.
Compile-Time Metaprogramming
Compile-Time Metaprogramming Tools
C++ Templates
Rust Macros
MetaOCaml
Benefits of Compile-Time Metaprogramming
Performance improvements by resolving computations at compile time.
Code changes or decisions happen while the program is running.
Runtime Metaprogramming
Runtime Metaprogramming Tools
Python decorators
Java reflection.
Benefits of Runtime Metaprogramming
Greater flexibility and adaptability.
Advantages of Metaprogramming
Code Reusability
Flexibility
Abstraction
Efficiency
Challenges of Metaprogramming
Complexity
Performance Overhead
Maintainability
Applications of Metaprogramming
Domain-Specific Languages (DSLs)
Dynamic Frameworks
Code Generation Tools
Aspect-Oriented Programmign (AOP)
Python:
Reflection:
Dynamic Code Execution:
Metaclasses:
Reflection: getattr() and setattr().
Dynamic Code Execution: eval() and exec().
Metaclasses: Customizing class creation.
JavaScript:
Dynamic Property Access:
Code Generation:
Dynamic Property Access: Object.defineProperty.
Code Generation: Use of eval for runtime code evaluation.
C++
Metaprogramming:
Templates for compile-time metaprogramming.
Example: Standard Template Library (STL) heavily uses metaprogramming techniques.
Macros to manipulate code as data, offering unparalleled flexibility. What language is this?
Lisp
Open classes and metaprogramming-friendly syntax. What language is this ?
Rust
In Rust, we can dynamically defining methods using ?
define_method
It is a set of statements that take inputs, do some specific computation and produces output.
Function
•A C++ function definition consists of a?
function return type
function name
parameters
function body
A C++ function consist of two parts:
Declaration
Definition
the function's name, return type, and parameters (if any)
Declaration
the body of the function (code to be executed)
Definition
It act as variables inside the function
Parameters
When a parameter is passed to the function, it is called an?
argument.