Deep C Dives: First Class Functions |
Written by Mike James | ||||||
Tuesday, 27 August 2024 | ||||||
Page 2 of 2
Functions FirstC’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, To declare a function we simply leave out the body, the block enclosed in curly brackets: returnType FunctionName(type parameter1, 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:
Deep C Dives
|
Apache Releases Tomcat 11 07/11/2024 Apache has announced the release of Tomcat 11, as well as marking the 25th anniversary of the first commit to the Apache Tomcat source code repository since becoming an ASF project. |
Sequin - Open Source Message Stream Built On Postgres 31/10/2024 Sequin is a tool for capturing changes and streaming data out of your Postgres database, guaranteeing exactly once processing. What does that mean? |
More News
|
Comments
or email your comment to: comments@i-programmer.info