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; } public static void Main(string[] args) { // 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); } } }
Definition:
A side effect is any observable change outside a function's own scope, such as modifying a global variable, printing to the console, writing to a file, or changing input parameters.
Determinism means that a function always produces the same output given the same input, with no variation based on outside factors.
1. What makes a function 'pure' in programming?
2. Which of the following is a side effect?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 8.33
Pure and Impure Functions
Veeg om het menu te tonen
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; } public static void Main(string[] args) { // 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); } } }
Definition:
A side effect is any observable change outside a function's own scope, such as modifying a global variable, printing to the console, writing to a file, or changing input parameters.
Determinism means that a function always produces the same output given the same input, with no variation based on outside factors.
1. What makes a function 'pure' in programming?
2. Which of the following is a side effect?
Bedankt voor je feedback!