fundamentals of programming - wp.kntu.ac.ir · fundamentals of programming lecture 9 c functions....

44
Fundamentals of Programming lecture 9 C Functions

Upload: others

Post on 25-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Fundamentals of Programminglecture 9

C Functions

Page 2: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

What about more complicated math?

+ - * / %

Page 3: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

What about more complicated math?3 * x = 2

x*x = 2

3x = 4

ex = 2

sin(x) = .6

Page 4: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

What about more complicated math?x*x = 2

3x = 4

ex = 2

sin(x) = .6

arctan(x) = 10

Page 5: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Standard C library● ANSI C standard (ISO C)

○ C89, C90, C95, C99, C11, C18○ gcc -std=c90

● C Standard Library: functions, types, macros:○ Math○ I/O○ string operations○ memory management○ system

● https://www.slideshare.net/teach4uin/stdlib-functions-lesson ● https://www.tutorialspoint.com/c_standard_library

Page 6: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

http://www.cplusplus.com/reference/cmath/

Page 7: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

http://www.cplusplus.com/reference/cmath/

Page 8: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

http://www.cplusplus.com/reference/cmath/

Page 9: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

http://www.cplusplus.com/reference/cmath/

Page 10: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

http://www.cplusplus.com/reference/cmath/

Page 11: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

http://www.cplusplus.com/reference/cmath/

Page 12: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

#include <math.h>

Page 13: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

Page 14: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Math

Compile:

gcc testpow.c -l m

Page 15: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Why functions?

Page 16: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Goldbach's conjecture

every even integer (>2) is sum of two primes

Page 17: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Goldbach's conjecture

Page 18: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Goldbach's conjecture

Page 19: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Goldbach's conjectureComments!

Page 20: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

What's wrong?

Page 21: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

Functions

https://en.wikipedia.org/wiki/Function_(mathematics)

https://www.mathsisfun.com/sets/function.html

https://en.wikibooks.org/wiki/Algebra/Functions

Page 22: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

A function for detecting prime numbers

Write a function which gets a number and returns 1 if it is prime and 0 otherwise

is_prime22 0

Page 23: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

A function for detecting prime numbers

Write a function which gets a number and returns 1 if it is prime and 0 otherwise

is_prime7 1

Page 24: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

A function for detecting prime numbers

Write a function which gets a number and returns 1 if it is prime and 0 otherwise

is_prime7 1int

Page 25: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

A function for detecting prime numbers

Write a function which gets a number and returns 1 if it is prime and 0 otherwise

is_prime7 1int int

Page 26: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

A function for detecting prime numbers

Page 27: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

A function for detecting prime numbers

Page 28: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

A function for detecting prime numbers

Page 29: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

A function for detecting prime numbers

Page 30: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *
Page 31: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *
Page 32: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *
Page 33: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *
Page 34: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

declarations / function prototypes

Page 35: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

declarations / function prototypes

Page 36: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *
Page 37: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

C Preprocessor, include files

● printf (scanf, …)○ library: /lib/x86_64-linux-gnu/libc.so.6○ header file: /usr/include/stdio.h

■ mostly contains function declarations

Page 38: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

C Preprocessor● look at the header file

○ cat /usr/include/stdio.h

● look at the output of preprocessor:○ gcc -E test.c

Page 39: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

random number generation, rand, srand● run

○ man 3 rand

Page 40: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

random number generation, rand, srand● also in man 3 rand

Page 41: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

random number generation, rand, srand● a random number between 0 and RAND_MAX

○ rand()

● a random number between 0 and n-1○ rand() % n

● a random number between 1 and n○ rand() % n + 1

● RAND_MAX is defined in stdlib.h○ cat /usr/include/stdlib.h○ #define RAND_MAX 2147483647

Page 42: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *

remember choose.c

Page 43: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *
Page 44: Fundamentals of Programming - wp.kntu.ac.ir · Fundamentals of Programming lecture 9 C Functions. What about more complicated math?+ - * / % What about more complicated math? 3 *