Transcript Topic4

Topic 4 – Programmer-Defined
Functions
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.
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 subproblem can be solved easily, typically
through the use of a function.
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.
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...)
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.
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);
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.
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.
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.
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.
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,
“}”
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:
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);
}
CISC 105 – Topic 4
Example
#include <stdio.h>
float square_it(float x);
int main()
{
This is the function declaration.
float number, squared;
It specifies
printf(“Enter the
number>”);that there is a function
scanf(“%f”,&number);
named square_it that takes a float
squared = square_it(number);
typeisargument
and returns a float.
printf(“The answer
%f.\n”,squared);
return(0);
}
float square_it(float x)
{
float result;
result = x * x;
return (result);
}
CISC 105 – Topic 4
This is the function call.
It calls the square_it function with
its float type argument equal to
the value of number. Notice that
<stdio.h>
first#include
the function
runs then the
expression
square_it(number)
float square_it(float x);
evaluates to the square of number.
Example
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);
}
CISC 105 – Topic 4
This is the function definition.
The header specifies that square_it
returns a float and takes a float as
#include <stdio.h> argument. Inside the function,
this
float square_it(float
x);is named x. The function
calculates the square of x, and
int main()
then returns that result.
{
Example
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);
}
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.
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.
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.
CISC 105 – Topic 4
Example
number
6
x
6
squared
36
result
36
The
Before
Next,
the
the
function
function
then reaches
square_it
call,
theis return
called.
andfunction
statement.
squared
The function
are
It equal
returns
definition
The
first
statement
in
thenumber
square_it
is
a declaration
The function
multiplication
is
now
performed,
setting
result
The
scanf
statement
sets
the
number
variable
to
thethe
the
in the
includes
scope.
value
the
stored
Space
declaration
inis result.
reserved
of Thus,
the
in xmemory
in
variable.
the
main
for
As
each
function,
the
of
number
of
result
variable.
Thus,
space
is
reserved
in
memory.
to the square of x, which is equal to the number the user
user’s
input.
In into
this the
case,
we’ll
assume
user
6.
statement
the
variable
two variables.
is
square_it(number)
passed
evaluates
it’s value
to the(6)
value
is input
copied
of result,
inputted.
In
this case,
the function,
result
is 36.
in
into
thisthe
case,
x variable.
36. Thus, squared gets set to 36.
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);
}
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.
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.
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.
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)
{
. . .
}
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)
{
. . .
}
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)
{
. . .
}
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.
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.
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.
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.
CISC 105 – Topic 4