Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

[style] one-line functions in C

Name: Anonymous 2010-04-11 13:03


int frozen(int arg)
{
  return (arg+23);
}


OR


int frozen(int arg)
{ return (arg+23); }

int frozen(int arg)
  { return (arg+23); }


OR MAYBE EVEN SCREEN ESTATE SAVING


int frozen(int arg) { return (arg+23); }


The first version would be consistent myth my usual style, but version 2.2 looks too sweet to resist.

Name: Anonymous 2010-04-11 13:11

Please, go buy tobacco and never come back.

Name: Anonymous 2010-04-11 13:12

int
frozen (
  int   arg
) {
        return
                ( arg + 23 );
}

or perhaps even
#define frozen(arg) ((arg)+23)

Name: Anonymous 2010-04-11 13:27

If you're writing one-line functions in C, you're doing it wrong. At best this is something that should be done through the preprocessor.

Name: Anonymous 2010-04-11 13:37

>>4
You'd best be trolling. If it's efficiency you're concerned about, then it should be the compiler's job to inline trivial functions.

Name: Anonymous 2010-04-11 13:37


int
     frozen
     (
         int
         arg
     )
{
     return
     (
         arg
       +  23
//     -----
     );
}


>>4
HIBFV?

Name: Anonymous 2010-04-11 13:39

>>5
If it fits on one line, it's too short to deserve to be its own function, and relying on the compiler to pick up the slack of your own incompetence is a counterproductive habit.

Name: Anonymous 2010-04-11 13:48

>>7
0/10 all around.

Name: Anonymous 2010-04-11 13:48

>>5
Then prefix the function definition with inline, you fool!

Name: Anonymous 2010-04-11 13:49

frozen = (+ 23)

Name: Anonymous 2010-04-11 13:59

         arg
       +  23
//     -----

I must admit, I lol'd out loud at this.

Name: Anonymous 2010-04-11 14:03

>>9
C99 is an abomination, and people who use it should be shot.

Name: Anonymous 2010-04-11 14:10

>>8
I remember when people would put some effort into these discussions, instead of just pretending their opponents were trolls as soon as they realised they were wrong.

Wait, no I don't.

Name: Anonymous 2010-04-11 14:15

int frozen(int arg) {
  return (arg+23); }


Both readable and not wasting space.

Name: Anonymous 2010-04-11 14:23

>>14
implying that "space" can be "wasted"

Name: Anonymous 2010-04-11 14:34

int
frozen(arg)
int arg;
{
    return arg + 23;
}

Name: Anonymous 2010-04-11 14:43

(defun frozen (arg)
    (+ arg 43))

Name: Anonymous 2010-04-11 14:48

>>15
Your post is proof that space can indeed be wasted.

Name: Anonymous 2010-04-11 14:59

I write real code so we actually use the same standard on all functions.

// style.h
#define RETURN_TYPE
#define FUNCTION_NAME
#define PARAMETERS (
#define PROTOTYPE );
#define DEFINITION_BEGIN ){
#define DEFINITION_END }

// frozen.h
RETURN_TYPE   int
FUNCTION_NAME frozen
PARAMETERS    int arg
PROTOTYPE

// frozen.c
RETURN_TYPE   int
FUNCTION_NAME frozen
PARAMETERS    int arg
DEFINITION_BEGIN
   return arg + 23;
DEFINITION_END

Name: Anonymous 2010-04-11 15:24

>>19
You need more #defines

#define RETURNING
#define FUNCTION
#define TAKES (
#define AND ,
#define AS
#define INPUT )
#define BEGIN {
#define END }
#define NOW
#define SUM +

RETURNING   int
FUNCTION    sum
TAKES       int a
AND         int b
AS INPUT
BEGIN
        return TAKES a SUM b AS INPUT NOW
END

Name: Anonymous 2010-04-11 15:37

>>20
#define NATIVEINT int

Name: Anonymous 2010-04-11 15:56

>>20
oh shit

- #define NOW
+ #define NOW ;

Name: Anonymous 2010-04-11 16:36

>>20
style.h has a lot more defines.  We don't use function declaration defines in our code, though.  We also have an AND for multiple variables:

PARAMETERS    int a
       AND    int b

That is how the style guide says to do it.

Name: Anonymous 2010-04-11 17:26

int frozen(int arg) {return arg + 23;}

>>5
Agreed. You should generally not worry about this at all during development because it's an optimization step that a) has hard to predict results (making even small functions inline might significantly worsen performance), and b) is extremely easy to change at the end of the development cycle (just move the dang thing to the header file and put 'static inline' in front.) The best solution is of course to let the compiler do it for you; use PGO and LTO where available (and turn off default inline for class scope functions) so you never have to think about manually inlining anything.

Inlining too much can also cause excessive header file dependencies; if an inline function calls another function, you have to move its include to your own header file, causing more dependencies and longer rebuild cycles. If in doubt, don't inline.

That being said, when writing a function like the above, I will put it in the header file as static inline upon writing, since a) the current platforms I'm targeting don't support LTO, b) it's obvious that optimizes down to a single processor instruction on almost all architectures, c) it doesn't require any header file dependencies, and d) it's typesafe (as opposed to a macro).

Name: Anonymous 2010-04-11 17:46

>>24
move the dang thing to the header file
in the header file as static inline
Are you mental?

Name: Anonymous 2010-04-11 18:11

>>23
I see you've thought this through!

Name: Anonymous 2010-04-11 18:12

I regret making this thread.

Name: Anonymous 2010-04-11 18:18

>>25
Why would >>24-san be mental?

Name: Anonymous 2010-04-11 18:18

>>27

It's ok, just confess your sins to Xarn and all will be forgiven.

Name: Anonymous 2010-04-11 18:26

>>28
By putting actual code in header files. Twice.

Name: Anonymous 2010-04-11 18:31

>>25,30
static inline. Where the fuck else do you put an inline function??

Name: Anonymous 2010-04-11 18:36

>>31
static inline. In a header. Why bother?

Name: Anonymous 2010-04-11 18:48

>>24,28,31
Read K&R.

Name: Anonymous 2010-04-11 18:49

>>30
So you're so hardcore that you copy and paste every inline function into every source file it is used in?

Name: Anonymous 2010-04-11 18:55

I don't know who the trolls are anymore.

Name: Anonymous 2010-04-11 19:00

>>35
Happens to the best of us.

Name: Anonymous 2010-04-11 19:54

>>35
Use the checklist:

Troll Checklist
If all boxes are checked at the end of the examination, you are dealing with a troll
[ ] They posted on /prog/

Name: Anonymous 2010-04-11 21:13

[✓] They posted on /prog/
Let me check here, one tick. Aww, dammit! IHBT

Name: Anonymous 2010-04-11 21:59

>>35
Anonymous is the troll.

Name: Anonymous 2010-04-12 4:47

I can't tell who's trolling who. Where does one place an inline function if not in a header file?

And static inline is necessary because gnu99 differs from c99 which also differs from c++ in the behaviour of bare inline. In gnu99, bare inline emits a non-inline function definition. In c99 it does not. In c++ it emits a static function definition.

Instead of trying to preproc all this bullshit, it's better to simply mark it static, and just don't take the address of an inline function. Most linkers are able to merge these definitions anyway (c++ linkers generally have to in order to avoid generating gigantic executables).

Name: Anonymous 2010-04-12 4:51

>>41
Sorry, that should read gnu89. Apparently gcc fixed the behaviour in gnu99.

I don't know how clang handles it but I assume it matches the spec, since it's one of the most pedantic compilers out there. Visual studio also differs from both gcc's c modes; it doesn't support inline in c, but using __inline emits as static inline iirc.

To sum it all up, not using static inline is going to break at some point on something somewhere.

Name: Anonymous 2010-04-12 5:50

#define i int
#define rt return
#define fr frozen
#define ar arg
i fr(i ar){rt ar+23;}

Name: Anonymous 2010-04-12 6:12

This ITT thread is full of COBOL-fags plus some ``Me is smarter than ur compiler''-pussies. Great!

Name: Anonymous 2010-04-12 6:16

>>44
U MENA JAVA

Name: GvR 2010-04-12 6:17

>>1
Curly braces are evil, and now you see precisely why.

def frozen(arg):
    return arg + 23

Name: Anonymous 2010-04-12 8:02

>>46
frozen = (23).__add__

Name: Anonymous 2010-04-12 9:25

>>47
Beautiful, but that's 23+add, not add+23.

Name: Anonymous 2010-04-12 10:31

Name: Anonymous 2010-04-12 12:10

ups, i haxed their servers xd

Name: Anonymous 2010-12-09 19:11

Name: Anonymous 2010-12-21 0:57

Name: Anonymous 2013-01-19 14:41

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List