Introduction 1.
Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++.
A function is a group of statements that is executed when it is called from some point of the program. The following is its format:
type name ( parameter1, parameter2, ...) { statements }
where:
A function is a group of statements that is executed when it is called from some point of the program. The following is its format:
type name ( parameter1, parameter2, ...) { statements }
where:
- type is the data type specifier of the data returned by the function.
- name is the identifier by which it will be possible to call the function.
- parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.
- statements is the function's body. It is a block of statements surrounded by braces { }.
Introduction 2.
Functions are easy to use; they allow complicated programs to be parcelled up into small blocks, each of which is easier to write, read, and maintain. We have already encountered the function main and made use of I/O and mathematical routines from the standard libraries. Now let's look at some other library functions, and how to write and use our own.
Calling a Function
The call to a function in C simply entails referencing its name with the appropriate arguments. The C compiler checks for compatibility between the arguments in the calling sequence and the definition of the function.Library functions are generally not available to us in source form. Argument type checking is accomplished through the use of header files (like stdio.h) which contain all the necessary information. For example, as we saw earlier, in order to use the standard mathematical library you must include math.h via the statement
Writing Your Own Functions
A function has the following layout:return-type function-name ( argument-list-if-necessary ) { ...local-declarations... ...statements... return return-value; }If return-type is omitted, C defaults to int. The return-value must be of the declared type.
A function may simply perform a task without returning any value, in which case it has the following layout:
void function-name ( argument-list-if-necessary ) { ...local-declarations... ...statements... }Arguments are always passed by value in C function calls. This means that local ``copies'' of the values of the arguments are passed to the routines. Any change made to the arguments internally in the function are made only to the local copies of the arguments. In order to change (or define) an argument in the argument list, this argument must be passed as an address, thereby forcing C to change the ``real'' argument in the calling routine.
As an example, consider exchanging two numbers between variables. First let's illustrate what happen if the variables are passed by value:
#include < stdio.h> void exchange(int a, int b); void main() { /* WRONG CODE */ int a, b; a = 5; b = 7; printf("From main: a = %d, b = %d\n", a, b); exchange(a, b); printf("Back in main: "); printf("a = %d, b = %d\n", a, b); } void exchange(int a, int b) { int temp; temp = a; a = b; b = temp; printf(" From function exchange: "); printf("a = %d, b = %d\n", a, b); }
Run this code and observe that a and b are NOT exchanged! Only the copies of the arguments are exchanged. The RIGHT way to do this is of course to use pointers:
Introduction 3
Writing Functions
Functions divide a program into smaller manageable chunks. It's usual to group related functions into libraries and then use a header file to make public those functions. That way, other parts of your application can call them.Return Type
All functions have a return type. If you omit it, then int is assumed. So you should always specify the return type.A function can return nothing- just like a procedure in pascal. In C this is done with the void type. E.g.
void StartLogging() { // Logging Code } Return Value
Other than in a void function, a value of the function's return type must be returned. This is done with the return statement. This also exits the function. For a void function, return by itself will do.This adds two numbers and outputs C= 13.//ex6_11 #include <stdio.h>int addtwonumbers(int a,int b){ return a + b; }int main(){ int c = addtwonumbers(6,7) ;printf("C= %i ",c) ;return 0;}
Learn about Function Prototypes
Applications are made up of one or more source files. Because we want to group functions into libraries, we can specify functions before we fully implement them. This makes it possible for teams of developers working on a project to compile their code, with calls to external functions before those functions are written.
Compiling precedes Linking so a file can be compiled, even if it can't be linked into an application. If your source file includes a header file with prototypes of functions that you call, then you can compile your file. Function prototypes are one line stubs. Instead of the function block with code inside curly braces {}, it ends with a semi-colon.
Example
Taking the getpercent() function in example ex6_2 on the previous page, the prototype looks like this. double getpercent(double a, double b ) ; Prototypes enable the compiler to check the number and types of parameters before generating code. Before prototypes were introduced in ANSI C, earlier C compilers weren't strict about checking. As a programmer, you expect the compiler to spot silly mistakes like that, not generate faulty code! No Parameter Functions
If your function has no parameters, use the keyword void to specify this. The function InitializeData() has no parameters. int InitializeData( void ) ; void EndGame( char * Message) ; Note : InitializeData() returns an int but EndGame("Message") doesn't.
No comments:
Post a Comment