Functions in C
Note: All the programs mentioned in this lecture are in:/home/cs352/SUMMER02/lecture6progs/
- FUNCTIONS - Individual objects that can't be nested. - A function can't be divided into more than one file. However different functions of a program can be in different files. - See overwrite.c for overwriting functions. - Function Prototypes: - Functions should be declared before they're used. - Number and type of arguments, and type of return value. Ex: void fun(char c, int i); == void fun(char, int); identifiers optional at this phase - aoutomatic conversion can be done.(see funconvert.c) - Function Definition: - General form is return_type func_name(parameter list){ declarations statements } - A function can't be defined inside another function. - if return type not specified, int by default. but do specify it. - the return statement tells to return to the calling environment. General form is: return expr; OR return; - see factorial.c - declarations inside the function:local variables other variables can be declared external to the function, then called "global variables" - Formal parameters:the ones in the parameter list Actual parameters:the ones in the function call - All Arguments are passed "call by value": Each argument is evaluated and its value is used locally in place of the corresponding formal params. (So the value DOES NOT change.) See callbyvalue.c (A local copy of the input is made for the actual argument) - Scope Rules: - Identifiers accessible only wihin the block they're declared. see scope.c