topic 4 – programmer- defined functions. cisc 105 – topic 4 functions so far, we have only seen...

32
Topic 4 – Programmer- Defined Functions

Post on 22-Dec-2015

233 views

Category:

Documents


2 download

TRANSCRIPT

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

Topic 4 – Programmer-Defined Functions

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

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.

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

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.

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

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.

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

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...)

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

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.

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

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);

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

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.

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

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.

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

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.

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

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.

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

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, “}”

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

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:

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

CISC 105 – Topic 4

Example#include <stdio.h>

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);

}

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

CISC 105 – Topic 4

Example#include <stdio.h>

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 floattype argument and returns a float.

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

CISC 105 – Topic 4

Example#include <stdio.h>

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 tothe value of number. Notice thatfirst the function runs then theexpression square_it(number)

evaluates to the square of number.

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

CISC 105 – Topic 4

Example#include <stdio.h>

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_itreturns a float and takes a float as

argument. Inside the function,this is named x. The functioncalculates the square of x, and

then returns that result.

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

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.

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

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.

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

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.

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

CISC 105 – Topic 4

Example

number

squared

Before the function call, number and squared are in scope. Space is reserved in memory for each ofthe two variables.

The scanf statement sets the number variable to theuser’s input. In this case, we’ll assume the user input 6.

6 6x

Next, the function square_it is called. The function definitionincludes the declaration of the x variable. As the numbervariable is passed into the function, it’s value (6) is copiedinto the x variable.

result

The first statement in the square_it function is a declarationof the result variable. Thus, space is reserved in memory.The multiplication is now performed, setting result equalto the square of x, which is equal to the number the userinputted. In this case, the result is 36.

36

The function then reaches the return statement. It returnsthe 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

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

CISC 105 – Topic 4

Example#include <stdio.h>

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);

}

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

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.

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

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.

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

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.

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

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){

. . .}

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

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){

. . .}

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

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){

. . .}

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

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.

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

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.

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

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.

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

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.