Beginning C Programming for Engineers Lecture 4: Functions R. - - PowerPoint PPT Presentation

beginning c programming for engineers
SMART_READER_LITE
LIVE PREVIEW

Beginning C Programming for Engineers Lecture 4: Functions R. - - PowerPoint PPT Presentation

Beginning C Programming for Engineers Lecture 4: Functions R. Lindsay Todd Functions p. 1/19 C Preprocessor The C Preprocessor includes header files like stdio.h , expands macros like RAND_MAX , and handles conditional compilation .


slide-1
SLIDE 1

Beginning C Programming for Engineers

Lecture 4: Functions

  • R. Lindsay Todd

Functions – p. 1/19

slide-2
SLIDE 2

C Preprocessor

The C Preprocessor includes header files like stdio.h, expands macros like RAND_MAX, and handles conditional compilation. prog.c preprocessor compiler prog

#include <math.h> #ifndef PI #define PI 3.1415926536 #endif

Functions – p. 2/19

slide-3
SLIDE 3

Functions

Functions are “subprograms” with parameters, program statements, and a result. They are used to: Break a program into simpler pieces. Consolidate similar code used in several places in a program. Reuse the same code in more than one program. (Groups of reusable functions are often organized as libraries.) Link to code written in other programming languages.

Functions – p. 3/19

slide-4
SLIDE 4

Prototypes and Definitions

1

#include <stdio.h>

2 3

/* Prototype */

4

float Square(float x);

5 6

/* Main function */

7

int

8

main()

9

{

10

printf("%g, %g, %g\n",

11

Square(1.), Square(2.), Square(3.));

12

return 0;

13

}

14 15

/* The function */

16

float

17

Square(float x)

18

{

19

return x*x;

20

}

A prototype tells the compiler that there is a function with a specified name, return type, and parameters. The function definition is like the prototype, but also includes the body of the function. 1, 4, 9

Functions – p. 4/19

slide-5
SLIDE 5

Function: Radian

1

#include <stdio.h>

2

#include <math.h>

3 4

#ifndef PI

5

#define PI 3.14159

6

#endif

7 8

/* Prototype */

9

float Radian(float x);

10 11

/* Main function */

12

int main() {

13

float ang, adj, opp;

14

printf("Enter angle (degrees): ");

15

scanf("%g", &ang);

16

printf("Enter adjacent side: ");

17

scanf("%g", &adj);

18

  • pp = adj * tan(Radian(ang));

19

printf("Opposite side: %g\n", opp);

20

return 0;

21

}

22 23

/* The function */

24

float

25

Radian(float deg)

26

{

27

float result;

28

result = deg/180.0 * PI;

29

return result;

30

}

Enter angle (degrees): 45 Enter adjacent side: 60 Opposite side: 59.9999 Enter angle (degrees): 60 Enter adjacent side: 10 Opposite side: 17.3205

Functions – p. 5/19

slide-6
SLIDE 6

Modularization

1

#include <stdio.h>

2

#include <stdlib.h>

3

#include <time.h>

4 5

int RandomInt(int m);

6

int SquareInt(int x);

7 8 9

int main()

10

{

11

int rs;

12

srand(time(NULL));

13

rs = SquareInt( RandomInt(10) );

14

printf("Random square: %d\n", rs);

15

return 0;

16

}

17 18

int RandomInt(int m)

19

{

20

return 1 + rand() % m;

21

}

22 23

int SquareInt(int x)

24

{

25

return x*x;

26

}

Break a program into logically simpler components. Program each of those components. Combine components into

  • ne program.

Random square: 81 Random square: 49

Functions – p. 6/19

slide-7
SLIDE 7

Flowchart: Summing Primes

Get limit sum = 0 n = 2 ++n Print sum n <= limit? Is n prime? sum += n

No Yes No Yes

i = 2 ++i i < n n % i == 0 return 0 return 1

Yes No No Yes

Functions – p. 7/19

slide-8
SLIDE 8

Summing Primes

1

#include <stdio.h>

2 3

int isPrime(int n);

4 5

int main() {

6

int n, limit, sum = 0;

7

printf("Enter limit: ");

8

scanf("%d", &limit);

9

for (n = 2; n <= limit; ++n) {

10

if (isPrime(n)) {

11

sum += n;

12

}

13

}

14

printf("Sum of primes <= %d is %d\n",

15

limit, sum);

16

return 0;

17

}

1

/* Return 1 if prime, 0 otherwise. */

2 3

int isPrime(int n) {

4

int i;

5

for (i = 2; i < n; ++i) {

6

if (n % i == 0) {

7

return 0;

8

}

9

}

10

return 1;

11

}

Enter limit: 1000 Sum of primes <= 1000 is 76127

Functions – p. 8/19

slide-9
SLIDE 9

Comparing Summing Programs

1

/* Sum up primes. */

2 3

#include <stdio.h>

4 5

int main()

6

{

7

int i, n, limit, sum = 0;

8

printf("Enter limit: ");

9

scanf("%d", &limit);

10

for (n = 2; n <= limit; ++n) {

11

for (i = 2; i < n; ++i) {

12

if (n % i == 0) break;

13

}

14

if (i == n) sum += n;

15

}

16

printf("Sum of primes <= %d is %d\n",

17

limit, sum);

18

return 0;

19

}

1

#include <stdio.h>

2 3

int isPrime(int n);

4 5

int main() {

6

int n, limit, sum = 0;

7

printf("Enter limit: ");

8

scanf("%d", &limit);

9

for (n = 2; n <= limit; ++n) {

10

if (isPrime(n)) {

11

sum += n;

12

}

13

}

14

printf("Sum of primes <= %d is %d\n",

15

limit, sum);

16

return 0;

17

}

1

/* Return 1 if prime, 0 otherwise. */

2 3

int isPrime(int n) {

4

int i;

5

for (i = 2; i < n; ++i) {

6

if (n % i == 0) {

7

return 0;

8

}

9

}

10

return 1;

11

}

Functions – p. 9/19

slide-10
SLIDE 10

Primes and Reverses

Get limit n = 2 ++n n <= limit? Is n prime? r = reverse(n) Is r prime? Print n, r

Yes No Yes No Yes No

1

/* Test n, reverse(n) for primeness. */

2

#include <stdio.h>

3

int isPrime(int n);

4

int reverse(int n);

5 6

int main()

7

{

8

int limit, n, r;

9

printf("Enter limit: ");

10

scanf("%d", &limit);

11

for (n = 2; n <= limit; ++n) {

12

if (!isPrime(n)) continue;

13

r = reverse(n);

14

if (!isPrime(r)) continue;

15

printf("%d and %d are prime.\n", n, r);

16

}

17

return 0;

18

}

Functions – p. 10/19

slide-11
SLIDE 11

Comparing Reverse Prime

1

/* Test n, reverse(n) for primeness. */

2

#include <stdio.h>

3

int reverse(int n);

4 5

int main()

6

{

7

int i, limit, n, r;

8

printf("Enter limit: ");

9

scanf("%d", &limit);

10

for (n = 2; n <= limit; ++n) {

11

for (i = 2; i < n; ++i) {

12

if (n % i == 0) break;

13

}

14

if (i != n) continue; /* Not prime */

15

r = reverse(n);

16

for (i = 2; i < r; ++i) {

17

if (r % i == 0) break;

18

}

19

if (i != r) continue; /* Not prime */

20

printf("%d and %d are prime.\n", n, r);

21

}

22

return 0;

23

}

1

/* Test n, reverse(n) for primeness. */

2

#include <stdio.h>

3

int isPrime(int n);

4

int reverse(int n);

5 6

int main()

7

{

8

int limit, n, r;

9

printf("Enter limit: ");

10

scanf("%d", &limit);

11

for (n = 2; n <= limit; ++n) {

12

if (!isPrime(n)) continue;

13

r = reverse(n);

14

if (!isPrime(r)) continue;

15

printf("%d and %d are prime.\n", n, r);

16

}

17

return 0;

18

}

1

/* Return 1 if prime, 0 otherwise. */

2 3

int isPrime(int n) {

4

int i;

5

for (i = 2; i < n; ++i) {

6

if (n % i == 0) {

7

return 0;

8

}

9

}

10

return 1;

11

}

Functions – p. 11/19

slide-12
SLIDE 12

More than one parameter

Functions may also have more than one parameter.

1

#include <stdio.h>

2 3

float RectArea(float length, float width);

4 5

int

6

main()

7

{

8

float x, y, area;

9

printf("Length, width: ");

10

scanf("%g, %g", &x, &y);

11

area = RectArea(x, y);

12

printf("Area = %g\n", area);

13

return 0;

14

}

15 16

float

17

RectArea(float length, float width)

18

{

19

return length * width;

20

}

Length, width: 4, 5 Area = 20 Length, width: 12, 0.5 Area = 6

Functions – p. 12/19

slide-13
SLIDE 13

Miscellaneous Details

Libraries often have one or more header files, e.g., stdio.h, containing function prototypes. Functions that take no parameters should have a parameter list containing only the keyword void. Functions that do not return a value should declared to have a return type of void. (Such functions may be useful for their “side effects”.) Examples:

void exit(int exitCode); void abort(void); int rand(void);

Functions – p. 13/19

slide-14
SLIDE 14

Variable scope

Each function has its own scope for variables, so variables with the same name do not clash. A function may only use variables it declares itself (including parameters).

1

#include <stdio.h>

2 3

int OddEvenTest(int b);

4 5

int main() {

6

int a, result;

7 8

a = 5;

9

result = OddEvenTest(a);

10

printf("a = %d; result = %d\n", a, result);

11

return 0;

12

}

13 14

int OddEvenTest(int b) {

15

int a;

16

a = b % 2;

17

return a;

18

}

Both main and OddEvenTest contain a variable named a. These are different memory

  • bjects.

Memory objects are al- located when a function starts, and go away when it ends. a = 5; result = 1

Functions – p. 14/19

slide-15
SLIDE 15

Scope: Activation Records

1

#include <stdio.h>

2 3

int OddEvenTest(int b);

4 5

int main() {

6

int a, result;

7 8

a = 5;

9

result = OddEvenTest(a);

10

printf("a = %d; result = %d\n", a, result);

11

return 0;

12

}

13 14

int OddEvenTest(int b) {

15

int a;

16

a = b % 2;

17

return a;

18

}

a

main

5

Memory before calling OddEvenTest.

Functions – p. 15/19

slide-16
SLIDE 16

Scope: Activation Records

1

#include <stdio.h>

2 3

int OddEvenTest(int b);

4 5

int main() {

6

int a, result;

7 8

a = 5;

9

result = OddEvenTest(a);

10

printf("a = %d; result = %d\n", a, result);

11

return 0;

12

}

13 14

int OddEvenTest(int b) {

15

int a;

16

a = b % 2;

17

return a;

18

}

a b a

main OddEvenTest

5 5 1

Memory while OddEvenTest is active.

Functions – p. 15/19

slide-17
SLIDE 17

Scope: Activation Records

1

#include <stdio.h>

2 3

int OddEvenTest(int b);

4 5

int main() {

6

int a, result;

7 8

a = 5;

9

result = OddEvenTest(a);

10

printf("a = %d; result = %d\n", a, result);

11

return 0;

12

}

13 14

int OddEvenTest(int b) {

15

int a;

16

a = b % 2;

17

return a;

18

}

a

main

5

Memory after OddEvenTest returns.

Functions – p. 15/19

slide-18
SLIDE 18

Call by value

C uses call by value for its integer and floating point types. Changing the value of a parameter does not change the value of a variable passed as an argument.

1

#include <stdio.h>

2 3

int OddEvenTest(int a);

4 5

int main() {

6

int a, result;

7 8

a = 5;

9

result = OddEvenTest(a);

10

printf("a = %d; result = %d\n", a, result);

11

return 0;

12

}

13 14

int OddEvenTest(int a) {

15

a = a % 2;

16

return a;

17

}

a = 5; result = 1

Functions – p. 16/19

slide-19
SLIDE 19

Recursion

Recursion is when a function calls itself, either directly or

  • indirectly. Some conditional code, e.g., an if statement, is

needed to ensure recursion eventually stops. Example:

n! = (n − 1)!n 0! = 1

We can use these facts to compute n! recursively.

Functions – p. 17/19

slide-20
SLIDE 20

Computing n!

This only works for n ≥ 0.

  • 1. If n is 0, then return 1 (which is 0!).
  • 2. Otherwise, compute x = (n − 1)!.
  • 3. Return xn.

This is an example of solving a problem by “divide and conquer” (since computing (n − 1)! should be easier than computing n!).

Functions – p. 18/19

slide-21
SLIDE 21

Recursion: Example

1

#include <stdio.h>

2 3

int fact(int n);

4 5

int main() {

6

int n;

7

printf("Enter n: ");

8

scanf("%d", &n);

9

printf("%d! = %d\n", n, fact(n));

10

return 0;

11

}

12 13

int

14

fact(int n)

15

{

16

int x;

17

if (n == 0) {

18

return 1;

19

}

20

x = fact(n-1);

21

return x * n;

22

}

0! = 1 4! = 24

Functions – p. 19/19