|
|
Functions and Parameters in CA tutorial for beginner or intermediate programmers about passing, naming, and return values with sample code.
This tutorial teaches the use of functions in C/C++ programming, aimed at beginner to intermediate programmers. Parameter passing, naming and return values are covered.
IntroductionIn this C programming tutorial, we are going to learn how to create user defined functions containing code statements designed to fulfil a specific task. If the reader has not yet set up their environment using the Borland C compiler (or equivalent) they should refer to the Setting up C article before reading the rest of this tutorial. Before continuing, the reader is advised to take a look at the following general programming articles if they have not programmed before: These tutorials are also part of the Computer Programming Topic here at Suite101. FunctionsA function is a user defined piece of code that should encapsulate a single task. Frequently, there is the temptation to cover many different processes within a single function, but this is considered bad style. Instead, try to break down a task into several pieces, and link them together, with each piece in a separate function. The key to using functions is to imagine that a reader might wish to read ten lines of code, and understand what the design of the program is, but that these ten lines might represent hundreds of lines of code that the reader does not need to look at. For example, we might see a line of code that says: DisplayMenuOptions(); We know that the outcome will be to display the menu options on the screen, and that to do this, many lines of code must be evaluated. However, from the single function call, we can infer that behavior without having to skip over it when looking for a specific piece of code, not related to menu options. Were this not in a function, then we would be obliged to read through all the menu display code before finding the one nugget we are interested in. This would quickly become tedious. The other aspect of programming using functions is known as re-use. The re-use mechanism allows us to define functions which can be used in several places in the code. So, our DisplayMenuOptions function might be reused when displaying sub menus, for example. Parameters & Return ValuesTo achieve this, it would be necessary to tell the function which menu to display; assuming that we have one or more levels of sub menu. So, we can pass values to the function which indicate:
These parameters can be of any type, either built-in to C, or other data types that have been defined by the programmer. As long as the C compiler can locate the types (i.e. they are in scope) at compile time, then any can be used. Assuming we use integers, we might call the function to display menu options as follows: DisplayMenuOptions(ROOT_MENU, NO_PREVIOUS_MENU); Of course, the above snippet also requires that we define (either in an #included file, or at the top of the source code) some values to pass to the function: // Menu option definitions #define ROOT_MENU 0 #define NO_PREVIOUS_MENU -1 Thus the function takes, in this case, two integer parameters. We shall see how to tell the compiler what these parameters are called, and their type later on. Parameters can be passed by reference or by value. The difference is that those arguments passed by reference can be altered inside the function, whereas those passed by value cannot - a copy of the actual value is passed, rather than the variable itself. Passing a constant value implies that it should be passed by value, even if the function definition would allow it to be passed by reference. So, the difference between the two is only apparent when passing variables defined in the calling code. Functions can also return a value, which can be any scalar type. Like the main function, which we have seen before, they can also return void, indicating that there is no value to be returned. We shall see more about parameters and return values as we progress through the remaining articles in this series. Function DefinitionsIn order to tell the compiler what a function looks like, we need to specify the name, return value, and parameters. If we want to use the function in a code block, then it must be defined before that code block. The function definition for our DisplayMenuOptions function might look like: void DisplayMenuOptions(int nDisplayMenu, int nCameFromMenu) { // Code statements here } If the programmer uses a lot of functions (good practice), they will find that in defining them all before the code block in which they are used can lead to a lot of code at the start of a program. Solving this leads to 2 possibilities:
Prototyping just means that the definition of the function can be placed at the start of the file, with the actual function defined at the end of the file: void DisplayMenuOptions(int nDisplayMenu, int nCameFromMenu); It is better practice, however, to store these external functions in a new file. The #include FileTo do this, we need two files - one ending in .h, called the header file, and another ending in .c, which is the source file. So, we might choose to arrange the code for our menu processing function as follows: File : menu_processing.h void DisplayMenuOptions(int nDisplayMenu, int nCameFromMenu); File : menu_processing.c void DisplayMenuOptions(int nDisplayMenu, int nCameFromMenu) { // Code statements here... } To make this work, we then have two steps to perform. The first is to place a line in the main source code file: #include "menu_processing.h" This will allow the compiler to compile the main file. However, in order to actually include the code in the finished application, the menu_processing.c file has to be compiled into a menu_processing.obj object code file. The linker also has to link the new object code into the main project. To accomplish this, we simply list these dependencies in the makefile for the project: OBJFILES = $(APP).obj, menu_processing.obj And... we're done. Questions can be asked through the message board below; so, let's interact!
The copyright of the article Functions and Parameters in C in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Functions and Parameters in C in print or online must be granted by the author in writing.
Comments
Apr 11, 2007 7:32 PM
damieon :
Apr 12, 2007 12:36 AM
Guy Lecky-Thompson :
2 Comments
|
|
|
|
|
|
|
|