Pure and Impure Functions
Pure and impure functions are central concepts in writing reliable and maintainable C# code. To understand them, think of a pure function like a vending machine: you insert a specific amount of money and always get the same snack in return, with no surprises. An impure function, on the other hand, is like a vending machine that sometimes gives you an extra snack, plays a sound, or changes the price depending on the time of day—its output or behavior can change based on things outside your direct input.
Program.cs
123456789101112131415161718192021222324252627282930using System; namespace ConsoleApp { public class Program { // Pure function: always returns the square of the input, no side effects public static int Square(int x) { return x * x; } // Impure function: prints to the console (side effect) public static void PrintSquare(int x) { int result = x * x; Console.WriteLine("The square is: " + result); } public static void Main(string[] args) { int number = 5; int squared = Square(number); Console.WriteLine("Pure function result: " + squared); PrintSquare(number); // Impure function: prints to console } } }
A function is considered pure if it always produces the same output for the same inputs and does not alter anything outside of itself. This means it has no side effects, such as modifying variables outside its scope, printing to the console, or reading from a file. Pure functions are deterministic: given the same input, they always return the same result.
Impure functions, on the other hand, can interact with the outside world or depend on external state, which makes their behavior less predictable. Pure functions are easier to test, debug, and reuse because you can trust that they behave consistently and do not interfere with other parts of your program.
Program.cs
1234567891011121314151617181920212223242526272829303132333435using System; namespace ConsoleApp { public class Program { // Global variable (field) public static int counter = 0; // Impure method: modifies global state public static void IncrementCounter() { counter = counter + 1; } // Pure method: calculates sum based only on parameters public static int Add(int a, int b) { return a + b; } // Entry point without parameters (required by this runner) public static void Main() { // Using impure method Console.WriteLine("Counter before: " + counter); IncrementCounter(); Console.WriteLine("Counter after: " + counter); // Using pure method int sum = Add(3, 4); Console.WriteLine("Pure method result: " + sum); } } }
This example shows two different behaviors. IncrementCounter() is impure because it changes the global variable counter, so calling it affects program state outside the method. Add(a, b) is pure because it depends only on its inputs and always returns the same result for the same arguments, without modifying anything else.
1. What makes a function 'pure' in programming?
2. Which of the following is a side effect?
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you give more examples of pure and impure functions in C#?
Why are pure functions preferred in some programming scenarios?
How can I refactor an impure function to make it pure?
Fantastisk!
Completion rate forbedret til 8.33
Pure and Impure Functions
Stryg for at vise menuen
Pure and impure functions are central concepts in writing reliable and maintainable C# code. To understand them, think of a pure function like a vending machine: you insert a specific amount of money and always get the same snack in return, with no surprises. An impure function, on the other hand, is like a vending machine that sometimes gives you an extra snack, plays a sound, or changes the price depending on the time of day—its output or behavior can change based on things outside your direct input.
Program.cs
123456789101112131415161718192021222324252627282930using System; namespace ConsoleApp { public class Program { // Pure function: always returns the square of the input, no side effects public static int Square(int x) { return x * x; } // Impure function: prints to the console (side effect) public static void PrintSquare(int x) { int result = x * x; Console.WriteLine("The square is: " + result); } public static void Main(string[] args) { int number = 5; int squared = Square(number); Console.WriteLine("Pure function result: " + squared); PrintSquare(number); // Impure function: prints to console } } }
A function is considered pure if it always produces the same output for the same inputs and does not alter anything outside of itself. This means it has no side effects, such as modifying variables outside its scope, printing to the console, or reading from a file. Pure functions are deterministic: given the same input, they always return the same result.
Impure functions, on the other hand, can interact with the outside world or depend on external state, which makes their behavior less predictable. Pure functions are easier to test, debug, and reuse because you can trust that they behave consistently and do not interfere with other parts of your program.
Program.cs
1234567891011121314151617181920212223242526272829303132333435using System; namespace ConsoleApp { public class Program { // Global variable (field) public static int counter = 0; // Impure method: modifies global state public static void IncrementCounter() { counter = counter + 1; } // Pure method: calculates sum based only on parameters public static int Add(int a, int b) { return a + b; } // Entry point without parameters (required by this runner) public static void Main() { // Using impure method Console.WriteLine("Counter before: " + counter); IncrementCounter(); Console.WriteLine("Counter after: " + counter); // Using pure method int sum = Add(3, 4); Console.WriteLine("Pure method result: " + sum); } } }
This example shows two different behaviors. IncrementCounter() is impure because it changes the global variable counter, so calling it affects program state outside the method. Add(a, b) is pure because it depends only on its inputs and always returns the same result for the same arguments, without modifying anything else.
1. What makes a function 'pure' in programming?
2. Which of the following is a side effect?
Tak for dine kommentarer!