Beginning C Programming for Engineers
Lecture 4: Functions
- R. Lindsay Todd
Functions – p. 1/19
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 .
Functions – p. 1/19
Functions – p. 2/19
Functions – p. 3/19
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
}
Functions – p. 4/19
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
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
}
Functions – p. 5/19
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
}
Functions – p. 6/19
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
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. 8/19
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
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
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
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
}
Functions – p. 12/19
Functions – p. 13/19
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
}
Functions – p. 14/19
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
Functions – p. 15/19
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
Functions – p. 15/19
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
Functions – p. 15/19
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
}
Functions – p. 16/19
Functions – p. 17/19
Functions – p. 18/19
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
}
Functions – p. 19/19