CSCI 101 Fundamentals of Computer Programming Lab 4 Functions Fall 2005 Introduction Functions are an integral part of almost every programming language Personally it serves two purposes One it allows for the ease of use of repeated programs Instead of having to rewrite the program to do the exact same thing each and every time it is much easier to simply write a function and call that every time when needed Think of the greeter program that has been used throughout almost every program since the first lab It does exactly the same thing each and every time asking for the user s name and storing it in a char array variable The second use of functions is compartmentalization or modularization of programs The size of programs written will soon get larger and functions provide an approach to write neater code which is easier to read and debug Every function has the following format outputType functionName inputType1 inputName1 inputType2 inputName2 function body return outputType outputName The name of the function is obviously functionName and it will return a variable of type outputType It will take in numerous arguments which are formatted in the way shown above Notice that every function has opening and closing braces This defines the body of the function where all local definitions calculations and output return happen Inside the body local variables can be declared that is only part of the function This is useful for convoluted calculations when the programmer might want to break them down to shorter steps At the end of the body it will have to return an output outputName whose type must be identical to outputType In C the functions are usually situated above the main function after the header files The simple reason is that the compiler compiles the program top down Therefore to allow for the use of the functions it will have to recognize them first by processing through them before the main function Example A very good example of a function is one that you have been using since your started writing your first program It is none other than the main function The main function is unique in the sense that the compiler recognizes it and that will be the first function it runs You will notice that it will have the exact same format as above Typically its outputType will either be int or void and the inputTypes void If the outputType is int it will usually return 0 notice that it is of type int at the end of the program Another simple example will be shown below void greeter char name printf nHello s n name As it will not return anything its outputType is set to void Its inputType is of type char array with the inputName name The body of the function only requires one line as that is the only thing the function does Now instead of calling the printf statement you can simply call the greeter program which will do exactly the same thing Exercise For your first function exercise you will redo the exercise from last week except that you are to use functions for a few of the tasks Your program must do the following where parts c d e f must be done in four separate functions a Prompt the user for his her name b Prompt the user to enter two coordinates in the Cartesian coordinate system x1 y1 x2 y2 c Calculate and outputs the distance of the two points given the two points d Calculates and outputs the angle it makes with the horizontal e Calculates and outputs the slope of the line f Print out your outputs in the main function Show your completed program to your TA LA to receive lab credit
View Full Document