Tuesday, January 25, 2011

1.7 Functions

  • A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation
  • Function definition:

  • Function definitions declare the parameter types and names, and the type of the result that the function returns ie. int power (int base, int n)
  • Function definitions can appear in any order, and in one source file or several, although no function can be split between files.
  • Power function exm:











  • Generally use parameter for a variable named in the parenthesized list in a function definition, and argument for the value used in a call of the function
  • The name used by function definition for its parameters are local to the function definition, and are not visible to any other function
  • The value that function definition computes is returned to main function by the return expression
  • A function need not return a value; a return statement with no expression cause control, but no useful value, to be returned to the caller
  • In main function, a return value of zero implies normal termination; non-zero values implies erroneous termination condition
  • Function prototype example: int power (int m, int n); expect two int arguments and returns an int
  • Function prototype declaration has to agree with the definition and uses of functions parameter names are optional in a function prototype.

No comments:

Post a Comment