+ All Categories
Home > Documents > Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System...

Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System...

Date post: 15-Jan-2016
Category:
View: 228 times
Download: 1 times
Share this document with a friend
Popular Tags:
31
Introduction to F unctions CS-2301 B-term 200 8 1 CS-2301 B-term 2008 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language, 2 nd ed., by Kernighan and Ritchie and from C: How to Program, 5 th ed., by Deitel and Deitel)
Transcript
Page 1: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 1CS-2301 B-term 2008 1

Introduction to Functions

CS-2301, System Programming for Non-majors

(Slides include materials from The C Programming Language, 2nd ed., by Kernighan and Ritchie and from C: How to Program, 5th ed., by Deitel and Deitel)

Page 2: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 2CS-2301 B-term 2008 2

Definition – Function

• A fragment of code that accepts zero or more argument values and produces a result value and zero or more side effects.

• A method of encapsulating a subset of a program or a system

• To hide details

• To be invoked from multiple places

• To share with others

Page 3: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 3CS-2301 B-term 2008 3

Functions – a big Topic

• Examples

• Function definition

• Function prototypes & Header files

• Pre- and post-conditions

• Scope and storage class

• Implementation of functions

• Recursive functions

Page 4: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 4CS-2301 B-term 2008 4

Common Functions

#include <math.h>– sin(x) // radians– cos(x) // radians– tan(x) // radians– atan(x) – atan2(y,x)– exp(x) // ex

– log(x) // loge x

– log10(x) // log10 x

– sqrt(x) // x 0– pow(x, y) // xy

– ...

#include <stdio.h>– printf()– fprintf()– scanf()– sscanf()– ...

#include <string.h>– strcpy()– strcat()– strcmp()– strlen()– ...

Page 5: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 5CS-2301 B-term 2008 5

Common Functions (continued)

• In Kernighan & Ritchie– <assert.h> // for diagnostics, loop invariants,

etc.– <stdarg.h> // for parsing arguments– <time.h> // time of day and elapsed time– <limits.h> // implementation dependent

numbers– <float.h> // implementation dependent numbers.

– <setjmp.h> // beyond scope of this course– <signal.h> // beyond scope of this course

Page 6: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 6CS-2301 B-term 2008 6

Common Functions (continued)

• See also the man pages of your system for things like

•<pthread.h> // concurrent execution•<socket.h> // network communications•... // many, many other facilities

• Fundamental Rule: if there is a chance that someone else had same problem as you, …

• … there is probably a package of functions to solve it!

Page 7: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 7CS-2301 B-term 2008 7

Functions in C

resultType functionName(argType1 arg1, argType2 arg2, …) {

body

}

• If no result, resultType should be void• Warning if not!

• If no arguments, use void between ()

Page 8: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 8CS-2301 B-term 2008 8

Functions in C

resultType functionName(argType1 arg1, argType2 arg2, …) {

body

} // functionName

• If no result, resultType should be void• Warning if not!

• If no arguments, use void between ()

It is good style to always

end a function with a

comment showing its name

Page 9: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 9CS-2301 B-term 2008 9

Function Definition

• Every function definition has the formreturn-type function-name (argument declarations) {

declarations and statements

}

• See top of page 70 in Kernighan & Ritchie

Page 10: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 10CS-2301 B-term 2008 10

Function Definition

• Every function definition has the formreturn-type function-name (argument declarations) {

declarations and statements

}

• See top of page 70 in Kernighan & Ritchie

• For practical purposes, code between {} (inclusive) is a compound statement

Page 11: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 11

Note

• Functions in C do not allow other functions to be declared within them

• Like C++, Java• Unlike Algol, Pascal

• All functions defined at “top level” of C programs

• (Usually) visible to linker• Can be linked by any other program that knows the

function prototype

Page 12: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 12CS-2301 B-term 2008 12

Examples

• double sin(double radians) {…

} // sin

• unsigned int strlen (char *s) {…

} // strlen

Page 13: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 13CS-2301 B-term 2008 13

Note on printf, etc.

• int printf(char *s, ...) {body

} // printf

• In this function header, “…” is not a professor’s place-holder

• (as often used in these slides)

• …but an actual sequence of three dots (no spaces between)

• Meaning:– the number and types of arguments is indeterminate

• Use <stdarg.h> to extract the arguments

Page 14: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 14CS-2301 B-term 2008 14

Questions?

Page 15: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 15CS-2301 B-term 2008 15

Function Prototypes

• There are many, many situations in which a function must be used separate from where it is defined –

• before its definition in the same C program

• In one or more completely separate C programs

• This is actually the normal case!

• Therefore, we need some way to declare a function separate from defining its body.

• Called a Function Prototype

Page 16: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 16CS-2301 B-term 2008 16

Function Prototypes (continued)

• Definition:– a Function Prototype is a language construct in C with the form:–

return-type function-name (argument declarations) ;

Page 17: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 17CS-2301 B-term 2008 17

Function Prototypes (continued)

• Definition:– a Function Prototype is a language construct in C with the form:–

return-type function-name (argument declarations) ;

• I.e., exactly like a function definition, except with a “;” instead of a body in curly brackets

Page 18: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 18CS-2301 B-term 2008 18

Purposes of Function Prototype

• So compiler knows how to compile calls to that function, i.e.,– number and types of arguments– type of result

• As part of a “contract” between developer and programmer who uses the function

• As part of hiding details of how it works and exposing what it does.

• A function serves as a “black box.”

Page 19: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 19

Header files

• In applications with multiple C programs, function prototypes are typically in header files

• I.e., the ‘.h’ files that programmers include in their code

• Grouped by related functions and features• To make it easier for developers to understand

• To make it easier for team development

• To make a package that can be used by someone else

Page 20: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 20

#include

• #include <foo.h>• Search the system’s directories in order for a file of

the name foo.h• Directories can be added with ‘-I’ switch to gcc

command– E.g., gcc –I myProject/include foo.c– Precedes system directories in search order

• #include “foo.h”• Search the directory where the source program is

found first, before -I and system directories

Page 21: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 21

Typical C Programming Style

• A lot of small C programs, rather than a few large ones

• Header files to tie them together

• Makefiles to build or rebuild them in an organized way

• Later in the term

Page 22: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 22

Definition – Stub

• A stub is a dummy implementation of a function with an empty body

• A placeholder while building a program• So that it compiles correctly

• Fill in one-stub at a time• Compile and test if possible

Page 23: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 23

Questions?

Page 24: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 24CS-2301 B-term 2008 24

“Contract” between Developer and User of a Function

1. Function Prototype

2. The pre- and post-conditions– I.e., assertions about what is true before the

function is called and what is true after it returns.

– A logical way of explaining what the function does

Page 25: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 25Introduction to FunctionsCS-2301 B-term 2008 25

Definitions

• Pre-condition:–a characterization or logical statement about

• the values of the arguments, and • values of relevant variables outside the function

prior to calling the function

• Post-condition:–a logical statement or characterization about

• the result of the function in relation to the values of the arguments and pre-conditions, and

• changes to values of variables outside the function

after the function returns

Page 26: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 26Introduction to FunctionsCS-2301 B-term 2008 26

Example 1

• double sin (double angle);– Pre:– angle is expressed in radians– Post:– result is the familiar sine of angle– Note: this function does not use or change any

other variables

Page 27: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 27Introduction to FunctionsCS-2301 B-term 2008 27

Example 2

• int printf (string, arg1, arg2, …)– Pre:– string terminated with ‘\0’ and containing

conversion codes

– Pre:– a buffer maintained by the file system contains zero or more unprinted characters from previous calls.

– Post:– args are substituted for conversion codes in copy of string; resulting string is added to buffer

– Post:– if ‘\n’ is anywhere in buffer, line is “printed” up to ‘\n’; printed characters are cleared from buffer

– Post:– result is number of characters added to buffer by printf

Page 28: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 28Introduction to FunctionsCS-2301 B-term 2008 28

Example 3

float total = 0;int count = 0;

int GetNewItem(void) {float input;int rc;printf(“Enter next item:- ”);if ((rc = scanf(“%f”, &input)) != EOF

&& (rc > 0)) {total += input;count++;

}; // ifreturn rc;

} // GetNewItem

Page 29: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 29Introduction to FunctionsCS-2301 B-term 2008 29

Example 3

float total = 0;int count = 0;

int GetItem(void) {float input;int rc;...;if ((rc = scanf(“%f”, &input)) != EOF

&& (rc > 0)) {total += input;count++;

}; // ifreturn rc;

} // GetItem

Pre:– total is sum of all previous inputs, or zero if none

Pre:– count is number of previous inputs, or zero if none

Post:– if valid input is received total = totalprev + input, count = countprev + 1

Page 30: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 30Introduction to FunctionsCS-2301 B-term 2008 30

Important

• Pre- and post-conditions are analogous to loop invariants

• I.e., they describe something about the data before and after a function is called and the relationship that the function preserves

• Often are used together with loop invariants• … to show that loop invariant is preserved from one

iteration to the next

Page 31: Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Introduction to FunctionsCS-2301 B-term 2008 31Introduction to FunctionsCS-2301 B-term 2008 31

Questions?


Recommended