Presentation is loading. Please wait.

Presentation is loading. Please wait.

TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.

Similar presentations


Presentation on theme: "TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function."— Presentation transcript:

1 TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function

2 OBJECTIVES In this chapter you will learn: To construct programs modularly from small pieces called functions. The common math functions available in the C Standard Library. To create new functions. The mechanisms used to pass information between functions. Simulation techniques using random num­ber generation. How to write and use recursive functions, i.e., functions that call themselves. 2

3 I NTRODUCTION Divide and conquer Construct a program from smaller pieces or components These smaller pieces are called modules Each piece more manageable than the original program 3

4 P ROGRAM M ODULES IN C Functions Modules in C Programs combine user-defined functions with library functions C standard library has a wide variety of functions Function calls Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results Function call analogy: Boss asks worker to complete task Worker gets information, does task, returns result Information hiding: boss does not know details 4

5 5 Hierarchical boss function/worker function relationship.

6 M ATH L IBRARY F UNCTIONS Math library functions perform common mathematical calculations #include Format for calling functions FunctionName( argument ); If multiple arguments, use comma-separated list printf( "%.2f", sqrt( 900.0 ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions 6

7 M ATH L IBRARY F UNCTIONS Include the math header by using the preprocessor directive #include when using functions in the math library. 7

8 8 Commonly used math library functions. (Part 1 of 2.)

9 9 Commonly used math library functions. (Part 2 of 2.)

10 F UNCTIONS Modularize a program All variables defined inside functions are local variables Known only in function defined Parameters Communicate information between functions Local variables Benefits of functions Divide and conquer Manageable program development Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) Avoid code repetition 10

11 F UNCTIONS In programs containing many functions, main is often implemented as a group of calls to functions that perform the bulk of the program’s work. 11 Each function should be limited to performing a single, well- defined task, and the func­tion name should effectively express that task. This facilitates abstraction and promotes software reusability. Programs should be written as collections of small functions. This makes programs easier to write, debug, maintain and modify.

12 F UNCTIONS 12 void main(){ int a=10,b=20,m; int big(int x, int y); m=big(a,b); printf(“Max=%d”,m); } int big(int x, int y){ if(x>y) return(x); else return(y); }

13 F UNCTIONS 13 void main(){ int a=10,b=20,m int big(int x, int y); m=big(a,b); printf(“Max=%d”,m); } int big(int x, int y){ if(x>y) return(x); else return(y); } 1020 a b m

14 E XAMPLE : T O FIND THE LARGER BETWEEN TWO NUMBERS 14 void main(){ int a=10,b=20,m int big(int x, int y); m=big(a,b); printf(“Max=%d”,m); } int big(int x, int y){ if(x>y) return(x); else return(y); } 1020 a b 1020 x y 20 big 20 m

15 W RITE YOUR OWN FUNCTION Function definition Two principal components long int factorial(int n) { int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } return(prob); }

16 W RITE YOUR OWN FUNCTION Function definition Two principal components First line Contains data type to be returned Contains the function name A set of arguments (optional) long int factorial(int n) { int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } return(prob); }

17 W RITE YOUR OWN FUNCTION Function definition Two principal components First line Contains data type to be returned Contains the function name A set of arguments (optional) Body of function Contains expression statements for the operation required If data type of the function had been defined, should return more then one functions. long int factorial(int n) { int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } return(prob); }

18 F UNCTION P ROTOTYPES Function can be defined with first line and body However, compiler may confuse the function name and cause error of unknown variable or function Function prototypes is introduced to alert the compiler regarding existence of function #include main() { int a; printf("\na = "); scanf("%d", &a); d = cubebyvalue(a); printf("\n\n maximum = %d", a); return 0; } int cubebyvalue(int x) { return(x * x * x); } int cubebyvalue(int);

19 #include int cubebyvalue(int x) { return(x * x * x); }; main() { int a; printf("\na = "); scanf("%d", &a); d = cubebyvalue(a); printf("\n\n maximum = %d", a); return 0; }

20 Accessing function Usually known as calling a function A function can be called by its name (from first line ). Two type of function access Call by reference Call by value

21 Two type of function access Call by value Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument Avoids accidental changes 21 #include int cubebyvalue(int); main() { int a,d; printf("\na = "); scanf("%d", &a); d = cubebyvalue(a); printf("\n\n%d,%d", a,d); return 0; } int cubebyvalue(int x) { return(x * x * x); }

22 Call by reference Passes original argument Changes in function effect original Only used with trusted function #include void cubebyreference(int*); main() { int a,d; printf("\na = "); scanf("%d", &a); cubebyreference(&a); printf("\n\n %d,%d", a,d); return 0; } void cubebyreference(int *xPtr) { *xPtr =*xPtr * *xPtr * *xPtr; }

23 R ECURSION Recursive functions Functions that call themselves Can only solve a base case Divide a problem up into What it can do What it cannot do What it cannot do resembles original problem The function launches a new copy of itself (recursion step) to solve what it cannot do Eventually base case gets solved Gets plugged in, works its way up and solves whole problem 23

24 #include int factorial(int n); main() { int n; printf("n = "); scanf("%d", &n); printf("n! = %d\n", factorial(n)); } int factorial(int n) { if (n<=1) { return 1; }else { return(n * factorial(n-1)); }

25 5! = 5 * 4 * 3 * 2 *1 4! = 4 * 3 * 2 * 1 3! = 3 * 2 * 1 2! = 2 * 1 1! = 1 5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 factorial(5) in main Factorial(5) 1 5*4!4*3!3*2!2*1 Factorial(4) 5 Factorial(3) 4 Factorial(2) 3 Factorial(1) 2 return(n*factorial(n-1))

26 Check this out on youtube. http://www.youtube.com/watch?v=iOS5sPivuJA& NR=1&feature=fvwp

27 # E ND OF S ESSION # 27 Thank You


Download ppt "TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function."

Similar presentations


Ads by Google