Previous: Control | C Programming | Next: Standard libraries |
In C programming, all executable code resides within afunction. Note that other programming languages may distinguish between a "function", "subroutine", "subprogram", "procedure", or "method" -- in C, these are all functions. Functions are a fundamental feature of any high level programming language and make it possible to tackle large, complicated tasks by breaking tasks into smaller, more manageable pieces of code.
At a lower level, a function is nothing more than a memory address where the instructions associated with a function reside in your computer's memory. In the source code, this memory address is usually given a descriptive name which programmers can use tocall the function and execute the instructions that begin at the function's starting address. The instructions associated with a function are frequently referred to as ablock of code. After the function's instructions finish executing, the function can return a value and code execution will resume with the instruction that immediately follows the initial call to the function. If this doesn't make immediate sense to you, don't worry. Understanding what is happening inside your computer at the lowest levels can be confusing at first, but will eventually become very intuitive as you develop your C programming skills.
For now, it's enough to know that a function and its associated block of code is often executed (called) several times, from several different places, during a single execution of a program.
As a basic example, suppose you are writing a program that calculates the distance of a given (x,y) point to the x-axis and to the y-axis. You will need to compute the absolute value of the whole numbers x and y. We could write it like this (assuming we don't have a predefined function for absolute value in any library):
#include<stdio.h>/*this function computes the absolute value of a whole number.*/intabs(intx){if(x>=0)returnx;elsereturn-x;}/*this program calls the abs() function defined above twice.*/intmain(){intx,y;printf("Type the coordinates of a point in 2-plane, say P = (x,y). First x=");scanf("%d",&x);printf("Second y=");scanf("%d",&y);printf("The distance of the P point to the x-axis is %d.\n Its distance to the y-axis is %d.\n",abs(y),abs(x));return0;}
The next example illustrates the usage of a function as a procedure. It's a simplistic program that asks students for their grade for three different courses and tells them if they passed a course. Here, we created a function, calledcheck()
that can be called as many times as we need to. The function saves us from having to write the same set of instructions for each class the student has taken.
#include<stdio.h>/*the 'check' function is defined here.*/voidcheck(intx){if(x<60)printf("Sorry! You will need to try this course again.\n");elseprintf("Enjoy your vacation! You have passed.\n");}/*the program starts here at the main() function, which calls the check() function three times.*/intmain(){inta,b,c;printf("Type your grade in Mathematics (whole number).\n");scanf("%d",&a);check(a);printf("Type your grade in Science (whole number).\n");scanf("%d",&b);check(b);printf("Type your grade in Programming (whole number).\n");scanf("%d",&c);check(c);/* this program should be replaced by something more meaningful.*/return0;}
Notice that in the program above, there is no outcome value for the 'check' function. It only executes a procedure.
This is precisely what functions are for.
It's useful to conceptualize a function like a machine in a factory. On the input side of the machine, you dump in the "raw materials," or the input data, that you want the machine to process. Then the machine goes to work and and spits out a finished product, the "return value," to the output side of the machine which you can collect and use for other purposes.
In C, you must tell the machine exactly what raw materials it is expected to process and what kind of finished product you want the machine to return to you. If you supply the machine with different raw materials than it expects, or if you try to return a product that's different than what you told the machine to produce, the C compiler will throw an error.
Note that a function isn't required to take any inputs. It doesn't have to return anything back to us, either. If we modify the example above to ask the user for their grade inside thecheck
function, there would be no need to pass the grade value into the function. And notice that thecheck
doesn't pass a value back. The function just prints out a message to the screen.
You should be familiar with some basic terminology related to functions:
It's always good to learn by example. Let's write a function that will return the square of a number.
intsquare(intx){intsquare_of_x;square_of_x=x*x;returnsquare_of_x;}
To understand how to write such a function like this, it may help to look at what this function does as a whole. It takes in anint, x, and squares it, storing it in the variable square_of_x. Now this value is returned.
The first int at the beginning of the function declaration is the type of data that the function returns. In this case when we square an integer we get an integer, and we are returning this integer, and so we writeint as the return type.
Next is the name of the function. It is good practice to use meaningful and descriptive names for functions you may write. It may help to name the function after what it is written to do. In this case we name the function "square", because that's what it does - it squares a number.
Next is the function's first and only argument, anint, which will be referred to in the function as x. This is the function'sinput.
In between the braces is the actual guts of the function. It declares an integer variable called square_of_x that will be used to hold the value of the square of x. Note that the variable square_of_x canonly be used within this function, and not outside. We'll learn more about this sort of thing later, and we will see that this property is very useful.
We then assign x multiplied by x, or x squared, to the variable square_of_x, which is what this function is all about. Following this is areturn statement. We want to return the value of the square of x, so we must say that this function returns the contents of the variable square_of_x.
Our brace to close, and we have finished the declaration.
Written in a more concise manner, this code performs exactly the same function as the above:
intsquare(intx){returnx*x;}
Note this should look familiar - you have been writing functions already, in fact - main is a function that is always written.
In general, if we want to declare a function, we write
typename(type1arg1,type2arg2, ...) { /*code */ }
We've previously said that a function can take no arguments, or can return nothing, or both. What do we write if we want the function to return nothing? We use C'svoid keyword.void basically means "nothing" - so if we want to write a function that returns nothing, for example, we write
voidsayhello(intnumber_of_times){inti;for(i=1;i<=number_of_times;i++){printf("Hello!\n");}}
Notice that there is noreturn statement in the function above. Since there's none, we writevoid as the return type. (Actually, one can use thereturn keyword in a procedure to return to the caller before the end of the procedure, but one cannot return a value as if it were a function.)
What about a function that takes no arguments? If we want to do this, we can write for example
floatcalculate_number(void){floatto_return=1;inti;for(i=0;i<100;i++){to_return+=1;to_return=1/to_return;}returnto_return;}
Notice this function doesn't take any inputs, but merely returns a number calculated by this function.
Naturally, you can combine both void return and void in arguments together to get a valid function, also.
Here's a simple function that does an infinite loop. It prints a line and calls itself, which again prints a line and calls itself again, and this continues until the stack overflows and the program crashes. A function calling itself is called recursion, and normally you will have a conditional that would stop the recursion after a small, finite number of steps.
// don't run this!voidinfinite_recursion(){printf("Infinite loop!\n");infinite_recursion();}
A simple check can be done like this. Note that ++depth is used so the increment will take place before the value is passed into the function. Alternatively you can increment on a separate line before the recursion call. If you say print_me(3,0); the function will print the line Recursion 3 times.
voidprint_me(intj,intdepth){if(depth<j){printf("Recursion! depth = %d j = %d\n",depth,j);//j keeps its valueprint_me(j,++depth);}}
Recursion is most often used for jobs such as directory tree scans, seeking for the end of a linked list, parsing a tree structure in a database and factorising numbers (and finding primes) among other things.
If a function is to be called only from within the file in which it is declared, it is appropriate to declare it as a static function. When a function is declared static, the compiler will know to compile an object file in a way that prevents the function from being called from code in other files. Example:
staticintcompare(inta,intb){return(a+4<b)?a:b;}
We can nowwrite functions, but how do we use them? When we write main, we place the function outside the braces that encompass main.
When we want to use that function, say, using ourcalculate_number function above, we can write something like
floatf;f=calculate_number();
If a function takes in arguments, we can write something like
intsquare_of_10;square_of_10=square(10);
If a function doesn't return anything, we can just say
say_hello();
since we don't need a variable to catch its return value.
While the C language doesn't itself contain functions, it is usually linked with the C Standard Library. To use this library, you need to add an #include directive at the top of the C file, which may be one of the following from C89/C90:
The functions available are:
<assert.h> | <limits.h> | <signal.h> | <stdlib.h> |
---|---|---|---|
|
|
|
|
<ctype.h> | <locale.h> | <stdarg.h> | <string.h> |
|
|
|
|
errno.h | math.h | stddef.h | time.h |
|
|
|
|
float.h | setjmp.h | stdio.h | |
|
|
|
|
Functions with variable-length argument lists are functions that can take a varying number of arguments. An example in the C standard library is theprintf function, which can take any number of arguments depending on how the programmer wants to use it.
C programmers rarely find the need to write new functions with variable-length arguments.If they want to pass a bunch of things to a function, they typically define a structure to hold all those things -- perhaps a linked list, or an array -- and call that function with the data in the arguments.
However, you may occasionally find the need to write a new function that supports a variable-length argument list.To create a function that can accept a variable-length argument list, you must first include the standard library headerstdarg.h. Next, declare the function as you would normally. Next, add as the last argument an ellipsis ("..."). This indicates to the compiler that a variable list of arguments is to follow. For example, the following function declaration is for a function that returns the average of a list of numbers:
floataverage(intn_args,...);
Note that because of the way variable-length arguments work, we must somehow, in the arguments, specify the number of elements in the variable-length part of the arguments. In theaverage function here, it's done through an argument calledn_args. In theprintf function, it's done with the format codes that you specify in that first string in the arguments you provide.
Now that the function has been declared as using variable-length arguments, we must next write the code that does the actual work in the function.To access the numbers stored in the variable-length argument list for ouraverage function, we must first declare a variable for the list itself:
va_listmyList;
Theva_list type is a type declared in thestdarg.h header that basically allows you to keep track of your list. To start actually usingmyList, however, we must first assign it a value. After all, simply declaring it by itself wouldn't do anything. To do this, we must callva_start, which is actually a macro defined instdarg.h. In the arguments tova_start, you must provide theva_list variable you plan on using, as well as the name of the last variable appearing before the ellipsis in your function declaration:
#include<stdarg.h>floataverage(intn_args,...){va_listmyList;va_start(myList,n_args);va_end(myList);}
Now thatmyList has been prepped for usage, we can finally start accessing the variables stored in it. To do so, use theva_arg macro, which pops off the next argument on the list. In the arguments tova_arg, provide theva_list variable you're using, as well as the primitive data type (e.g.int,char) that the variable you're accessing should be:
#include<stdarg.h>floataverage(intn_args,...){va_listmyList;va_start(myList,n_args);intmyNumber=va_arg(myList,int);va_end(myList);}
By poppingn_args integers off of the variable-length argument list, we can manage to find the average of the numbers:
#include<stdarg.h>floataverage(intn_args,...){va_listmyList;va_start(myList,n_args);intnumbersAdded=0;intsum=0;while(numbersAdded<n_args){intnumber=va_arg(myList,int);// Get next number from listsum+=number;numbersAdded+=1;}va_end(myList);floatavg=(float)(sum)/(float)(numbersAdded);// Find the averagereturnavg;}
By callingaverage (2, 10, 20), we get the average of10 and20, which is15.
Previous: Control | C Programming | Next: Standard libraries |