You are on page 1of 32

SCC 110

Software Development
Nigel Davies and Adrian Friday
First test in the lab
** next week **
We have been
encouraging you to
divide problems up into
smaller steps
Decomposing problems
• We solve pretty much all problems by
repeatedly decomposing it into a series
of smaller steps until the problem is
tractable.

refinement
Decomposing problems
• Most programming languages give us
some support for this:
• C lets us define functions which
package and name specific functionality
• And libraries – sets of pre-compiled
functions we can call from our
programs
Functions
• Help us avoid repetition by ‘factoring out’ common elements
we can ‘reuse’
• Create ‘reusable units’ of code (we can package into libraries
others can use)
• Separate out well defined tasks into logical reusable blocks,
improving readability and maintenance
• Examples include:
• getting user input (scanf)
• printing to the screen (printf)
• comparing two strings
In C
Function definitions have the form:
•direct-declarator ( parameter-
type-list )
• parameter-type-list could be empty (no
parameters)
• A function may return a simple arithmetic type
(e.g. an int), but not arrays or other functions
• They are followed by a compound-statement,
i.e. {…}, the function body, as with main()
Program flow
Functions change the order of execution
Functions change the ‘flow’
get_user_input

display_some_
output

main
❷ int timesbytwo(int number)
{
return number * 2;
}

❶ int main()
{
int value = 4, result;
result ❸
= timesbytwo(value);
printf("Twice value is %d\n", result);
}
A function call in more detail
This is the
type that is
returned
int timesbytwo(int number) this is the
{
return number * 2; formal
}
argument
int main()
Note! { (note the
int value = 4, result;
result = timesbytwo(value); ‘int’)
printf("Twice value is %d\n", result);
}

and this is a actual


argument or
parameter (any valid
expression involving
values or variables)
voidreturn
void for the here means no arguments
type means nothing istoreturned
the function

void sayhello(void)
{
int main()
printf("hello.\n");
{
} sayhello();
}

In C, functions may have ‘side effects’, such as printing so


the return value isn’t always needed !
Data flow
We pass data in using arguments, and out using a return value
A variable’s ‘scope’ is the {…} block
where declared
Storage allocated here
int main()
{
int numberOfGuesses = 10; Index allocated here
Index
int guesses[numberOfGuesses];
‘exists’ in
Function for (int index = 0; index < numberOfGuesses; index++) { the for()
scope printf("Please enter guess %d:\n", index + 1);
scanf("%d", &guesses[index]); only!
}
Index de-allocated here
printf("Index = %d\n", index); // ERROR, index out of scope
} numberOfGuesses
and guesses de-
14 allocated here
int timesbythree(int number)
{
return number * 3;
}

the argument number is *local* to the function !!!

The argument ‘number’ is a copy of whatever value or


variable is passed in when the function is called
We can modify the copy, but changes are local

void changescorebyone(int score)


{
score++;
}

this is called ‘pass by value’:


score is a variable ‘local to’ the function that is an
‘independent copy’ of that passed in
void changescorebyone(int score)
{
score++; score 01
}

int main()
{
int x = 0;
changescorebyone(x); x 0
printf("x = %d\n", x);
}
x=0
void changescorebyone(int score)
0
{
1
score++;
}

int main()
{
int score = 0; 0
changescorebyone(score);
printf("%d\n", score);
}
i.e. the parameter
values in the function
are only ever a copy
(pass by value)!
Nothing links the variables inside the function scope,
including the parameters, with those outside
And now for some
work for you :)
You and the person
next to you need to
build a calculator
One person writes the
functions, one person
writes the main
program that calls the
functions
You need functions to
add, subtract, multiply
and divide numbers
go :)
Review of caller/ callee
solutions
Important gotchas
Where to declare
// Prototype for changescorebyone

• Functions should always be


void changescorebyone(int);

int main()
declared before you call them {
int score = 0;
• Or, a ‘function prototype’, // Function call
changescorebyone(score);
needs to appear before you }
printf("%d\n", score);

call it
// Declaration, note types must match

• This is what header files, e.g. void changescorebyone(int score)


{
<stdio.h>, do }
score++;
Passing arrays is special

Only the location of the array is ‘passed by value’, so


when you modify the contents of the array, that’s not
local to the function
void ChangeArrayByOne(int array[])
{ array
array[0]++;
}

int main() myArray


{
int myArray[2] = {1,2}; 21 2
ChangeArrayByOne(myArray);
printf(“%d\n”, myArray[0]);
}
int main()

one function has a special meaning !


int main(int argc, char *argv[])
{
return 0;
}

The ‘full version’ of ‘main’ has arguments and returns a


status code (0 == success!)
Summary
• The idea of decomposing programs into
functions
• How functions are declared and called in C
• The ‘scope’ of a variable
• ‘Pass by value’, i.e. how function
parameters are local to the function (need
pointers!)

You might also like