22. GPIO Pin Toggle

Create an inline function that toggles the state of a GPIO pin.

Each pin state is represented as:

  • 0 → LOW
  • 1 → HIGH

Your inline function must:

  • Take the current pin state as input
  • Return the toggled state:
    • 0 → 1
    • 1 → 0

In main():

  • Read the initial states of exactly 10 GPIO pins (space-separated, index 0–9).
  • Read an integer n, the number of toggle operations.
  • Read n pin indices.
  • For each pin index:
    • Toggle the corresponding pin every time it appears.
  • Print the final states of all 10 pins, separated by spaces.

Example 

Input:

0 1 0 1 0 1 0 1 0 1
3
0 1 0

Output:

0 0 0 1 0 1 0 1 0 1

 

Explanation of Example:

  • Toggle pin 00 → 1
  • Toggle pin 11 → 0
  • Toggle pin 0 again → 1 → 0

Final state:

0 0 0 1 0 1 0 1 0 1

 

Input Constraints:

  • Pin states are guaranteed to be either 0 or 1
  • Pin indices are guaranteed to be in the range 0 to 9
  • 0 ≤ n ≤ 10^5
  • No global variables may be used
  • Toggling must be performed using an inline function

 

 

 

 

Need Help? Refer to the Quick Guide below

An Inline Function is a function where the compiler attempts to expand the code directly at the call site rather than performing a standard function call (which involves jumping to memory, pushing arguments to the stack, and returning).

Think of it as a "Copy-Paste" operation performed intelligently by the compiler. It aims to eliminate the CPU overhead of the function call mechanism.

Syntax & Usage

1. Basic Declaration

Add the inline keyword before the function definition.

Note: The definition (body) usually needs to be in the header file so the compiler can see it during expansion.

// Defined in .h file
inline int max_val(int a, int b) {
    return (a > b) ? a : b;
}

// Usage in .cpp
void process() {
    int x = max_val(10, 20); 
    // Compiler effectively replaces this with:
    // int x = (10 > 20) ? 10 : 20;
}

2. Implicit Inline

Member functions defined inside the class declaration are implicitly inline by default.

class LED {
    // Implicitly inline because it's defined inside the class
    void on() { 
        *register |= 0x01; 
    }
};

How It Works: Call vs. Expansion

Standard Function CallInline Expansion
Step 1: Push registers/args to stack.Step 1: Paste function code directly.
Step 2: Jump to function address.Step 2: Execute logic.
Step 3: Execute logic.Step 3: Continue execution.
Step 4: Pop stack & Return.Overhead: None.
Overhead: High (CPU Cycles).Cost: Increased Flash usage (Code Bloat).

Relevance in Embedded/Firmware

1. Replacing Unsafe C Macros

In C, we use #define MAX(a, b) ((a) > (b) ? (a) : (b)) to avoid function overhead. This is dangerous (type-unsafe, double-evaluation bugs).

inline functions provide the same speed as macros but with Type Safety and debugging support.

// Macro (Dangerous): SQUARE(x++) -> increments x twice!
// Inline (Safe): square(x++) -> works correctly.

2. Zero-Cost Abstractions

You can write clean "Getter/Setter" functions for hardware registers without slowing down the code.

inline void set_bit(uint32_t *reg, uint8_t bit) {
    *reg |= (1 << bit);
}
// This compiles to a single assembly instruction (LDR/ORR/STR), just like raw C.

3. High-Frequency Loops

For code running inside tight loops (e.g., DSP filters, pixel processing) or fast Interrupt Service Routines (ISRs), saving the 10-20 cycles of a function call can be significant.

Common Pitfalls (Practical Tips)

PitfallDetails
❌ Code BloatIf an inline function is large and called from 50 places, the code is copied 50 times. This can explode your firmware size.
❌ Compiler DiscretionThe inline keyword is just a request. The compiler may ignore it if the function is too complex (contains loops, recursion, or switch cases) and treat it as a normal function.
❌ Debugging IssuesDebugging inline functions can be jumpy because the "function" doesn't strictly exist as a single block of memory; it's scattered everywhere.
✅ Usage RuleOnly inline small functions (1-5 lines of code). Let the compiler decide for larger ones.
❌ Linker ErrorsIf you define an inline function in a .cpp file and try to use it in another file, you'll get an "Undefined Reference" error. Inline definitions must go in the Header (.h).