Ch. 5: Functions

Ch. 5: Functions - Functions Outline Introduction Function...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Functions Outline Introduction Function Definitions Function Prototypes Function Name by Identifier __func__ Recursive Functions Header Files Random Number Generation Type Generic Functions Function Files Sample Problem Indirect Recursive Functions Variable Number Arguments in Functions Extensions in Ch Nested Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 their reusability. That is, users can develop an application program based upon what others have done. They do not have to start from scratch.
Background image of page 2
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; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The traditional function definition, know as K&R C, is still supported in C. In this notation, parameter identifiers in a function definition are separated by the declaration list. Example: return_type function_name(argument) argument_declaration { statements } int addition(a, b) int a; int b; { return a + b; } or int addition(a, b) int a, b; { return a + b; }
Background image of page 4
The return type can be any valid type specifier. •T h e return statement can be used to return a value from 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 type conversion, return 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 calling function can freely ignore the return value. Example : int func(int i) { return i+1; // the same as ‘return (i+1);’ } int j; j = func(4); func(5); // ignore the return value
Background image of page 6
If the return type is void , the return statement is optional. 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; } func(2);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
If the number of arguments passed to the called function is less than or more than the number of formal definitions, it is a syntax error. Example:
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/21/2008 for the course EME 005 taught by Professor Cheng during the Fall '07 term at UC Davis.

Page1 / 52

Ch. 5: Functions - Functions Outline Introduction Function...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online