10.0 Functions
10.0 Functions
1 [EEE]
Lecture 10: Functions
M. Arifur Rahman
Introduction
• A function is a series of statements that have been
grouped together and given a name.
• Each function is essentially a small program, with its own
declarations and statements.
• Advantages of functions:
A program can be divided into small pieces that are easier to
understand and modify.
We can avoid duplicating code that’s used more than once.
A function that was originally part of one program can be reused in
other programs
2
Defining and Calling Functions
• Before we go over the formal rules for defining a
function, let’s look at three simple programs that define
functions.
3
Program: Computing Averages
• A function named average that computes the average of
two double values:
double average(double a, double b)
{
return (a + b) / 2;
}
• The word double at the beginning is the return type of
average.
• The identifiers a and b (the function’s parameters)
represent the numbers that will be supplied when
average is called.
4
Computing Averages
• Every function has an executable part, called the body,
which is enclosed in braces.
• The body of average consists of a single return
statement.
• Executing this statement causes the function to “return”
to the place from which it was called; the value of (a +
b) / 2 will be the value returned by the function.
5
Computing Averages
• A function call consists of a function name followed by a
list of arguments.
average(x, y) is a call of the average function.
• Arguments are used to supply information to a function.
The call average(x, y) causes the values of x and y to be copied into
the parameters a and b.
• An argument doesn’t have to be a variable; any
expression of a compatible type will do.
average(5.1, 8.9) and average(x/2, y/3) are legal.
6
Computing Averages
• We’ll put the call of average in the place where we need
to use the return value.
• A statement that prints the average of x and y:
printf("Average: %g\n", average(x, y));
• The return value of average isn’t saved; the program
prints it and then discards it.
• If we had needed the return value later in the program,
we could have captured it in a variable:
avg = average(x, y);
7
Computing Averages
• The average.c program reads three numbers and uses
the average function to compute their averages, one pair
at a time:
Enter three numbers: 3.5 9.6 10.2
Average of 3.5 and 9.6: 6.55
Average of 9.6 and 10.2: 9.9
Average of 3.5 and 10.2: 6.85
8
Computing Averages
/* Computes pairwise averages of three numbers */
#include <stdio.h>
int main(void)
{
double x, y, z;
return 0;
}
9
Program: Printing a Number
• To indicate that a function has no return value, we specify
that its return type is void:
void print_value(int n)
{
printf("The number is %d\n", n);
}
• void is a type with no values.
• A call of print_value must appear in a statement by itself:
print_value(i);
• The myprog.c program calls print_value 10 times inside a
loop.
10
Printing a Number
#include <stdio.h>
void print_value(int n)
{
printf("The number is %d\n", n);
}
int main(void)
{
int i;
return 0;
}
11
Function Definitions
• General form of a function definition:
return-type function-name ( parameters )
{
declarations
statements
}
12
Function Definitions
• The return type of a function is the type of value that the
function returns.
• Rules governing the return type:
Functions may not return arrays.
Specifying that the return type is void indicates that the function
doesn’t return a value.
• If the return type is omitted, the function is presumed to
return a value of type int.
13
Function Definitions
• After the function name comes a list of parameters.
• Each parameter is preceded by a specification of its type;
parameters are separated by commas.
• If the function has no parameters, the word void should
appear between the parentheses.
14
Function Definitions
• The body of a function may include both declarations and
statements.
• An alternative version of the average function:
double average(double a, double b)
{
double sum; /* declaration */
sum = a + b; /* statement */
return sum / 2; /* statement */
}
15
Function Definitions
• Variables declared in the body of a function can’t be
examined or modified by other functions.
16
Function Definitions
• The body of a function whose return type is void (a “void
function”) can be empty:
void print_text(void)
{
}
• Leaving the body empty may make sense as a temporary
step during program development
17
Function Calls
• A function call consists of a function name followed by a
list of arguments, enclosed in parentheses:
average(x, y);
print_value(i);
print_text();
• If the parentheses are missing, the function won’t be
called:
print_text; /*** WRONG ***/
18
Function Calls
• A call of a void function is always followed by a semicolon
to turn it into a statement:
print_count(i);
print_pun();
• A call of a non-void function produces a value that can be
stored in a variable, tested, printed, or used in some
other way:
avg = average(x, y);
if (average(x, y) > 0)
printf("Average is positive\n");
printf("The average is %g\n", average(x, y));
19
Function Calls
• The value returned by a non-void function can always be
discarded if it’s not needed:
average(x, y); /* discards return value */
• This call is an example of an expression statement: a
statement that evaluates an expression but then
discards the result.
20
Function Declarations
• Function declarations of the kind we’re discussing are
known as function prototypes.
• A function prototype doesn’t have to specify the names
of the function’s parameters, as long as their types are
present:
double average(double, double);
• It’s usually best not to omit parameter names.
21
Arguments
• In C, arguments are passed by value: when a function is
called, each argument is evaluated and its value
assigned to the corresponding parameter.
• Since the parameter contains a copy of the argument’s
value, any changes made to the parameter during the
execution of the function don’t affect the argument.
22
Arguments
• The fact that arguments are passed by value has both
advantages and disadvantages.
• Since a parameter can be modified without affecting the
corresponding argument, we can use parameters as
variables within the function, reducing the number of
genuine variables needed.
23
Function Definitions
• Consider the following function, which raises a number x
to a power n:
int power(int x, int n)
{
int i, result = 1;
return result;
}
24
Function Definitions
• Since n is a copy of the original exponent, the function
can safely modify it, removing the need for i:
int power(int x, int n)
{
int result = 1;
return result;
}
25
The return Statement
• A non-void function must use the return statement to
specify what value it will return.
• The return statement has the form
return expression ;
• The expression is often just a constant or variable:
return 0;
return status;
26
The return Statement
• return statements may appear in functions whose return
type is void, provided that no expression is given:
return; /* return in a void function */
• Example:
void print_int(int i)
{
if (i < 0)
return;
printf("%d", i);
}
27
The return Statement
• A return statement may appear at the end of a void
function:
void print_pun(void)
{
printf("To C, or not to C: that is the question.\n");
return; /* OK, but not needed */
}
• Using return here is unnecessary.
• If a non-void function fails to execute a return statement,
the behavior of the program is undefined if it attempts to
use the function’s return value.
28
The return Statement
• Normally, the return type of main is int:
int main(void)
{
…
}
• Older C programs often omit main’s return type, taking
advantage of the fact that it traditionally defaults to int:
main()
{
…
}
29
The return Statement
• The value returned by main is a status code that can be
tested when the program terminates.
• main should return 0 if the program terminates normally.
• To indicate abnormal termination, main should return a
value other than 0.
• It’s good practice to make sure that every C program
returns a status code.
30
End of
Lecture
31