Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Pure and Impure Functions | Pure vs Impure Functions & Stepwise Decomposition
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Methods & Modular Thinking

bookPure 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

Program.cs

copy
123456789101112131415161718192021222324252627282930
using 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

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using 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); } } }
Note
Definition

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?

question mark

What makes a function 'pure' in programming?

Select the correct answer

question mark

Which of the following is a side effect?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookPure and Impure Functions

Pyyhkäise näyttääksesi valikon

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

Program.cs

copy
123456789101112131415161718192021222324252627282930
using 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

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using 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); } } }
Note
Definition

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?

question mark

What makes a function 'pure' in programming?

Select the correct answer

question mark

Which of the following is a side effect?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1
some-alt