Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional programming in C/C++?

I have been reading this article: http://en.wikipedia.org/wiki/Function_pointer and am sort of confused. Since C/C++ support function pointers, doesn't that mean they support functional programming in general? Note that I don't actually want to use C or C++ for functional programming, but I'm curious since I have never heard that C or C++ supports such a thing. (I know that compilers for many functional programming languages exist in C, but that is not what I really mean by "support").

like image 256
user2258552 Avatar asked Jun 03 '13 23:06

user2258552


People also ask

What is functional programming in C?

Introduction. Functional programming is a programming paradigm in which we try to bind everything in pure mathematical functions style. It is a declarative type of programming style. Its main focus is on “what to solve” in contrast to an imperative style where the main focus is “how to solve”.

Can functional programming be done in C?

Functional Programming - StringsA string can also be regarded as a predefined class which is supported by most of the programming languages such as C, C++, Java, PHP, Erlang, Haskell, Lisp, etc.

Is C functional or OOP?

C is not object oriented language. C is a general-purpose, imperative language, supporting structured programming. Because C isn't object oriented therefore C++ came into existence in order to have OOPs feature and OOP is a programming language model organized around objects.


1 Answers

Functional programming (please look it up if you're really interested) has little to do with functions pointers or lack of them.

C++ is a multi-paradigm language with a great deal of support for FP, especially the later versions. Many people working with WG21 like FP and push for support. In C++11 we even got lambda and in C++14 polymorphic lambdas debut. That covers many things. While functions stay second-class citizens lambda can take over fine.

Unfortunately tail-recursion handling is still not mandatory, but compilers actually handle it, and in the last decade even conveniently report "infinite recursion" when you just messed up the const overload. :)

You can go pretty far using FP style in C++, and learning it helps you make better code even when you chose other styles. I encourage everyone to study SICP.

like image 151
Balog Pal Avatar answered Sep 23 '22 16:09

Balog Pal