Functional-imperative programming

  • Thread starter Thread starter 0rthodontist
  • Start date Start date
  • Tags Tags
    Programming
AI Thread Summary
A function written in an imperative style can have no side effects if it only uses local variables and its declared inputs, aligning with functional programming principles. The discussion explores whether functional programming languages exist that allow imperative-style functions while ensuring they have no side effects. The focus is on languages or compilers that can certify functions as "no side effects" and "well defined." The C++ standard template library (STL) is mentioned as an example that encourages functional programming practices within an imperative language, particularly through template metaprogramming, which operates at compile-time and does not produce side effects. The conversation also touches on the distinction between side effects in functional programming and the implications of using global state in imperative languages like C++. Overall, the thread emphasizes the potential for blending imperative and functional programming paradigms while maintaining the benefits of functional programming.
0rthodontist
Science Advisor
Messages
1,229
Reaction score
0
If a function written in an imperative style does not set any variables other than local variables, and has access to no input other than its declared inputs (no keyboard input for example), then the function has no side effects. For example
Code:
Input n
int sum = 0
for i <-- 1 to n
  sum <-- sum + i
return sum
is consistent with functional programming--it has no side effects even though it is written in an imperative style.

Are there functional languages that make use of this--so that individual functions may be written in an imperative style while the whole program is functional in nature? And I don't mean that the language is primarily functional but allows some imperative programming as a special case that you should try to avoid unless absolutely necessary, I mean a language that checks basically imperative functions and verifies they do not have side effects. It seems like this would retain the advantages of functional programming while allowing many things to be implemented more simply.
 
Last edited:
Technology news on Phys.org
Also--physicsforums doesn't seem to be very active on computer science. Is there someplace that talks about computer science--as opposed to technical details of any particular product--and is very active/contains experts?
 
What about doing it the other way? Take an imperative programming language, and write in a functional style.

In fact, the C++ standard template library sort of encourages that!
 
That's basically what I had in mind: adapting an imperative language so that sections of code can be called functional.

From what I've read about it, the STL looks like it emulates the first-class-functions part of functional programming. What I'm really looking for is some language or compiler that has a built in mechanism that can certify functions as "no side effects" and "well defined."
 
I doubt it will suit your purposes, but I think you can't have any side effects from C++'s template mechanisms. :smile:
 
Could you explain how that works? (are we using the same definition of "side effect"? I mean it in the functional-programming sense of not producing any effect other than evaluating to the return value)
 
A template metaprogram "runs" at compile-time. The only statements available to such a program are declarations. The only values accessible to a template metaprogram are those that can be determined at compile-time.

So, in that sense, there cannot be any side effects.


Of course, the whole point of a template metaprogram is to write C++ code; everything declared is subsequently accessible to the C++ program in which it resides... though I don't think that counts as a "side-effect", as you meant it.
 
Hurkyl -

I think he means idempotent functions. And in C++/C it certainly is possible to write code that has side effects. Opening a file is one example.
Because the file "metadata", like file pointers, are globally modifiable.

If I got what he meant.
 
I was talking specifically about the template metaprogramming aspect of C++. You can't open a file with a template metaprogram... you can only use it to generate code that, when executed, will open a file.

(The template metaprogram executes at compile time)
 

Similar threads

Back
Top