Procedural vs Object Oriented vs Functional Programming


  • 1

  • 0
    P

    The following code represents aspects of each programming style.

    It is object-oriented because it encapsulates code into various objects. For instance, the Heart object encapsulates arteries, etc.

    It is functional because its methods have no state and merely pass objects among themselves like a heart pumps blood among its departments.

    Lastly, it is procedural because it takes the gives the computer instructions for what to do, e.g. for loop -> make new.

    class Heart {
        constructor(props) {
    	this.Aortic_Valve = false;
    	this.Mitral_Valve = false;
    	this.Pulmonary_Valve = false;
    	this.Tricuspid_Valve = false;
    	this.Superior_Venae_Cavea(props);
    	this.Inferior_Venea_Cavea(props);
        }
    
        Aorta(blood) {
    	distribute_to_REST(blood);
        }
    
        Inferior_Venea_Cavea(blood) {
    	this.Right_Atrium(blood);
        }
    
        Left_Atrium(lungs) {
    	var blood = lungs.getBlood();
    	this.Mitral_Valve = true;
    	this.Left_Ventricle(blood);
        }
    
        Left_Ventricle(blood) {
    	this.Mitral_Valve = false;
    	this.Aortic_Valve = true;
    	this.Aorta(blood);
        }
    
        Pulmonary_Artery(blood) {
    	this.Pulmonary_Valve = false;
    	this.Left_Atrium(new Abstract_Lungs(blood));
        }
    
        Superior_Venae_Cavea(blood) {
    	this.Right_Atrium(blood);
        }
    
        Right_Atrium(blood) {
    	this.Tricuspid_Valve = true;
    	this.Right_Ventricle(blood);
        }
    
        Right_Ventricle(blood) {
    	this.Tricuspid_Valve = false;
    	this.Pulmonary_Valve = true;
    	this.Pulmonary_Artery(blood);
        }
    }
    
    function distribute_to_REST(blood) {
        console.log(blood);
    }
    
    class Abstract_Lungs {
        constructor(props) {
    	    this.blood = props.map(cell => cell + '+');
        }
        getBlood() { return this.blood; }
    }
    
    let blood = [['rbc', 'wbc', 'p'], ['rbc', 'wbc', 'p'], ['rbc', 'wbc', 'p']];
    
    for (var i = 0; i < blood.length; i++) {
        new Heart(blood[i])
    }

  • 0
    S

    Procedural Programming is a kind of programming paradigm where everything will be in the form of instruction. This is could be anything like loop statements, if-else clause, functions and even sub-routines. These instructions will tell what the computer should do. Procedural Programming revolves around variables and instructions.
    Example: C, C++, PHP, Python

    Object Oriented Programming is nothing but a structured procedural programming paradigm. Everything problem is modeled into an object. Object binds together data and behavior. Through behavior it is possible to perform actions on data. Because of Object Oriented Programming, it is possible to structure everything logically. Object Oriented Programming revolves around objects.
    Example: C++, Java, PHP, C#, Ruby, Python

    Functional Programming is a programming paradigm in which the problem is treated as a sequence of functions. Data flows freely among these functions. In this paradigm, it is possible to pass a function as an argument to another function and a function can return another function. Functional Programming revolves around functions.
    Example: JavaScript, C++, Haskell, Ruby


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.