Search results
Top results related to what is the difference between procedural and functional programming language
Top Answer
Answered Aug 06, 2019 · 182 votes
A functional language (ideally) allows you to write a mathematical function, i.e. a function that takes n arguments and returns a value. If the program is executed, this function is logically evaluated as needed.1
A procedural language, on the other hand, performs a series of sequential steps. (There's a way of transforming sequential logic into functional logic called continuation passing style.)
As a consequence, a purely functional program always yields the same value for an input, and the order of evaluation is not well-defined; which means that uncertain values like user input or random values are hard to model in purely functional languages.
1 As everything else in this answer, that’s a generalisation. This property, evaluating a computation when its result is needed rather than sequentially where it’s called, is known as “laziness”. Not all functional languages are actually universally lazy, nor is laziness restricted to functional programming. Rather, the description given here provides a “mental framework” to think about different programming styles that are not distinct and opposite categories but rather fluid ideas.
Other Answers
Answered Nov 28, 2012 · 109 votes
I've never seen this definition given elsewhere, but I think this sums up the differences given here fairly well:
Functional programming focuses on expressions
Procedural programming focuses on statements
Expressions have values. A functional program is an expression who's value is a sequence of instructions for the computer to carry out.
Statements don't have values and instead modify the state of some conceptual machine.
In a purely functional language there would be no statements, in the sense that there's no way to manipulate state (they might still have a syntactic construct named "statement", but unless it manipulates state I wouldn't call it a statement in this sense). In a purely procedural language there would be no expressions, everything would be an instruction which manipulates the state of the machine.
Haskell would be an example of a purely functional language because there is no way to manipulate state. Machine code would be an example of a purely procedural language because everything in a program is a statement which manipulates the state of the registers and memory of the machine.
The confusing part is that the vast majority of programming languages contain both expressions and statements, allowing you to mix paradigms. Languages can be classified as more functional or more procedural based on how much they encourage the use of statements vs expressions.
For example, C would be more functional than COBOL because a function call is an expression, whereas calling a sub program in COBOL is a statement (that manipulates the state of shared variables and doesn't return a value). Python would be more functional than C because it allows you to express conditional logic as an expression using short circuit evaluation (test && path1 || path2 as opposed to if statements). Scheme would be more functional than Python because everything in scheme is an expression.
You can still write in a functional style in a language which encourages the procedural paradigm and vice versa. It's just harder and/or more awkward to write in a paradigm which isn't encouraged by the language.
Other Answers
Answered May 07, 2023 · 106 votes
Basically the two styles, are like Yin and Yang. One is organized, while the other chaotic. There are situations when Functional programming is the obvious choice, and other situations were Procedural programming is the better choice. This is why there are at least two languages that have recently come out with a new version, that embraces both programming styles. ( Perl 6 and D 2 )
Procedural:
- The output of a routine does not always have a direct correlation with the input.
- Everything is done in a specific order.
- Execution of a routine may have side effects.
- Tends to emphasize implementing solutions in a linear fashion.
Perl 6
sub factorial ( UInt:D $n is copy ) returns UInt {- # modify "outside" state state $call-count++; # in this case it is rather pointless as # it can't even be accessed from outside- my $result = 1;- loop ( ; $n > 0 ; $n-- ){- $result *= $n;- }- return $result;}-
D 2
int factorial( int n ){- int result = 1;- for( ; n > 0 ; n-- ){ result *= n; }- return result;}-
Functional:
- Often recursive.
- Always returns the same output for a given input.
- Order of evaluation is usually undefined.
- Must be stateless. i.e. No operation can have side effects.
- Good fit for parallel execution
- Tends to emphasize a divide and conquer approach.
- May have the feature of Lazy Evaluation.
Haskell
( copied from Wikipedia );
fac :: Integer -> Integer-fac 0 = 1fac n | n > 0 = n * fac (n-1)-
or in one line:
fac n = if n > 0 then n * fac (n-1) else 1-
Perl 6
proto sub factorial ( UInt:D $n ) returns UInt {*}-multi sub factorial ( 0 ) { 1 }multi sub factorial ( $n ) { $n * samewith $n-1 } # { $n * factorial $n-1 }-
D 2
pure int factorial( invariant int n ){ if( n <= 1 ){ return 1; }else{ return n * factorial( n-1 ); }}-
Side note:
Factorial is actually a common example to show how easy it is to create new operators in Perl 6 the same way you would create a subroutine. This feature is so ingrained into Perl 6 that most operators in the Rakudo implementation are defined this way. It also allows you to add your own multi candidates to existing operators.
sub postfix:< ! > ( UInt:D $n --> UInt ) is tighter(&infix:<*>) { [*] 2 .. $n }-say 5!; # 120-
This example also shows range creation (2..$n) and the list reduction meta-operator ([ OPERATOR ] LIST) combined with the numeric infix multiplication operator. (*) It also shows that you can put --> UInt in the signature instead of returns UInt after it.
( You can get away with starting the range with 2 as the multiply "operator" will return 1 when called without any arguments )
1/5
Top Answer
Answered Apr 12, 2018 · 7 votes
A non-functional declarative language is PROLOG. Programming in PROLOG is stating a number of facts, and then ask questions, which the system tries to verify or deny.
Example:
human(socrates). // "Socrates is a human."mortal(X) :- human(X). // "If X is a human, then X is mortal" or // "All humans are mortal."? mortal(socrates) // Is Socrates mortal?Yes.? mortal(X) // Who is mortal?socrates ? mortal(pythagoras). No. // since system doesn't know about any human except Socrates
Another well known language that is declarative, but not functional, is SQL.
Note that there are not only no functions as first class values. In the PROLOG example, there are no functions at all! To be sure, both SQL and PROLOG have some built-in functions, but have no way to let you write your own functions. One could think that the rule
mortal(X) :- human(X).-
is a function, but it isn't, it is an inference rule. Hence, declarative, non-functional languages.
For the second part of your question: it is certainly possible to write imperative code in functional programming languages. Simon Peyton Jones once stated that he thinks that Haskell is the finest imperative programming language in the world. (And this was only a half joke.)
Example:
main = do print "Enter a number" line <- getLine print (succ (read line :: Int))
2/5
Top Answer
Answered Sep 10, 2018 · 127 votes
Imperative
There are several sub-paradigms of the imperative programming paradigm, such as the procedural or the object-oriented programming paradigms.
In the imperative programming paradigm, you describe the algorithm step-by-step, at various degrees of abstraction.
Examples of programming languages which support the procedural paradigm:
- C (and most other legacy languages)
- PHP, mostly
- In some sense, all major languages
Object-Oriented
It typically refers to languages that exhibit a hierarchy of types that inherit both methods and state from base types to derived types, but also includes the unusual prototype-based JavaScript.
Examples of programming languages which support the OO paradigm:
- Java
Declarative
There are several sub-paradigms of the declarative programming paradigm, such as the functional or the logic programming paradigms.
In the declarative programming paradigm, you describe a result or a goal, and you get it via a "black box". The opposite of imperative.
Examples of programming languages which support the declarative programming paradigm:
- yacc
- Treetop
- SQL
- Regular Expressions
- lex
- XSLT
- markup, troff, CSS, VHDL
Functional
Functional programming emphasizes the application of functions without side effects and without mutable state. The declarative systems above exhibit certain aspects of functional programming.
Examples of programming languages which support the declarative functional paradigm:
- Haskell
- OCaml
- Scheme
- Erlang
- F#
- Scala
Other Answers
Answered Nov 28, 2012 · 109 votes
I've never seen this definition given elsewhere, but I think this sums up the differences given here fairly well:
Functional programming focuses on expressions
Procedural programming focuses on statements
Expressions have values. A functional program is an expression who's value is a sequence of instructions for the computer to carry out.
Statements don't have values and instead modify the state of some conceptual machine.
In a purely functional language there would be no statements, in the sense that there's no way to manipulate state (they might still have a syntactic construct named "statement", but unless it manipulates state I wouldn't call it a statement in this sense). In a purely procedural language there would be no expressions, everything would be an instruction which manipulates the state of the machine.
Haskell would be an example of a purely functional language because there is no way to manipulate state. Machine code would be an example of a purely procedural language because everything in a program is a statement which manipulates the state of the registers and memory of the machine.
The confusing part is that the vast majority of programming languages contain both expressions and statements, allowing you to mix paradigms. Languages can be classified as more functional or more procedural based on how much they encourage the use of statements vs expressions.
For example, C would be more functional than COBOL because a function call is an expression, whereas calling a sub program in COBOL is a statement (that manipulates the state of shared variables and doesn't return a value). Python would be more functional than C because it allows you to express conditional logic as an expression using short circuit evaluation (test && path1 || path2 as opposed to if statements). Scheme would be more functional than Python because everything in scheme is an expression.
You can still write in a functional style in a language which encourages the procedural paradigm and vice versa. It's just harder and/or more awkward to write in a paradigm which isn't encouraged by the language.
Other Answers
Answered May 07, 2023 · 106 votes
Basically the two styles, are like Yin and Yang. One is organized, while the other chaotic. There are situations when Functional programming is the obvious choice, and other situations were Procedural programming is the better choice. This is why there are at least two languages that have recently come out with a new version, that embraces both programming styles. ( Perl 6 and D 2 )
Procedural:
- The output of a routine does not always have a direct correlation with the input.
- Everything is done in a specific order.
- Execution of a routine may have side effects.
- Tends to emphasize implementing solutions in a linear fashion.
Perl 6
sub factorial ( UInt:D $n is copy ) returns UInt { # modify "outside" state state $call-count++; # in this case it is rather pointless as # it can't even be accessed from outside my $result = 1; loop ( ; $n > 0 ; $n-- ){ $result *= $n; } return $result;}
D 2
int factorial( int n ){ int result = 1; for( ; n > 0 ; n-- ){ result *= n; } return result;}
Functional:
- Often recursive.
- Always returns the same output for a given input.
- Order of evaluation is usually undefined.
- Must be stateless. i.e. No operation can have side effects.
- Good fit for parallel execution
- Tends to emphasize a divide and conquer approach.
- May have the feature of Lazy Evaluation.
Haskell
( copied from Wikipedia );
fac :: Integer -> Integerfac 0 = 1fac n | n > 0 = n * fac (n-1)
or in one line:
fac n = if n > 0 then n * fac (n-1) else 1
Perl 6
proto sub factorial ( UInt:D $n ) returns UInt {*}multi sub factorial ( 0 ) { 1 }multi sub factorial ( $n ) { $n * samewith $n-1 } # { $n * factorial $n-1 }
D 2
pure int factorial( invariant int n ){ if( n <= 1 ){ return 1; }else{ return n * factorial( n-1 ); }}
Side note:
Factorial is actually a common example to show how easy it is to create new operators in Perl 6 the same way you would create a subroutine. This feature is so ingrained into Perl 6 that most operators in the Rakudo implementation are defined this way. It also allows you to add your own multi candidates to existing operators.
sub postfix:< ! > ( UInt:D $n --> UInt ) is tighter(&infix:<*>) { [*] 2 .. $n }say 5!; # 120
This example also shows range creation (2..$n) and the list reduction meta-operator ([ OPERATOR ] LIST) combined with the numeric infix multiplication operator. (*) It also shows that you can put --> UInt in the signature instead of returns UInt after it.
( You can get away with starting the range with 2 as the multiply "operator" will return 1 when called without any arguments )
3/5
Top Answer
Answered Aug 31, 2013 · 4 votes
The C language is governed by the ISO approved C standard and it does not take in to account the underlying platform on which you use C. So from the perspective of the language standard there is no difference, and a standard compliant program shall work correctly on both.
However in practical usage one needs to do platform specific things for ex: IPC mechanisms, multithreading, file access and so on which are specific to the platform, such functionality will vary from platform to platform because each will provide functionality specific to itself. Note that such functionality is not covered by the C language standard, so using it makes the program non portable across other platforms.
4/5
Top Answer
Answered Dec 09, 2010 · 46 votes
Structured programming is an old term that I think would encompass functional, procedural, and much else. It basically means using explicit control-flow structures rather than jumping about directly from instruction to instruction.
Functional and procedural programming are both, in that sense, structured paradigms. Functional programming is also declarative programming -- the structure given to your code corresponds to its meaning -- a program is a function that changes the state of the world. Procedural programming is what you'd consider "typical" programming in any C language or its descendants, including OO languages such as Java and C++. A program is a series of instructions, to be executed serially, and invoking subprocedures along the way.
Or, as it has been famously described:
Functional programming is like describing your problem to a mathematician. Imperative programming is like giving instructions to an idiot.
--- arcus, #scheme on Freenode-
5/5
People also ask
What is procedural programming language?
- 1. Procedural programming languages A procedural language follows a sequence of statements or commands to achieve a desired output. Each series of steps is called a procedure, and a program written in one of these languages will have one or more procedures within it.
5 Types of Programming Language | Coursera
www.coursera.org/gb/articles/types-programming-languageWhat is the difference between procedural and object oriented programming?
- Often, we compare procedural and object-oriented programming paradigms. This is because both can deal with data, and modules, such as procedural programming functions and object-oriented methods. Procedures and modules operating on data are treated as separate entities in procedural programming.
Procedural Programming - Definition, Advantages, and Disadvantages
www.techgeekbuzz.com/blog/procedural-programming/What is a functional programming language?
- Functional programming languages Rather than focusing on the execution of statements, functional languages focus on the output of mathematical functions and evaluations. Each function–a reusable module of code–performs a specific task and returns a result. The result will vary depending on what data you input into the function.
5 Types of Programming Language | Coursera
www.coursera.org/gb/articles/types-programming-languageWhat are examples of procedural languages?
- Common examples of procedural languages include: 2. Functional programming languages Rather than focusing on the execution of statements, functional languages focus on the output of mathematical functions and evaluations. Each function–a reusable module of code–performs a specific task and returns a result.
5 Types of Programming Language | Coursera
www.coursera.org/gb/articles/types-programming-languagewww.coursera.org › types-programming-language5 Types of Programming Language | Coursera
www.coursera.org › types-programming-languageMay 20, 2024 · Each series of steps is called a procedure, and a program written in one of these languages will have one or more procedures within it. Common examples of procedural languages include: C and C++. Java. Pascal. BASIC. 2. Functional programming languages. Rather than focusing on the execution of statements, functional languages focus on the ...
www.geeksforgeeks.org › what-is-procedural-languageWhat is Procedural Language? - GeeksforGeeks
www.geeksforgeeks.org › what-is-procedural-languageMay 6, 2024 · Procedural Language is also known as 3GL which means third generation language. It is a type of programming language that follows a procedure; set of commands or guidelines that must be followed for smooth execution of the program. It works on step by step basis.
en.wikipedia.org › wiki › List_of_programmingList of programming languages by type - Wikipedia
en.wikipedia.org › wiki › List_of_programming6 days ago · Procedural languages. Procedural programming languages are based on the concept of the unit and scope (the data viewing range) of an executable code statement. A procedural program is composed of one or more units or modules, either user coded or provided in a code library; each module is composed of one or more procedures, also called a ...
profiletree.com › what-is-a-programming-languageWhat Is a Programming Language? An Introduction to Coding
profiletree.com › what-is-a-programming-languageMay 5, 2024 · They can be divided into different types, including low-level and high-level languages, procedural, functional, object-oriented, scripting and logic programming languages. Computer Languages vs. Programming Languages. Computer languages and programming languages may sound the same, but they’re not.
www.baeldung.com › java-functional-programmingFunctional Programming in Java | Baeldung
www.baeldung.com › java-functional-programmingMay 11, 2024 · One of the popular programming paradigms known as Object-Oriented Programming (OOP) extends procedural programming concepts. In contrast, the declarative approach expresses the logic of a computation without describing its control flow in terms of a sequence of statements.
www.techgeekbuzz.com › blog › what-is-functionalWhat is Functional Programming? A Beginner's Guide - Techgeekbuzz
www.techgeekbuzz.com › blog › what-is-functionalMay 19, 2024 · Many times, we get confused between Procedural programming and Functional programming. However, both are different, and the main disparity is the outcome of the same input. As we know, Functional Programming is completely embraced by mathematics.
www.techgeekbuzz.com › blog › procedural-programmingProcedural Programming - Definition, Advantages, and ...
www.techgeekbuzz.com › blog › procedural-programming2 days ago · Procedural vs. Object-Oriented Programming. Often, we compare procedural and object-oriented programming paradigms. This is because both can deal with data, and modules, such as procedural programming functions and object-oriented methods. Procedures and modules operating on data are treated as separate entities in procedural programming.