Académique Documents
Professionnel Documents
Culture Documents
Subprograms
Functions
There are many useful operations which
DELEGATING TASKS
Subprograms
Nathan Friedman
2011
Subroutines
2011
Subprograms
A Factorial Function
int factorial(int n) {
int fact;
fact = 1;
while (n > 0) {
fact = n * fact;
n = n-1;
}
return fact;
2011
Subprograms
2011
A Factorial Function
int factorial(int n) {
int fact;
fact = 1;
while (n > 0) {
fact = n * fact;
n = n-1;
}
return fact;
definition of main
In fact, main is a function which is automatically
invoked when the program is executed
2011
2011
Subprograms
Subprograms
A Factorial Function
int factorial(int n) {
int fact;
fact = 1;
while (n > 0) {
fact = n * fact;
n = n-1;
}
return fact;
computed
Subprograms
2011
Subprograms
2011
int factorial(int n) {
int fact;
fact = 1;
while (n > 0) {
fact = n * fact;
n = n-1;
}
return fact;
define it
This is the name used to refer to the function
elsewhere in the program
2011
2011
10
A Factorial Function
Subprograms
Subprograms
11
Subprograms
12
2011
Subprograms
13
A Factorial Function
2011
Subprograms
14
int factorial(int n) {
int fact;
fact = 1;
while (n > 0) {
fact = n * fact;
n = n-1;
}
return fact;
function
Their names can be the same as names in other parts of
2011
Subprograms
A Factorial Function
int factorial(int n) {
int fact;
fact = 1;
while (n > 0) {
fact = n * fact;
n = n-1;
}
return fact;
}
15
2011
Subprograms
16
function
If the function returns a value, there must be at least one
2011
Subprograms
17
2011
Subprograms
18
Using Functions
as library functions.
the arguments
expression
2011
Subprograms
19
2011
Subprograms
20
in the expression
If the function is of type void, there is no value returned
2011
Subprograms
21
2011
Subprograms
Some Caveats
Using a Function
#include <stdio.h>
int factorial(int n) {
int fact;
fact = 1;
while (n > 0) {
fact = n * fact;
n = n-1;
}
return fact;
equal.
The type of the corresponding arguments and
22
void main() {
int n;
scanf("%d",&n);
printf ("The factorial of %d = %d\n", n, factorial(n));
}
2011
Subprograms
23
2011
Subprograms
24
Call by Value
2011
Subprograms
25
2011
Subprograms
A Simple Example
#include <stdio.h>
26
2011
Subprograms
27
2011
Subprograms
28
Prototypes
program
It is possible to link definitions from different files but we
will not look at that in this course
C requires that every name be defined before it is used in
the program
This is done in order to make it easier to check that the
name is being used properly
Function definitions must appear before the function is
used
2011
Subprograms
29
2011
Function Prototypes
Example
#include <stdio.h>
2011
2011
Subprograms
31
Subprograms
30
Subprograms
32
Prime Numbers
#include <stdio.h>
#include <math.h>
2011
Subprograms
33
2011
Subprograms
34
2 is a prime number
If we could determine whether a given number is
Subprograms
range
2011
be filled in later
We want to list all primes in a given range
35
number is a prime
Leaving these two issues aside for now, we can develop
2011
Subprograms
36
Main Program
void main() {
int range, count=1, number;
range = get_range();
printf ("Prime number #%d = 2\n", count);
number = 3;
while (number <= range) {
if (prime(number)) {
count = count + 1;
printf ("Prime number #%d = %d\n",
count, number);
}
number = number + 2;
}
printf ("There are %d prime numbers less than %d\n",
count, range);
pieces
The first function is get_range, which has no arguments
2011
Subprograms
37
2011
Subprograms
38
Is a number N prime?
If N=2 it is prime.
If N>2 check whether it has any proper divisors
If it is even it has the divisor 2
If it is odd we use a loop to check potential divisors:Look
for divisors less than N, where M>2
We need a loop that checks goes the potential divisors
do {
scanf ("%d",&n);
if (n >= 2) break;
printf ("Please a number >= 2.\n");
printf ("You entered %d. Please try again.\n", n);
}
while (1);
return n;
2011
Subprograms
39
2011
Subprograms
#include <stdio.h>
int get_range ();
int prime (int);
40
void main() {
int range, count=1, number;
range = get_range();
printf ("Prime number #%d = 2\n", count);
number = 3;
while (number <= range) {
if (prime(number)) {
count = count + 1;
printf ("Prime number #%d = %d\n", count, number);
}
number = number + 2;
}
printf ("There are %d prime numbers less than %d\n",
count, range);
}
10
2011
Subprograms
41
2011
Subprograms
42
11