tk1913-c programming1 tk1913-c programming 1 c library functions c provides a collection of library...

26
TK1913-C Programming TK1913-C Programming 1 Functions 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.

Post on 21-Dec-2015

254 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 11

C Library FunctionsC 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.

Page 2: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 22

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>

Page 3: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 33

math math Library functionLibrary 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

Page 4: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 44

Example: sqrt() function This function can be used to attain the square root of a given number The function declaration:

Example:double num; printf(“Input a number: ");scanf("%lf", &num);printf(“Square root of %.2lf: %.2lf\n”, num, sqrt(num));

double sqrt(double n);

The number its square root you

want to get

The data type returned by

function sqrt()

math math Library functionLibrary function

Page 5: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 55

Example: abs() function This function is used to get the absolute value of a

number The function declaration:

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);

int abs(int n);

The number its absolute value you want to get

The data type returned by abs()

function

math math Library functionLibrary function

Page 6: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 66

Variable scopeVariable 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

Page 7: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 77

#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

Variable scope : local variablesVariable scope : local variables

Page 8: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 88

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

Variable scope : local variablesVariable scope : local variables

number

#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;}

void negative(int number)number

int main( )

Page 9: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 99

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;}

Variable scope : global variablesVariable scope : global variables

numberint main( )

void negative()

Page 10: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1010

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

Recursive FunctionRecursive Function

Page 11: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1111

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

Recursive FunctionRecursive Function

Page 12: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1212

factorial(5) = 5 * factorial(4)

Getting the value of factorial(5) using recursion

Recursive Function Example : factorialRecursive Function Example : factorial

factorial(5) = 5 * factorial(4)

factorial(4) = 4 * factorial(3)

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)

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(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 * 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(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 * 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 * 2

factorial(2) = 2

factorial(5) = 5 * factorial(4)

factorial(4) = 4 * factorial(3)

factorial(3) = 6

factorial(5) = 5 * factorial(4)

factorial(4) = 4 * 6

factorial(3) = 6

factorial(5) = 5 * factorial(4)

factorial(4) = 24

factorial(5) = 5 * 24

factorial(4) = 24

factorial(5) = 120

Page 13: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1313

#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;

elsereturn (n * factorial(n - 1));

}

This is a recursive function becausea function call in its body calls itself.

factorial function (using recursion)factorial function (using recursion)

Page 14: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1414

#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;

elsereturn (n * factorial(n - 1));

}

0! = 1

n! = n * (n – 1)!

factorial function (using recursion)factorial function (using recursion)

Page 15: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1515

#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;

elsereturn (n * factorial(n - 1));

}

???

n

_

int main()

Input n :_

5

Input n : 5

5n1

Caller Called P/m passed Returned value

main fac-1 5

fac-1

Page 16: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1616

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1int factorial(int n) {

Caller Called P/m passed Returned value

main fac-1 5

Caller Called P/m passed Returned value

main fac-1 5 5 * factorial(4)

fac-2 (4)

n2 4

Caller Called P/m passed Returned value

main fac-1 5 5 * factorial(4)

fac-1 fac-2 4

Page 17: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1717

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1int factorial(int n) {

fac-3 (3)

n2 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 5 * factorial(4)

fac-1 fac-2 4 4 * factorial(3)

3n3

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

Page 18: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1818

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1int factorial(int n) {

fac-4 (2)

n2 4

3n3

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

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)

n4 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

Page 19: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 1919

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1int factorial(int n) {

fac-5 (1)

n2 4

3n3

n4 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

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)

n5 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

Page 20: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 2020

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1

int factorial(int n) {

fac-6 (0)

n2 4

3n3

n4 2

n5 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

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)

n6 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)

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 1

Page 21: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 2121

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1

int factorial(int n) { n2 4

3n3

n4 2

n5 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

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

Page 22: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 2222

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1

int factorial(int n) { n2 4

3n3

n4 22

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

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 * 2

Page 23: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 2323

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1

int factorial(int n) { n2 4

3n36

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

Page 24: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 2424

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1

int factorial(int n) { n2 4

24

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

Page 25: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 2525

#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;

elsereturn (n * factorial(n - 1));

}

5

n

Input n : 5

5n1

int factorial(int n) {

120

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

Value of 5! = 120

int main()

Page 26: TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions

TK1913-C ProgrammingTK1913-C Programming 2626

#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;

elsereturn (n * factorial(n -1));

}

Input n : 5

Value of 5! = 120