Search results
Top results related to is procedural and imperative programming the same?
Top Answer
Answered May 05, 2010 · 3 votes
Paul R has a pretty good answer, but I'd like to expand on it a bit. If you think of the sound as a series of pulses (and it kind of is), then a higher pitch will have more pulses per second (higher frequency) and a lower pitch will have fewer (lower frequency). To lower the pitch of an existing sound, you have to spread those pulses out (make them further apart from each other). As a result, the duration of the sound will increase because you haven't reduced the number of pulses, you've just made them further apart (fewer per second). The opposite happens if you try to increase the pitch: the pulses are closer together, thus making the sound shorter in duration.
If you want the duration to remain constant regardless of changes to the recorded pitch, you have to either throw information away (lower pitch) or manufacture information (higher pitch). This is where the fancy processing comes in. What can be safely discarded? What can be safely duplicated or constructed?
1/5
Top Answer
Answered May 23, 2017 · 2 votes
You may want to use the accepted answer in this question as a good starting point.
F# Beginner: retrieving an array of data from a server
Depending on the database you are using you may get some other choices, but start with something fairly functional and you can improve on it as you gain experience.
2/5
Top Answer
Answered Aug 06, 2019 · 186 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 · 114 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 )
3/5
Top Answer
Answered Aug 29, 2015 · 1102 votes
A great C# example of declarative vs. imperative programming is LINQ.
With imperative programming, you tell the compiler what you want to happen, step by step.
For example, let's start with this collection, and choose the odd numbers:
List<int> collection = new List<int> { 1, 2, 3, 4, 5 };-
With imperative programming, we'd step through this, and decide what we want:
List<int> results = new List<int>();foreach(var num in collection){ if (num % 2 != 0) results.Add(num);}
Here, we're saying:
- Create a result collection
- Step through each number in the collection
- Check the number, if it's odd, add it to the results
With declarative programming, on the other hand, you write code that describes what you want, but not necessarily how to get it (declare your desired results, but not the step-by-step):
var results = collection.Where( num => num % 2 != 0);-
Here, we're saying "Give us everything where it's odd", not "Step through the collection. Check this item, if it's odd, add it to a result collection."
In many cases, code will be a mixture of both designs, too, so it's not always black-and-white.
Other Answers
Answered Nov 28, 2012 · 114 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 )
4/5
Top Answer
Answered Jul 22, 2016 · 10 votes
This is a tough one because the terms involved often mean different things to different people.
Roughly speaking (although some would say "strictly speaking") imperative is the opposite of declarative.
Strict OO proponents would likely say that OO is declarative. See the "Object Thinking" style of OO in David West's book of that name and this blog: http://www.yegor256.com/.
OO as it is commonly practiced often "devolves" (again, others will sharply disagree) into a very procedural and imperative style where you start telling the computer what to do rather than describing and modeling the real world (in some sense) with your objects.
(Here's a video that might be useful. It contrasts procedural programming and OO, and for our purposes here "procedural" is roughly a synonym for "imperative".)
I know that's not a great answer, but maybe it's useful.
5/5
softwareengineering.stackexchange.com › questionsWhat's The Difference Between Imperative, Procedural and ...
softwareengineering.stackexchange.com › questionsProcedural programming, on the other hand, is a specific type (or subset) of Imperative programming, where you use procedures (i.e., functions) to describe the commands the computer should perform. First question: Is there an Imperative programming language which is not procedural?
People also ask
What is the difference between procedural programming and imperative programming?
- Procedural programming, on the other hand, is a specific type (or subset) of Imperative programming, where you use procedures (i.e., functions) to describe the commands the computer should perform. First question: Is there an Imperative programming language which is not procedural?
What's The Difference Between Imperative, Procedural and Structured
softwareengineering.stackexchange.com/questions/117092/whats-the-difference-between-imperative-procedural-and-structured-programmingWhat is the difference between imperative programming and functional programming?
- Imperative programming refers to code that is concerned with lower levels of abstraction. Procedural programming is a subset of imperative programming which utilizes subroutines. Functional programming is a subset of declarative programming which utilizes subroutines.
The Differences Between Procedural, Functional, Imperative, and
amzotti.github.io/programming paradigms/2015/02/13/what-is-the-difference-between-procedural-function-imperative-and-declarative-programming-paradigms/Can a language be imperative without being procedural or structured?
- Update: This first question seems to be answered. A language CAN be imperative without being procedural or structured. An example is pure Assembly language. Then you also have Structured programming, which seems to be another type (or subset) of Imperative programming, which emerged to remove the reliance on the GOTO statement.
What's The Difference Between Imperative, Procedural and Structured
softwareengineering.stackexchange.com/questions/117092/whats-the-difference-between-imperative-procedural-and-structured-programmingWhat are functional and procedural programming paradigms?
- The concept of functional and procedural programming paradigms are really just extensions of the concept of declarative and imperative programming paradigms. In fact, functional programming is a subset of declarative programming, and procedural programming is a subset of imperative programming.
The Differences Between Procedural, Functional, Imperative, and
amzotti.github.io/programming paradigms/2015/02/13/what-is-the-difference-between-procedural-function-imperative-and-declarative-programming-paradigms/www.geeksforgeeks.org › what-is-imperative-programmingWhat is Imperative Programming? - GeeksforGeeks
www.geeksforgeeks.org › what-is-imperative-programmingApr 25, 2024 · There are different mutually exclusive domains of imperative programming i.e., procedural programming, structured programming, modular and OOP. However, it is also important to note that all of these programming paradigms are present as the subdomains of imperative programming to provide evolution and address some challenges but there are some ...
en.wikipedia.org › wiki › Imperative_programmingImperative programming - Wikipedia
en.wikipedia.org › wiki › Imperative_programmingIn computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform.
en.wikipedia.org › wiki › Procedural_programmingProcedural programming - Wikipedia
en.wikipedia.org › wiki › Procedural_programmingProcedural programming is classified as an imperative programming, because it involves direct command of execution. Procedural is a sub-class of imperative since procedural includes block and scope concepts, whereas imperative describes a more general concept that does not require such features.
amzotti.github.io › programming paradigms › 2015The Differences Between Procedural, Functional, Imperative ...
amzotti.github.io › programming paradigms › 2015Feb 13, 2015 · Imperative programming refers to code that is concerned with lower levels of abstraction. Procedural programming is a subset of imperative programming which utilizes subroutines. Functional programming is a subset of declarative programming which utilizes subroutines.
www.baeldung.com › cs › procedural-programmingWhat Is Procedural Programming? | Baeldung on Computer Science
www.baeldung.com › cs › procedural-programmingJul 21, 2024 · 1. Overview. In this tutorial, we’ll dive into the procedural programming paradigm. We’ll learn about its key characteristics and how it’s different from other paradigms. 2. Procedural Programming. Procedural programming is an imperative programming paradigm. In procedural programming, we organize sets of statements in procedures run sequentially.
www.freecodecamp.org › news › an-introduction-toProgramming Paradigms – Paradigm Examples for Beginners
www.freecodecamp.org › news › an-introduction-toMay 2, 2022 · Procedural programming is a derivation of imperative programming, adding to it the feature of functions (also known as "procedures" or "subroutines"). In procedural programming, the user is encouraged to subdivide the program execution into functions, as a way of improving modularity and organization.
Searches related to Is procedural and imperative programming the same?