Title: SOFTWARE DEVELOPMENT IN C CM9041
1SOFTWARE DEVELOPMENT IN CCM904-1
- Lecture 6
- Functions, Parameters and Arguments
2Structure of a C Program
- The Function main
- In C, all code is packaged in functions
- main is the function that the system calls when
you run your program - Every program must have one and only one main
- All functions may return a value
Function 1
Function 2
Main statement Call function1 statement statement
Call function 2 return
3Prototypes
- Prototypes allow
- Description of all functions to be in one place
- Main program containing overall structure comes
first - Maps to functional decomposition/top down
development - Speeds up compilation process
4Structure of a C Program
Prototype Function1
Prototype Function2
Main statement Call function1 statement statement
Call function 2 return
Function 1
Function 2
5Function Prototypes
Function prototypes allow the compiler to check
that the data-types specified in function
declarations and invocations are coded
consistently. Eg. int power(int,
int) main() int x, y, z float a, b, c c
power(a, b) z power(a, b) z power(x, y)
/ end main / / An exponentiation function
/ int power(int b, int e) ... return ans /
end power /
6Function Components
- A function definition consists of two
- parts
- A Specification ( or header or interface)
Specifies the function name, the return value and
argument list - A Body ( or block or implementation) Code to be
executed when the function is called - A sequence of statements enclosed in braces
7Format
- return-type name (parameter list)
- Prototype
- int multiply(int,int)
- Function header
- int multiply(int num1, int num2)
- Usage
- total multiply(n1,n2)
- (where total,n1 and n2 are declared as integer)
8Parameters and arguments
- Example a function to add up two values and
return the sum - include ltstdio.hgt
- int mult(int,int) / prototype/
- int main(void)
-
- int total,n13,n25
-
- total mult(n1,n2) /function called
returns total/ - printf(The total is d,total)
- return 0
-
- int mult(int num1,int num2)
-
- return(num1num2)
arguments
parameters
9Example invocations
x fname(arg1, arg2) fname(arg1, arg2) x
fname() When functions are used well it is
possible ignore exactly how a task is done since
knowing what the function does is
sufficient. Functions can allow code to be
reused making programs more robust reducing
coding errors and simplifying program
maintenance. Function prototypes can also be used
to improve the compile-time elimination of errors.
10Functions Scope
The scope of variables when using functions is
one of the most important considerations in
program design. / Global Variables / int
z main() int x 10 z noddy(x) z11,
x10 z bigears(x) z32, x10 / end main
/ / function noddy / int noddy(int b) b10
return b retval is 11 / end noddy / /
function bigears / int bigears(int a) a10 a
za a21, z11 return za retval is
32 / end bigears /
11Memory Addresses
00000000
z
00000000
00000000
x
00001010
00000000
b
00001010
00000000
a
00000000
12Passing by value
Passing by value The actual values passed as
arguments to functions are a copy of the the
value of the argument at the point of
invocation. Independence This is generally a
desirable feature since it allows the programmer
coding each function to work completely
independently the code found elsewhere in the
same program. (The only restriction being the
functions return type and the types and order of
its arguments.) Complication But the
independence obtained from passing by value in
this way can also cause problems. Eg suppose the
writer of function bigears really intended to
update the value of x in the programs main
function? Solution make x a global variable
similar to A ?
13Global variablesmisuse/abuse
Cross-out the last line of the previous slide ! !
! DO IT NOW ! ! ! The global-isation of
otherwise trivial program variables is a recipe
for disaster! Why not make every variable a
global variable? Compromising functional
independence is the reason why only important and
widely used program variables and data structures
should ever be declared globally - and only then
in a carefully planned managed way! A better
solution pass the arguments you want the
function to update by reference rather than by
value.
14Passing by reference
This modified noddy bigears program has a
different effect on the value of x in main. /
Global Variables / int z main() int x
10 z noddy(x) z11, x10 z
bigears(x) z32, x21 / end main / /
function noddy / int noddy(int b) b10 return
b retval is 11 / end noddy / /
function bigears / int bigears(int a) a10
a z a a21, z11 return z
a retval is 32 / end bigears /
15Memory Addresses
00000000
z
00000000
00000000
x
00001010
00000000
b
00001010
f7fffbf9
a
16Pointers
- What is a C pointer?
- Every variable in a C program (whatever its type
int, float, double, char, array or structure)
occupies an area of storage (or computer memory)
reserved for it at compile-time. - For the primitive data types the exact amount of
storage used per variable is platform dependent
and not specified in the C language . - But C compilers always know exactly how much
storage to reserve for each primitive data type,
on a particular machine (lecture1 sizeof an int
was 4 bytes ...). - The variable declaration statement is the point
at which the compiler reserves the storage and
the address of the variable (relative to the
start of the compiled program) is fixed at that
time. - C pointers access the contents of variables via
their memory location (or address) rather than by
name!
17Pointer Creation
- Pointers have to be declared, just like other
variables - int j, k, ip
- float g, h, fp
- double m, n, dp
- char a, b, cp
- struct CustRec Investor,
- Borrower, CustRecp
- Pointer variable names prefixed by when
declared - Good idea to give some indication that the
variable is a pointer when choosing a name - but
not obligatory. - Declaration of each pointer reserves an area of
storage for holding an address this address will
be the reference that the pointer holds to its
target object. - Each pointer variable carries info about the size
(in bytes) of the type of object to which it can
refer. (Each pointer variable knows the value
returned by sizeof for an object of its target
data-type.)
18Pointer Operations
The address stored as the pointers reference can
be updated using the unary operator eg int
x, y, ipA ipB x 15 ipA x x
gives ?!? ipB ipA ipBs reference is
?!? where ?!? is the meaningless
number which corresponds to the address of
integer variable x. ipA and ipB both now point to
x. Once the pointers reference has been updated
the pointer can be used to manipulate the target
address location using the unary dereferencing
operator eg y ipA y holds 15
19Pointer Operations
A dereferenced pointer can be used anywhere that
the target object could be used printf(d,
ip) gives gt?!? whereas printf(d,
ip) gives gt15 Pointer variables can be
subjected to pointer arithmetic eg int n10,
ip char g10, cp ip n0 cp
g0 ip ip holds the address of n1 cp
cp holds the address of g1 Note, pointer
arithmetic is consistent with the pointer
variables type so ip advances 4 bytes with
every ip whereas cp advances only one byte for
every cp .
20Recap
- Functions separate program into task-specific
chunks - Prototypes aid compilation and readability
- Return value updates linked variable in calling
function - Variables passed by value are copied into local
variables - Passing by reference allows linked variables to
be updated - Uses pointers
- Pointers point to the address of another variable