DAY22
Monday, 02 July 2012
Today we might talk about:
-
what a C function is; how it is declared, and how it is used.
-
input arguments and output values; local variables; scope of variable names;
-
global variables;
-
details of how an input argument is used to transfer information
from the caller to the function;
-
passing an array to a function;
-
a puzzle: why array arguments
can be changed but scalar arguments cannot be.
-
passing arrays (vector or a matrix) whose size is not a constant;
-
static and automatic variables.
-
global variables;
Programs and functions we might talk about:
-
factorial.c,
a function which computes the factorial of an
integer n, returning the value as a double.
-
leap_year.c,
a program which determines if a given year is a leap year.
-
month_length.c,
a program which prints the number of days in a month (ignoring leap year).
We want to convert this to a function, which is given the month and returns
the number of days. We want to include the information from the leap_year.c
program so that month_length() can now take account of leap years as well.
-
p8.4.c, a program which calls a function
to compute triangular numbers;
-
prime_check.c prints a message about whether
the input number is a prime or not. We can pass in an integer constant,
variable, or array entry. We also check what happens if we send in bad input.
-
puzzle.c, tries to sort 3 integers by calling
a function. They are sorted inside the function, but when the function
returns, they aren't sorted. What's going on? If we pass an array instead
of three scalar values, the sorting "sticks". The explanation has to do
with the difference in how scalar and array input arguments are handled.
If scalar arguments really do need to be modified, they have to be passed
as pointers (which we haven't covered yet).
-
remember.c, shows how a function can
"remember" one variable by declaring it "static". In that case,
the variable's value is not destroyed between calls.
-
vector_functions.c, defines some
functions for the dot product, norm, and angle between two vectors.
The angle function calls the dot and norm functions.
Last revised on 02 July 2012.