![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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/1.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/2.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/3.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/4.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/5.jpg)
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 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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/12.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/13.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/14.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/15.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/16.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/17.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/18.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/19.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/20.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/21.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/22.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/23.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/24.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/25.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/26.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/27.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/28.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/29.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/30.jpg)
![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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/31.jpg)
![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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/32.jpg)
![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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/33.jpg)
![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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/34.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/35.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/36.jpg)
![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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/37.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/38.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/39.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/40.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/41.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/42.jpg)
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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/43.jpg)
![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 *](https://reader033.vdocuments.net/reader033/viewer/2022042806/5f6e82627d061072ab4fe636/html5/thumbnails/44.jpg)