Tag Archives: procedural programming

Side Effects in programming

According to Wikipedia, a function or expression is said to have a side effect if it modifies some state outside its local environment. Let’s understand this in depth by examples.

To understand side effects, first, we should know what “state” is. In layman terms, the state refers to the value of the variable at any given point in the program like x=2.

Now, the above Wikipedia definition might have started to make some sense. If not, don’t worry, see the figure below.

In this figure, we have a variable x that is outside the scope of two functions. Clearly, after the Function_2 call, the value of x changes which means that Function_2, other than returning value(if any), is interacting with variables outside its local environment and thus has a side effect.

In general, any lasting effect that occurs in a function, not through its return value, is called a side effect. Not through its return value means if we assign x=Function_1(), this will clearly change the value of x but this is not a side effect. For side effect, the change should occur inside a function.

Common ways to have side effects:

  1. Modifying a non-local variable: Let’s understand by an example

    The x in the square() function is local to its scope so no change in outside x. Global in square_side_effect() makes x global so any change in x will change it in whole code (outside function scope) so the square_side_effect function() is not pure(has side effects).
  2. Changing the value of a mutable object: Let’s take Lists which is a mutable object.

    Clearly, after calling list_side_effect(), the value of lst changes so, list_side has side effects.
  3. Performing Input/Output operations: This doesn’t change any objects or variable value, but it does have a potential lasting effect outside the function call because a person might see the output and be influenced by it.
  4. Calling other side-effect functions

Now, you might have got some feeling about the side effects. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Functional programming paradigm

Functional programming is based on Lambda Calculus, a computational model proposed by Alonzo Church in the 1930’s. This paradigm falls under the declarative umbrella rather than imperative, meaning, programming is done with expressions or declarations instead of statements. (Relax… See below the properties and you will understand what this means)

Note: We can do programming in a functional style in languages that are not specifically designed for functional programming. C++, Java, Python etc. all added constructs to facilitate the functional style. So, programmers using “mostly imperative” languages may have utilized some of the functional concepts.

Pros: more concise, more predictable, and easier to test than imperative or object-oriented code only if you understand.

Main Concepts of Functional Programming:

  1. Pure Functions: A pure function is a function which follows given properties like
    • For the same input, it always returns the same output.
    • Has no side effects(For details check the link).
    • Referential Transparency.

    Although, these properties are somehow correlated. Like, a function can only return the same output for the same input, only if it has no side effects. Let’s see by examples

    The square function always gives the same output for the same x .

    Even though we didn’t pass any arguments to the random_num function, it gives different output, meaning that random_num is not pure.

    Now, let’s look at the side effect property.
    Side effect is any state(value of the variable) change outside the called function scope. In simple terms, if a function changes the state/value of the variable that is outside function scope or local environment then the function/unit of code is said to have a side effect.

    Clearly, the x value changes after square_side() function which means that this function has side effects. The x in the square() function is local to its scope so no change in outside x. Global in square_side() makes x global so any change in x will change it in whole code (outside function scope) so the square_side function is not pure(has side effects).

    Another important property of pure functions is Referential Transparency means we can replace a function call with its resulting value without changing the meaning of the program. Let’s see by an example

    In the above example, we can easily replace double(2) by 4 because we know that this function always gives the same result for the same input(No side effects). This is Referential Transparency.

  2. First Class functions: “first-class” is a computer science term that describes something that has no restriction on its use. Thus, first-class functions mean it can appear anywhere in the program like as arguments to other functions and as their return values.
  3. Recursion: Recursion means letting an operation be repeated until it reaches the base case. In the functional paradigm, we do not use the loops and all the work is done by recursion.

  4. Avoid Shared State:  A shared state is a state that is shared between more than one function or more than one data structure and you should avoid creating functions that rely on the same variable.

Now, you might have got some feeling about the Functional programming. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Procedural Programming paradigm

Procedural programming, as the name suggests, is based on the concept of procedural call. To understand it more deeply, we should first know what procedural call means.

Procedures, also known as routines, subroutines, or functions, simply contain a series of computational steps to be carried out. Let’s see an example of the procedure

Procedure calls are modular and are bound by scope. It means we break the program into independent, interchangeable small pieces known as modules such that each module contains everything necessary to execute. Each module is composed of one or more procedures.

Procedural programs may possibly have multiple levels or scopes, with subprograms defined inside other subprograms. Scoping helps to keep the procedures modular and prevents/allows the procedure, from accessing the variables of other procedures.

Procedural programming falls under the imperative umbrella which means that procedural inherits the properties of the imperative paradigm. Procedural programming uses a linear or top-down approach.

Pros: easier to read and more maintainable, more flexible, allows modules to be used again in the form of code libraries.

Cons: this approach may not use hardware efficiently when tackling complex problems because of side effects (may not run parallelly)

In short, procedural programming means breaking the program into modules and using the imperative approach. If we take away the imperative nature from procedural, then it becomes same as Functional.

Now, you might have got some feeling about the Procedural programming. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.