Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.

Similar presentations


Presentation on theme: "Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin."— Presentation transcript:

1 Topic 4 – Programmer- Defined Functions

2 CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin with the first statement in the main function and end when the main function returns. The ability to create functions assists in the use of top-down design.

3 CISC 105 – Topic 4 Top-Down Design Top-down design refers to the breaking of a large problem into its component sub-problems. This process is repeated until each sub- problem can be solved easily, typically through the use of a function.

4 CISC 105 – Topic 4 Top-Down Design This design approach can be illustrated through the development of an algorithm to draw a simple stick-figure. We can divide this drawing process into a three-step process: (1) draw a circle, then (2) draw a plus sign, and, finally, (3) draw an upside down V. Each of the final steps is easily implemented.

5 CISC 105 – Topic 4 Functions A function is a set of C statements that performs a specific task. Functions take data (their arguments) and return data (their return type). A function can take as many arguments as desired, however, they can only return one piece of data (i.e. an int, or a float, etc...)

6 CISC 105 – Topic 4 Declaring and Defining a Function To create a programmer-defined function, two components are necessary. First, the function needs a function declaration. This is a list of the function’s return type, the function name, an open paren, the function arguments’ types and names, a close paren, and then a semicolon.

7 CISC 105 – Topic 4 Function Declarations Therefore, if a function named solve_quad takes two float -type arguments and one int -type argument and returns a float -type, the function declaration would look like: float solve_quad(float x, float y, int z);

8 CISC 105 – Topic 4 Function Declarations Each function needs to be declared, in the same manner as each variable needs to be declared. All of the function declarations appear after the preprocessor directives and before the main function. Notice that function declarations simply list the functions and their argument and return types; they do not specify the inner workings of the function.

9 CISC 105 – Topic 4 Function Definitions The second component required of a function is the specification of its inner workings. Thus, a function definition is simply a sequence of statements. The function definition begins just like the main definition, with a function header. It begins with the return type, then the function name, an open paren, the argument list with names for each variable, and a close paren.

10 CISC 105 – Topic 4 Function Definitions Note that the argument list in the function definition assigns a name to each input argument. Thus, the argument list in the definition also serves as variable declarations for those variables. These variables are initially set to the values passed into the function by the function call.

11 CISC 105 – Topic 4 Function Definitions Following the function header is an open brace, “{“. After the brace is the sequence of C statements which are to be executed. This typically begins with the declaration of local variables, in the same manner as in the main function. Then, the executable statements that compose the function occur.

12 CISC 105 – Topic 4 Function Definitions The final statement is a return statement, with one parameter. This parameter is of the data type specified in the function declaration and the function header. This is the value that the function call evaluates to. After the return statement is a closing brace, “}”

13 CISC 105 – Topic 4 Function Definitions Therefore, to write a program that asks the user for a number, squares it, and displays the result, with the squaring implemented in a function named square_it, the C program would look like:

14 CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); }

15 CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); } This is the function declaration. It specifies that there is a function named square_it that takes a float type argument and returns a float.

16 CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); } This is the function call. It calls the square_it function with its float type argument equal to the value of number. Notice that first the function runs then the expression square_it ( number ) evaluates to the square of number.

17 CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); } This is the function definition. The header specifies that square_it returns a float and takes a float as argument. Inside the function, this is named x. The function calculates the square of x, and then returns that result.

18 CISC 105 – Topic 4 Variables and Functions Each variable is only valid within the function in which it is declared. Therefore, the square_it function knows nothing about the number and squared variables declared in main. The main function knows nothing about the result variable declared in square_it. The region, or area of the program, in which a variable is valid is called the scope of the variable.

19 CISC 105 – Topic 4 Variables and Functions A variable is said to be in scope when it is valid. Thus, any variables declared in (at the beginning of) a function are in scope within the function. A variable is said to be out of scope when it is not valid. Thus, any variables declared in a function are out of scope outside of that function, including in other functions.

20 CISC 105 – Topic 4 Variables and Functions When a function is called, copies are made of each of the arguments. These copies are placed in the variables declared in the function definition header. The values of the original values in the calling function are not altered when passed to a function during a call.

21 CISC 105 – Topic 4 Example number squared Before the function call, number and squared are in scope. Space is reserved in memory for each of the two variables. The scanf statement sets the number variable to the user’s input. In this case, we’ll assume the user input 6. 6 6 x Next, the function square_it is called. The function definition includes the declaration of the x variable. As the number variable is passed into the function, it’s value (6) is copied into the x variable. result The first statement in the square_it function is a declaration of the result variable. Thus, space is reserved in memory. The multiplication is now performed, setting result equal to the square of x, which is equal to the number the user inputted. In this case, the result is 36. 36 The function then reaches the return statement. It returns the value stored in result. Thus, in the main function, the statement square_it(number) evaluates to the value of result, in this case, 36. Thus, squared gets set to 36. 36

22 CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); }

23 CISC 105 – Topic 4 Functions with no Return Type Functions do not have to return any data. For such a function, the return type in the function declaration and function definition is of data type void. For such a function, there need not be a return statement. The function ends when all of the statements in the definition complete or when a return statement is encountered. If a return statement is placed in such a function, it takes no arguments.

24 CISC 105 – Topic 4 Functions with no Return Type A common use for such functions is to produce output. Such an output function would display some text or some data. As the only purpose of such a function would be to produce output, there does not need to be a return type.

25 CISC 105 – Topic 4 Functions with no Arguments Functions also do not need to receive any arguments. For such a function, the argument list in the declaration and definition is composed of the keyword void, or is simply left blank. A common use for such functions is to obtain necessary input from the user. There need not be any data input into the function; it simply asks for appropriate input and returns the user’s value.

26 CISC 105 – Topic 4 Examples Thus, a function that takes no input arguments, and returns a double data type, would look like: double function1(void); int main() {... } double function1(void) {... }

27 CISC 105 – Topic 4 Examples Thus, a function that takes 2 double input arguments, and returns no data would look like: void function2(double x, double y); int main() {... } void function2(double x, double y) {... }

28 CISC 105 – Topic 4 Examples Thus, a function that takes no input arguments, and returns no data would look like: void function3(void); int main() {... } void function3(void) {... }

29 CISC 105 – Topic 4 So… Why do we use Functions? Using functions in writing programs allows two primary benefits. First, this approach allows for extensive code reuse. Once a function is written, it can be called many times in a program. For a program that frequently performs certain tasks, this makes coding easier.

30 CISC 105 – Topic 4 Code Reuse For example, if twenty lines of instructions for a program are displayed more than once in that program, the statements which display that text can be put into a function and called whenever they are needed. This removes the need to copy and paste code at multiple points in a program. If the program is in the debug stage, this also eliminates the need to change multiple sections.

31 CISC 105 – Topic 4 Abstraction Functions also allow abstraction. Once a problem is divided into subproblems, each subproblem can be implemented in its own function. Thus, the main function is simply a set of function calls, solving one subproblem, then the next subproblem, then the next, etc… This makes program comprehension and maintenance much easier.

32 CISC 105 – Topic 4 Abstraction If the specifications of one subproblem change, only that function needs to be changed. As that subproblem is solved within the function, no other functions need to change. An example would be a tax program. If one function, calculate_tax, calculates the tax owed using a formula, only that function needs to change if the method of calculating tax changes.


Download ppt "Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin."

Similar presentations


Ads by Google