Intrinsic and Simple User-Defined Functions

Intrinsic Functions (or Library Functions)

Prewritten programs that are built-in the compiler and available to be used to compute a singe value.

NOTE: Arguments must be enclosed in parentheses. An argument can be a constant, variable, or expression.

Trigonometric Functions

Example:

Inverse Trigonometric Functions

Example:

Exponential Functions

Example:

Logarithmic Functions

Example:

Absolute Value Functions

Example:

Intrinsic Function Examples

Use of Intrinsic Functions

Intrinsic Functions are used on the right side of an assignment statement.

(See Appendix B in your C text for an additional list of Intrinsic Functions and their respective header files)

User Defined Functions

Library functions are important tools to make programs simpler both to read and write.

To make programs even more simple and easy to follow, we can further break them down by accomplishing simple tasks by means of writing our own functions.

Functions are usually written in a program before the main function. They are written in the same format as main and are given a unique name (any legal C variable name). The functions can then be called by the main function.

In the following example, the function info prints information about the program being run. The function is called in the main function simply by stating its name with an empty set of parentheses. When the main function reaches the call, control is transfered to the info function, which executes, then transfers control back to main.

#include 
#define PI 3.14159
                    /*Prints information about program*/
void info(void) {

  printf("This program calculates the surface area\n");
  printf(" and volume of a solid circular cylindar.\n");
  printf("You will be asked to enter a radius and a\n");
  printf("height.  The program will then tell you both\n");
  printf("what you entered and the calculated information.\n");
}
int main(void) {

  double r, ht, area, vol;
  info();                    /*Calls function info*/
  printf("Enter radius:  ");
  scanf("%lf", &r);
  printf("Enter height:  ");
  scanf("%lf", &ht);

  vol = PI * r*r * ht;
  area = 2 * PI * r*r + 2 * PI * r * ht;

  printf("For a cylinder of radius %f and height %f:\n", r, ht);
  printf("Surface Area = %f\nVolume = %f\n", area, vol);
  return(0);
}
Writing more complicated functions will be discussed in a later lesson.