Deep C Dives: First Class Functions
Written by Mike James   
Tuesday, 27 August 2024
Article Index
Deep C Dives: First Class Functions
Functions First

Cdive180

Functions First

C’s functions are simple and hence efficient, but their simplicity has a cost. C is a one-pass language in that it can be compiled in a single reading of the program text. This in turn implies that if you want to make use of something, a function say, it has to be declared before it is used. The reason is simple. If you use a function before it is declared the compiler knows nothing about it and cannot check if your use makes sense. This means that in C you have to define your functions before you use them and this means that they have to come in a particular order and all before the main function. Some C compilers will cope if they encounter a function that you haven’t declared before use. They generally warn of “implicit” function declaration but this is not a good idea.

Putting functions before main isn’t the natural way to read a program. You don’t generally want to have to scan through all of the functions before you get to the main program and then have to backtrack to find the functions that are used. The natural way to do things is to read the main program and look up any functions that are used.

To provide a simpler program layout and organization, C allows you to declare variables, including functions, before they are defined.

Declaring a variable just informs the compiler of its type, no memory allocations have to be made at this point.

Defining a variable can be a separate step and it involves allocating memory and perhaps initializing it to a value.

We already know how to define a function:

returnType FunctionName(type parameter1,
type parameter2...){ statements that define the function }

To declare a function we simply leave out the body, the block enclosed in curly brackets:

returnType FunctionName(type parameter1, 
type parameter2 …)

Naming the parameters is pointless as the compiler doesn’t make use of them, so this can be simplified to:

returnType FunctionName(type, type  ...)

Some programmers prefer the brevity of just listing the types, some think that providing the parameter names informs the reader what the function does, especially so if a comment is used. For example:

int sum(int, int);

defines the sum function as taking two integers and returning an integer, but

int sum(int a, int b); // returns a+b

This works when the function is simple, but is less effective as the complexity and number of parameters increases. In most cases it is better to include a brief declaration and leave the comments for the definition.

Once you know about declaring functions, a C program can have a standard form:

function declarations
main(){...}
function definitions

and to make this even easier to read, it is conventional to put the function declarations into a header file.

#include declarations.h
main(){…}
function definitions

Things are a little more subtle than this in that the function definitions don’t have to be in the same file/compilation unit. By default function declarations are assumed to be extern, i.e. external. This means that if you declare a function and do not define the function within the same file, the linker will look for the function within the libraries that have been specified.

What this means is that by default any function you declare and define in a file can be used from other files. In this sense every function is by default global to the entire project.

If you want to restrict the function to be “local” and defined in the same file prefix the declaration with static:

static int sum(int, int);

this means that the sum function has to be defined in the same file and it is not available for use in other compilation units – it is internal to the file. Use static in a function declaration when you want to avoid name clashes with functions in any libraries you might be using.

What about variables that are not function pointers, do you need to declare them?

In most cases you can declare and define a variable in the usual way:

int myInteger;

This allocates the memory needed as well as telling the compiler the type of the variable. Of course, variables have to be declared before they are used, but where they are declared modifies where the memory needed is allocated from. The only example of declaring a variable without defining it is when you declare the variable as extern:

extern int myInteger;

This informs the compiler that myInteger is an int, but doesn’t allocate any storage for it as it is assumed that this is done in another compilation unit. That is myInteger is a variable in another compilation unit (file).

You can declare a variable as many times as necessary, but you can only define it once.

In Chapter but not included in this extract:

  • Function Pointers and Type
  • Function Parameters and Return Types
  • Parameters and Arguments
  • Calling Conventions
  • Ignoring Parameters
  • Variadic
  • Return
  • Functions and Big Data
  • Returning More Than One Result
  • Inline Functions
  • C Functions Considered

Deep C Dives
Adventures in C

By Mike James

Cdive360

Buy from Amazon.

Contents

Preface
Prolog C
Dive

  1. All You Need Are Bits
  2. These aren’t the types you’re looking for
  3. Type Casting
  4. Expressions
  5. Bits and More Bits
  6. The Brilliant But Evil for
  7. Into the Void 
  8. Blocks, Stacks and Locals
  9. Static Storage
  10. Pointers
  11. The Array and Pointer Arithmetic
  12. Heap, The Third Memory Allocation
  13. First Class Functions
        Extract:
    First Class Functions ***NEW!
  14. Structs and Objects
  15. The Union
  16. Undefined Behavior
  17. Exceptions and the Long Jump

<ASIN:B0D6LZZQ8R>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Programmers Day 2024
12/09/2024

Today, September 12th, is designated as Programmers Day, a tradition we do our best to uphold. We even designed a card! 



Paul Allen's Historic Computers Under the Hammer
04/09/2024

Dozens of rare historic computers are currently up for auction. As well as a room-sized PDP 7, there's an Altair, an Apple I, a Cray I and an Enigma machine on offer to the highest bidder as the priva [ ... ]


More News

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Tuesday, 27 August 2024 )