Top Banner
APS105 Functions (and Pointers) 1
44

APS105

Jan 25, 2016

Download

Documents

APS105. Functions (and Pointers). Modularity. Modularity Break a program into manageable parts (modules) Modules interoperate with each other Benefits of modularity:.  modularity/modules in C: functions. Functions. C pre-defined functions. main Math functions Sqrt () , fabs () , etc - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: APS105

1

APS105

Functions (and Pointers)

Page 2: APS105

2

Modularity

• Modularity– Break a program into manageable parts (modules)– Modules interoperate with each other

• Benefits of modularity:

modularity/modules in C: functions

Page 3: APS105

3

Functions

Page 4: APS105

4

C pre-defined functions

• main• Math functions

– Sqrt(), fabs(), etc

• I/O functions– printf() , scanf()

Page 5: APS105

5

Two Types of Functions

• Commands– perform a process, but don’t return anything

• Queries– evaluate something, return a result

• Some languages:– commands: called procedures, subroutines– queries: called functions

• C language:– commands: called functions– queries: called functions

Page 6: APS105

6

Defining a Function

• Define a function that prints a blank line.

• Note: this is called a "function definition"

Page 7: APS105

7

Incorporating a Function in a Program

.

Page 8: APS105

8

Function Prototype

• Sometimes must summarize a function– tell the compiler what it needs to know– eg., if function has parameters or return value

• Function Prototype:– similar to a copy of the first line of the function

definition– Example of a prototype:

.

Page 9: APS105

Prototype Example: Declare main First

.

Page 10: APS105

10

Parameters

Page 11: APS105

11

Functions with Parameters

• Define a function that prints n blank lines.

• Note: n is called a "parameter"

Page 12: APS105

12

Calling a Function with Parameters

.

Page 13: APS105

13

When A Function is Called…

1) argument(s) is/are evaluated

2) control passes to the function

3) parameter(s) is/are assigned value(s) • the values of the argument(s) are copied• i.e., param gets a copy of the value of the arg• this method is called "call-by-value"

• the only method used by C• other languages provide other methods

4) the function is executed

5) control passes back to the calling function

Page 14: APS105

14

Example of A Query Function

• a function that returns the factorial of an int.

Page 15: APS105

15

Function Scope of Identifiers/Variables

• An identifier/variable exists within its scope– if declared within a function

• the scope is within the function

• Implications: for a var declared within a func– can't be accessed outside that function– can re-use the identifier in multiple functions

Page 16: APS105

16

Example of Scope

.

Page 17: APS105

17

Example of Multiple Parameters

.

Page 18: APS105

18

Parameter Type Correspondence

• Param and argument types needn't match– as long as they are "assignment compatible"

• Example:.

Page 19: APS105

19

Returning

Page 20: APS105

20

Multiple Returns

• A function can have multiple return statements• Example: a function that returns max value

.

Page 21: APS105

21

Avoiding Multiple Returns

• Example: a function that returns max value.

Page 22: APS105

22

No Return

• Example: print n blank lines.

Page 23: APS105

23

Style: Returning Bool

• Functions that return bool should be named:is<something>

• Produces more readable code• Example:

.

Page 24: APS105

24

isPerfectSquare

.

Page 25: APS105

25

isPerfectSquare (cont'd)

.

Page 26: APS105

26

isPerfectSquare (cont'd)

.

Page 27: APS105

27

Pointers

Page 28: APS105

28

Swap

• Example: a function to swap two values.

Page 29: APS105

29

Pointers

• Need a way to refer to locations of things– not just a copy of a variable’s value

• a pointer:– points to a variable– eg., like a house address points to a house

• Example:

Page 30: APS105

30

Memory• RAM

– random access memory– composed of memory chips– organized like a giant table of locations

• Addresses– every location in table has an address– just like every house on a street– use the address to read/write a location

• In C– addresses are stored in pointer variables– a pointer is a memory address

Address Value

0 0

1 23

2 0

3 0

… …

232-2 102

232-1 7

Memory

Page 31: APS105

31

Understanding Pointers

• Declaring a pointer variableint *p;

• Declaring and assigningint *p; int x; x = 8; p = &x;

Addr Value

0 0

1 23

2 0

3 0

… …

232-2 102

232-1 7

Memory

.

Page 32: APS105

32

Using Pointers

• Declaring, assigning, and using a pointerint *p;

int x; x = 8; p = &x; printf("%d\n",*p); // print what's at p

.

Page 33: APS105

33

Pointers Example

double x, y;double *p, *q;x = 3.6;y = 6.7;p = &x;q = &y;*p = 1.0;*q = *p + 1.0;

Addr Value

0 0

1 0

2 0

3 0

4 0

5 0

… …

232-2 0

232-1 0

Memory

Page 34: APS105

34

Pointers Example Again

double x, y;double *p, *q;x = 3.6;y = 6.7;p = &x;q = &y;*p = 1.0;*q = *p + 1.0;

x

y

p

q

Page 35: APS105

35

Fixed Swap

• Example: a function to swap two values

.

Page 36: APS105

36

Functions that Return Pointers

• Create a function called largeLoc– Takes addresses of two doubles– Returns the address of the larger value

• Example usage:double x = 2.6;double y = 3.4;double *p;p = largeLoc(&x,&y);.

Page 37: APS105

37

largeLoc

.

Page 38: APS105

38

Scope

Page 39: APS105

39

Scope of Internal Identifiers

• Scope of an identifier:– the range within which it is recognized– an identifier is not recognized outside its scope

• i.e., it cannot be used, or compiler will complain

• Ex: for (int i = 1; ...; ...)– scope of i is within the for loop only

• Scope of a function parameter:– only within the body of the function

• Scope of a variable declared in a function– starts at the point of declaration– ends at the end of the block {} it was declared in– called an internal identifier

Page 40: APS105

40

Scope Example

int foo(int x){ int y=5; for (int i=0;i<10;i++) { int z = 3; y *= z + x; } return y;}

Page 41: APS105

41

Overlapping Scope

int i = 1;printf(“i = %d\n”,i);{ int i = 2; printf(“i = %d\n”,i);}printf(“i = %d\n”,i);

• What is the output?

Page 42: APS105

42

Scope of External Identifiers

• External identifier– one that is declared outside of any function– ex: a function identifier/name is external

• Example:int x; // x is an external identifier

void main()

{

...

}

• If external ident. declared before all func.s:– then it is called global

Page 43: APS105

43

Top-Down Modular Design

• Functions and scope provide modularity– can build them independently and combine

• Modularity eases large programs– break the problem into smaller & smaller parts– until the parts are small and manageable– make these into functions– combine them into the whole

Page 44: APS105

44

Ex: Goldbach’s Conjecture

• Can an even number larger than 2 be expressed as the sum of two primes?.