Functions, Subroutines, Calling Sequence

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/91

flashcard set

Earn XP

Description and Tags

Functions, Subroutines, Calling Sequence

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

92 Terms

1
New cards

Perform computations and return a value.

Functions

2
New cards

Execute a series of instructions but do not return a value.

Subroutines

3
New cards

Advantages of Functions and Subroutines

  • Modularity

  • Reusability

  • Easier debugging

  • Readability

4
New cards

FUNCTION or SUBROUTINE?

Calculate the square of a number.

FUNCTION

5
New cards

FUNCTION or SUBROUTINE?

Displaying messages

SUBROUTINE

6
New cards

Anatomy of a Function

  • Parameters

  • body

  • return statement

7
New cards

Types of functions

  • Void

  • Value-returning

8
New cards

Scope and Lifetime of Functiosn

  • Local

  • Global

  • Static

9
New cards

Return Values of Functions

  • Single

  • Multiple

10
New cards

Standalone blocks of reusable code.

Subroutines

11
New cards

Simplifying complex logic by splitting tasks into subroutines.

Procedural Abstraction

12
New cards

When to Use Subroutines?

  • For repetitive tasks (e.g., input/output operations).

  • To increase maintainability and clarity.

13
New cards

Subroutines and Procedural Abstraction Examples

  • Subroutine for data validation.

  • Recursive subroutines for algorithms like factorial or Fibonacci.

14
New cards

Defining functions inside other functions.

Nested Functions/Subroutines

15
New cards

Two cases inside when performing recursive call

  • Base Case

  • Recursive Case

16
New cards

Functions that take other functions as parameters or return them

Higher Order Functions

17
New cards

Examples of Higher-Order Functions

  • Map

  • Filter

  • Reduce

18
New cards

Reducing overhead with inline functions or efficient recursion.

Optimization

19
New cards

Best Practices for Functions and Subroutines

  • Naming conventions

  • Modular design principles

  • Avoiding side effects

20
New cards

The set of actions taken by a program to transfer control to a subroutine and back.

Calling sequence

21
New cards

Calling sequence includes managing?

  • Function arguments

  • return values

  • call stack

22
New cards

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.

23
New cards

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.

24
New cards

Specifies how arguments are passed, return values are handled, and the stack is managed.

Calling Conventions

25
New cards

Common conventions for Calling Sequence

  • CDECL

  • STDCALL

  • FASTCALL

26
New cards

Caller cleans up the stack.

CDECL

27
New cards

Callee cleans up the stack.

STDCALL

28
New cards

Uses registers for argument passing.

FASTCALL

29
New cards

are variables that act as placeholders for the input data a function or method needs to operate.

Parameters

30
New cards

They allow a function to receive and process information dynamically, making it reusable and versatile.

Parameters

31
New cards

Types of Parameters

  • Formal Parameters

  • Actual Parameters

32
New cards

These are defined in the function's declaration or definition.

Formal Parameters

33
New cards

They specify the input that the function expects when it is called.

Formal Parameters

34
New cards

These are the actual values or data passed to the function when it is called.

Actual Parameters/Arguments

35
New cards

Kinds of Parameters

  • Positional Parameters

  • Default Parameters

  • Keyword Parameters

  • Variable-Length Parameters

36
New cards

Arguments are matched to parameters based on their position in the function call.

Positional Parameters

37
New cards

Parameters with default values that are used if no argument is provided for them.

Default Parameters

38
New cards

Arguments are matched to parameters by name, regardless of their position.

Keyword Parameters

39
New cards

Functions can accept a variable number of arguments using special syntax.

Variable-Length Parameters

40
New cards

Functions can accept a variable number of arguments using special syntax. What are these?

  • *args for positional arguments

  • **kwargs for keyword arguments

41
New cards

Functions can accept a variable number of arguments using special syntax for positional arguments?

*args

42
New cards

Functions can accept a variable number of arguments using special syntax for keyword arguments?

**kwargs

43
New cards

Importance of Parameters

  • Reusability

  • Modularity

  • Flexibility

44
New cards

Parameter Passing Mechanisms

  • Pass by Value

  • Pass by Reference

  • Pass by Pointer

  • Pass by Name

45
New cards

A copy of the argument is passed.

Pass by Value

46
New cards

A reference to the argument is passed.

Pass by Reference

47
New cards

When using this passing mechanism, modifying the parameter does not affect the original value.

Pass by Value

48
New cards

When using this passing mechanism, changes to the parameter reflect in the original variable.

Pass by Reference

49
New cards

A pointer to the memory address of the argument is passed.

Pass by Pointer

50
New cards

Expressions are re-evaluated each time they are accessed.

Pass by Name

51
New cards

Which of these is least commonly used?

  • Pass by Value

  • Pass by Reference

  • Pass by Pointer

  • Pass by Name

Pass by Name

52
New cards

Mechanism for writing generic, reusable code.

Templates

53
New cards

Useful for functions and classes that operate on different data types.

Templates

54
New cards

Types of Templates

  • Function Templates

  • Class Templates

55
New cards

Define functions with generic types.

Function Templates

56
New cards

Define classes with generic types.

Class Templates

57
New cards

What language is this?

Generic programming using type hints (typing).

Python

58
New cards

What language is this?

Generics with for collections (ArrayList).

Java

59
New cards

What language is this?

template T add(T a, T b) { return a + b;}

C++

60
New cards

Advantages of Templates

  • Type safety and reusability.

  • Reducing code duplication.

61
New cards

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

62
New cards

Programs can inspect and modify their own structure and behavior during runtime.

Reflection

63
New cards

Code can be treated as data structures that can be manipulated programmatically.

Code as Data

64
New cards

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

65
New cards

Using reflection APIs in Java to dynamically call methods or change fields. What concept is portrayed here?

Reflection

66
New cards

Programs can write or modify other programs, often at compile time or runtime.

Code Generation

67
New cards

Using templates in C++ or macros in Lisp to generate boilerplate code. What concept is portrayed here?

Code Generation

68
New cards

A feature of many languages (e.g., Lisp, Rust) that allows compile-time code transformations.

Macros

69
New cards

Programs can execute code constructed as strings or data structures during runtime.

Dynamic Evaluation

70
New cards

Types of Metaprogramming

  • Compile-Time Metaprogramming

  • Runtime Metaprogramming

71
New cards

Code manipulation occurs during compilation.

Compile-Time Metaprogramming

72
New cards

Compile-Time Metaprogramming Tools

  • C++ Templates

  • Rust Macros

  • MetaOCaml

73
New cards

Benefits of Compile-Time Metaprogramming

Performance improvements by resolving computations at compile time.

74
New cards

Code changes or decisions happen while the program is running.

Runtime Metaprogramming

75
New cards

Runtime Metaprogramming Tools

  • Python decorators

  • Java reflection.

76
New cards

Benefits of Runtime Metaprogramming

Greater flexibility and adaptability.

77
New cards

Advantages of Metaprogramming

  • Code Reusability

  • Flexibility

  • Abstraction

    • Efficiency

78
New cards

Challenges of Metaprogramming

  • Complexity

  • Performance Overhead

  • Maintainability

79
New cards

Applications of Metaprogramming

  • Domain-Specific Languages (DSLs)

  • Dynamic Frameworks

  • Code Generation Tools

  • Aspect-Oriented Programmign (AOP)

80
New cards

Python:

Reflection:

Dynamic Code Execution:

Metaclasses:

  • Reflection: getattr() and setattr().

  • Dynamic Code Execution: eval() and exec().

  • Metaclasses: Customizing class creation.

81
New cards

JavaScript:

Dynamic Property Access:

Code Generation:

  • Dynamic Property Access: Object.defineProperty.

  • Code Generation: Use of eval for runtime code evaluation.

82
New cards

C++

Metaprogramming:

Templates for compile-time metaprogramming.

Example: Standard Template Library (STL) heavily uses metaprogramming techniques.

83
New cards

Macros to manipulate code as data, offering unparalleled flexibility. What language is this?

Lisp

84
New cards

Open classes and metaprogramming-friendly syntax. What language is this ?

Rust

85
New cards

In Rust, we can dynamically defining methods using ?

define_method

86
New cards

It is a set of statements that take inputs, do some specific computation and produces output.

Function

87
New cards

•A C++ function definition consists of a?

  • function return type

  • function name

  • parameters

  • function body

88
New cards

A C++ function consist of two parts:

  • Declaration

  • Definition

89
New cards

the function's name, return type, and parameters (if any)

Declaration

90
New cards

the body of the function (code to be executed)

Definition

91
New cards

It act as variables inside the function

Parameters

92
New cards

When a parameter is passed to the function, it is called an?

 argument.