0

I know other questions exist but I still can't wrap my head around this.

Suppose I have the following code:

struct point
{
    int x;
    int y;
};
void SetPoint(struct point *pt, int x, int y)
{
    pt->x = x;
    pt->y = y;
}

Can this function be declared __attribute__((pure))? It is guaranteed to do nothing besides change the value of the structure that is pointed to.

If not, can someone explain why?

  • Why would it do anything that you didn't tell it to do? – Iharob Al Asimi Mar 02 '18 at 12:25
  • @Iharob It shouldn't. I'm checking to see if functions of this type can be declared as `pure`. –  Mar 02 '18 at 12:25
  • 1
    @IharobAlAsimi The question is whether memory pointed to by a parameter can be modified by a pure function. – Sneftel Mar 02 '18 at 12:28
  • 1
    I think it was clear from documentation of *GCC*. It clearly states that a *pure* function should only return something, it seems that it's *pure* in the sense of mathematics, it evaluates it's arguments and return a value, but does not modify anything or said in the correct terms **has no side effects**. – Iharob Al Asimi Mar 02 '18 at 12:28
  • @Sneftel I understand now, the question is however confusing and specially if you didn't now what *pure* was, it's interesting that one keeps learning and learning. And it's the best reason to live. – Iharob Al Asimi Mar 02 '18 at 12:30
  • 1
    Possible duplicate of [\_\_attribute\_\_((const)) vs \_\_attribute\_\_((pure)) in GNU C](https://stackoverflow.com/questions/29117836/attribute-const-vs-attribute-pure-in-gnu-c). – vgru Mar 02 '18 at 14:35

1 Answers1

6

A pure function doesn't modify anything from outside. Modifying the pointed member IS an outside change, so you shouldn't use the attribute in this case.

Pure should be used for "return-only" functions (no side-effects)

From GCC documentation:

pure functions have no effects except the return value and their return value depends only on the parameters and/or global variables

Ivan Sanz Carasa
  • 1,141
  • 8
  • 16
  • 1
    I just read that, it's like if you made c a functional programming language, a *pure* function has no side effects. – Iharob Al Asimi Mar 02 '18 at 12:27
  • Do you think that "*and their return value depends only on the parameters and/or global variables*" means that you can't call other functions from within a *pure* function? – Iharob Al Asimi Mar 02 '18 at 12:35
  • i would say that you can (without breaking optimizations) only call pure functions from pure functions (like const) @IharobAlAsimi – Ivan Sanz Carasa Mar 02 '18 at 12:36
  • @IvanSanz-Carasa.: You can call other functions even if it not marked pure. – user2736738 Mar 02 '18 at 12:37
  • you can, but you will be breaking the compiler optimizations, only pure functions should be called from pure functions @coderredoc – Ivan Sanz Carasa Mar 02 '18 at 12:38
  • 1
    @IvanSanz-Carasa.: There is a difference right? You can't call vs you can call with optimization thrown away. And hey just saying - yours answer is a good one – user2736738 Mar 02 '18 at 12:39
  • 1
    @coderredoc And that also makes your function not pure by having the potential for side-effects. – Iharob Al Asimi Mar 02 '18 at 12:40
  • @IharobAlAsimi.: Well now that you said not pure - it's you know that it is not. If you accidentally mark a non-pure as pure - the optimization will be stalled that's it. – user2736738 Mar 02 '18 at 12:41
  • It's worth emphasizing that a "pure function" is usually a function which always returns the same result for the same set of input paramters. In GCC, `pure` attribute is a programmer's guarantee that the function doesn't change state, but it can still depend on global variables. `const` attribute, on the other hand, is a guarantee that the function only reads parameters, doesn't read nor mutate any global state, and behaves like a pure mathematical function. – vgru Mar 02 '18 at 14:44
  • 1
    @IvanSanz-Carasa: coderredoc wrote that you can call other functions even if not **marked** pure. Meaning you can call functions that **are pure** even if they are not **marked pure**. A function that calls only pure functions and is otherwise pure satisfies the requirements to be marked pure, even if the functions it calls are not marked pure. – Eric Postpischil Mar 02 '18 at 15:17
  • If the pure function modifies a variable through a pointer passed to it, can it still be marked as `pure`? If it were called again in my case it will have no effect. –  Mar 03 '18 at 13:16