Functions. Functions. Design and Abstraction. Design and Abstraction

Functions Functions Here is a simple program with two functions: Have you seen C functions before? Yes! We’ve been working with the main function fr...
Author: David Moody
6 downloads 0 Views 208KB Size
Functions

Functions Here is a simple program with two functions:

Have you seen C functions before? Yes! We’ve been working with the main function from the very beginning, as well as, printf, scanf, etc., which are also functions. Definition A function is relatively independent block of code, within a larger program, that performs a particular task. Functions allow us to: better structure our programs easily modify and extend programs easily test for and isolate bugs minimise code duplication and maximise reuse

Andrew Taylor

COMP1911 Computing 1A

Design and Abstraction

#i n c l u d e < s t d i o . h> i n t max ( i n t n , i n t m) ;

// f u n c t i o n p r o t o t y p e

i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) { p r i n t f ( ” The l a r g e r o f %d and %d i s %d . \ n ” , 3 , 4 , max ( 3 , 4 ) ) ; r e t u r n 0 ; // ˆˆˆˆˆˆˆˆˆ f u n c t i o n c a l l } i n t max ( i n t n , i n t m) { i n t maximum ; i f ( n > m) { maximum = n ; } else { maximum = m; } r e t u r n maximum ; } Andrew Taylor

// f u n c t i o n d e f i n i t i o n

// // // //

this simple function r e t u r n s the value of the l a r g e s t of i t s argu ments , n and m

COMP1911 Computing 1A

Design and Abstraction

Functions are powerful structuring and abstracting tool in programming languages.

We often use the top-down approach for problem solving, stopping when the level of detail roughly corresponds to individual functions.

What is abstraction? The process of creating self-contained units of software that allow the solutions to tasks to be parametrised, and hence made general purpose. [Moffat, 2003]

Remember Each function should perform a single logical task!

The idea is that we can break down our program into tasks and then reason about the interaction between the tasks while abstracting/hiding their exact implementation details. Top-Down Design A design approach that starts with the high-level task and then progressively breaks it down into smaller and smaller sub-tasks until the desired level of detail is achieved. Andrew Taylor

COMP1911 Computing 1A

Problem Let’s revisit a simple problem and apply top-down design and abstraction: Read two integers and print out the smallest. What are the logical tasks? read an integer (×2) find the smaller of the two print out the result

Andrew Taylor

COMP1911 Computing 1A

Design and Abstraction

Well, let’s write our program by using these functions! #i n c l u d e < s t d i o . h>

What is the abstraction? We assume that we have some parts already and abstract over their implementation, in particular: read an integer find the minimum of two integers

i n t r e a d I n t ( v o i d ) ; // f u n c t i o n p r o t o t y p e s i n t m i n I n t ( i n t n1 , i n t n2 ) ; i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) { i n t n , m, min ;

We already abstract over the details of printing by using printf.

p r i n t f ( ” E n t e r two i n t e g e r s : ” ) ;

So let’s assume we have this function for reading an integer:

// u s e f u n c t i o n t o r e a d numbers ! n = r e a d I n t ( ) ; // c o l l e c t r e t u r n v a l u e m = r e a d I n t ( ) ; // i n n & m

int readInt ( void ) ;

And this function for finding the minimum of two integers:

// u s e f u n c t i o n t o f i n d minimum min = m i n I n t ( n , m) ; // p a s s n & m a s a r g s p r i n t f ( ” The s m a l l e r number i s %d . \ n ” , min ) ;

i n t m i n I n t ( i n t n1 , i n t n2 ) ;

So how do we use this? return 0; } Andrew Taylor

Andrew Taylor

COMP1911 Computing 1A

The Type of Functions

COMP1911 Computing 1A

The Type of Functions When defining a function, you have control over: the number, order and type of arguments; and

Arguments and Return Value Functions are parametrised via zero or more arguments and they may (optionally) return a single value. Consider: i n t m i n I n t ( i n t n1 , i n t n2 ) ; This function has:

type of the return argument (if any) Example: double double void int void

foo1 ( i n t i , char s [ ] , double d ) ; f o o 2 ( f l o a t f , d o u b l e dd [ ] , i n t i i [ ] ) ; f o o 3 ( c h a r s 1 [ ] , c h a r s 2 [ ] [ SIZE ] ) ; foo4 ( void ) ; foo5 ( void ) ;

two arguments (n1 and n2), both of type int The void Type The special type void is used to indicate that a function takes no arguments and/or returns no value.

a return value of type int

Andrew Taylor

COMP1911 Computing 1A

Andrew Taylor

COMP1911 Computing 1A

Function Prototypes

Function Calls

Definition A function prototype provides the compiler with the type information for a particular function. Function Prototypes are needed because the compiler processes the program code sequentially. It lets the compiler know that the implementation for these functions can be found somewhere else. A compile error occurs if a function call is encountered before it is defined.

Execution Flow The code of a function is only executed when requested via a function call. Current code execution is halted; Execution of the function body begins; When the function completes, execution resumes at a point after the function call.

Function prototypes provide the compiler with type information about our functions you want to use. Type Mismatch The types of arguments and return value must match the types expected by the actual function! Andrew Taylor

COMP1911 Computing 1A

Example

Andrew Taylor

COMP1911 Computing 1A

Example Let’s now write our two functions: i n t r e a d I n t ( v o i d ) { // t h i s must match p r o t o t y p e int n;

Here is the function call for minInt:

s c a n f (”%d ” , &n ) ; //

Suggest Documents