Solo disponible en BuenasTareas
• Páginas : 10 (2369 palabras )
• Descarga(s) : 0
• Publicado : 21 de febrero de 2011

Vista previa del texto
C for Engineers and Scientists: An Interpretive Approach

Chapter 6: Functions
Outline
Introduction Function Definitions Function Prototypes Function Name by Identifier __func__ Recursive Functions - Indirect Recursive Functions Header Files Type Generic Functions Function Files Sample Problem Variable Number Arguments in Functions Note: Some slides have been modified from the original.Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Introduction
• A C program is generally formed by a set of functions. These functions subsequently consist of many programming statements. • By using functions, a large task can be broken down into smaller ones. • Functions are important because of theirreusability. That is, users can develop an application program based upon what others have done. They do not have to start from scratch.

C for Engineers and Scientists: An Interpretive Approach

Function Definitions
• A function can be defined in the form of
return_type function_name(argument declaration) { statements }Example:
int addition(int a, int b) { int s; s = a + b; return s; } int main() { int sum; sum = addition(3, 4); printf(“sum = %d\n”, sum); return 0; }

C for Engineers and Scientists: An Interpretive Approach

The return type can be any valid type specifier.

• The return statement can be used to return a valuefrom the called function to the calling function as in return expression; If necessary, the expression will be converted to the return type of the function. However, if the expression cannot be converted to the return type of the function according to the built-in data type conversion rules implicitly, it is a syntax error.
Example:
int func(void) { double d; d = 4.6; return d; // OK: C typeconversion, return 4 }

C for Engineers and Scientists: An Interpretive Approach

• If the return type is not void, a return statement is necessary at the end of the function in C99. Otherwise, the default zero will be used as the return value and a warning message will be produced by the system in C90 and Ch. • A callingfunction can freely ignore the return value. Example:
int func(int i){ return i+1; // the same as „return (i+1);‟ } int main() { int j; j = func(4); func(5); // ignore the return value return 0; }

C for Engineers and Scientists: An Interpretive Approach

If the return type is void, the return statement isoptional. However, no expression should follow return; otherwise, it is a syntax error. Example: void func(int i) { if(i == 3) { printf("i is equal to 3 \n"); return i; // ERROR: return int } else if(i > 3) { printf("i is not equal to 3 \n"); return; // OK } i = -1; // return not necessary since return type is void } int main(){ func(2); return 0; }

C for Engineers and Scientists: An Interpretive Approach

• The data type of an actual argument of the calling function can be different from that of the formal argument of the called function as long as they are compatible. The value of an actual argument will be converted to the data type of its formal definition according to the built-in data conversion rulesimplicitly at the function interface stage.
Example: int func1(int i) { return 2*i; } int main(){ func1(5); func1(5.0); return 0; } // argument type is int

// OK // OK, 5.0 converted to 5