840 likes | 2.89k Views
C Library Functions. C provides a collection of library functions for programmers If these library functions are used in a program, be sure to include their prototypes. Library function prototypes are placed in specific header files.
E N D
C Library Functions • C provides a collection of library functions for programmers • If these library functions are used in a program, be sure to include their prototypes. Library function prototypes are placed in specific header files. • Example: Function prototypes of printf() and scanf() can be found in header file stdio.h. TK1913-C Programming1
To include a function prototype defined in a header file into our program, use the preprocessor instruction#include. • Example, #include <stdio.h> #include <math.h> TK1913-C Programming2
math Library function • One type of library functions which provides several functions for execution of mathematic operations is math • Prototypes of these functions are declared in a header file named math.h TK1913-C Programming3
The number its square root you want to get math Library function Example: sqrt()function • This function can be used to attain the square root of a given number • The function declaration: double sqrt(double n); The data type returned by function sqrt() Example: double num; printf(“Input a number: "); scanf("%lf", &num); printf(“Square root of %.2lf: %.2lf\n”, num, sqrt(num)); TK1913-C Programming4
The number its absolute value you want to get The data type returned by abs() function math Library function Example: abs() function • This function is used to get the absolute value of a number • The function declaration: int abs(int n); Example: int num, abs_value; printf(“Input number: "); scanf("%d", &num); abs_value = abs(num); printf(“Absolute value for %d: %d\n”, num, abs_value); TK1913-C Programming5
Variable scope • There are two types of scope for variables: local and global • Local variable : • May only be accessed and manipulated in the block where it is declared • Global variable : • May be accessed and manipulated anywhere in a program, including in user-defined functions • Must be declared outside a function definition • Is not encouraged because program readability may be compromised and maintenance more tedious TK1913-C Programming6
Variable scope : local variables #include <stdio.h> void negative(int); int main( ) { int num; scanf("%d", &num); negative(num); printf(“Value of num is now: %d\n", num); } void negative(int number) { number = -number; } Example: ??? num ??? number TK1913-C Programming7
Variable scope : local variables • In the following example, the variable number in main() and variable number in negative() are not referring to the same memory location. Both are local variable #include <stdio.h> void negative(int); int main( ) { int number; scanf("%d", &number); negative(number); printf(“Value of number is now: %d\n", number); } void negative(int number) { number = - number; } int main( ) number void negative(int number) number TK1913-C Programming8
Variable scope : global variables • In the following example, number in main() and negative() are referring to the same memory location #include <stdio.h> void negative(); int number; int main( ) { scanf("%d", &number); negative(); printf(“Value of number is now : %d\n", number); } void negative() { number = -number; } number int main( ) void negative() TK1913-C Programming9
Recursive Function • Can a function call itself ? • A function that calls itself is known as recursive function • Recursive function is used to handle problem that can be broken into sub-problem which resembles the original problem, repeatedly until the solution is achieved TK1913-C Programming10
Recursive Function • Example : Factorial formula can be written as : 1, when n = 0 n! = n * (n-1) * … * 2 * 1 , when n >= 1 In other words, 1, when n = 0 n! = n * (n-1)! , when n >= 1 TK1913-C Programming11
Recursive Function Example : factorial • Getting the value of factorial(5) using recursion factorial(5) = 5 * factorial(4) factorial(5) = 5 * factorial(4) factorial(4)= 4 * 6 factorial(3) = 6 factorial(5) = 5 * 24 factorial(4) = 24 factorial(5) = 5 * factorial(4) factorial(4) = 24 factorial(5) = 120 factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 6 factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * 2 factorial(2) = 2 factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * factorial(0) factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * 1 factorial(1) = 1 factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * 1 factorial(0) = 1 factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * factorial(0) factorial(0) = 1 factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(5) = 5 * factorial(4) factorial(4)= 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) TK1913-C Programming12
factorial function (using recursion) #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial( int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } This is a recursive function because a function call in its body calls itself. TK1913-C Programming13
factorial function (using recursion) #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial( int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 0! = 1 n! = n * (n – 1)! TK1913-C Programming14
Caller Called P/m passed Returned value main fac-1 5 _ Input n :_ Input n : 5 int main() #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 ??? fac-1 n n1 5 TK1913-C Programming15
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 Caller Called P/m passed Returned value main fac-1 5 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n int factorial(int n) { n1 5 4 n2 fac-2 (4) TK1913-C Programming16
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n int factorial(int n) { n1 5 4 n2 3 n3 fac-3 (3) TK1913-C Programming17
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n int factorial(int n) { n1 5 4 n2 3 n3 fac-4 (2) n4 2 TK1913-C Programming18
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * factorial(1) fac-4 fac-5 1 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * factorial(1) Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n int factorial(int n) { n1 5 4 n2 3 n3 fac-5 (1) n4 2 n5 1 TK1913-C Programming19
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * factorial(1) fac-4 fac-5 1 1 * factorial(0) fac-5 fac-6 0 1 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * factorial(1) fac-4 fac-5 1 1 * factorial(0) fac-5 fac-6 0 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * factorial(1) fac-4 fac-5 1 1 * factorial(0) Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * factorial(1) fac-4 fac-5 1 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n n1 5 int factorial(int n) { 4 n2 3 n3 1 n4 2 fac-6 (0) n5 1 n6 0 TK1913-C Programming20
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * factorial(1) fac-4 fac-5 1 1 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * factorial(1) fac-4 fac-5 1 1 * 1 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n n1 5 int factorial(int n) { 4 n2 3 n3 1 n4 2 n5 1 TK1913-C Programming21
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * 2 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * factorial(2) fac-3 fac-4 2 2 * 1 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n n1 5 int factorial(int n) { 4 n2 3 n3 2 n4 2 TK1913-C Programming22
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 3 * 2 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * factorial(3) fac-2 fac-3 3 6 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n n1 5 int factorial(int n) { 4 n2 3 n3 6 TK1913-C Programming23
Input n : 5 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 4 * 6 Caller Called P/m passed Returned value main fac-1 5 5 * factorial(4) fac-1 fac-2 4 24 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n n1 5 int factorial(int n) { 4 n2 24 TK1913-C Programming24
Caller Called P/m passed Returned value main fac-1 5 5 * 24 Caller Called P/m passed Returned value main fac-1 5 120 Input n : 5 Input n : 5 Value of 5! = 120 int main() #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n n1 5 int factorial(int n) { 120 TK1913-C Programming25
Input n : 5 Value of 5! = 120 #include <stdio.h> int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n -1)); } TK1913-C Programming26