1 / 27

TMC1414/TMC1413 Introduction To Programming

TMC1414/TMC1413 Introduction To Programming. Lecture 06 Function. 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.

elana
Download Presentation

TMC1414/TMC1413 Introduction To Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TMC1414/TMC1413Introduction To Programming 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.

  3. Introduction • 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

  4. Program Modules 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

  5. Hierarchical boss function/worker function relationship.

  6. Math Library Functions • Math library functions • perform common mathematical calculations • #include <math.h> • 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

  7. Math Library Functions Include the math header by using the preprocessor directive #include<math.h>when using functions in the math library.

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

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

  10. Functions • 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

  11. Functions 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. 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. Functions 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. Functions void main(){ int a=10,b=20,m int big(int x, int y); m=big(a,b); printf(“Max=%d”,m); } 10 20 a b m int big(int x, int y){ if(x>y) return(x); else return(y); }

  14. Example: To find the larger between two numbers void main(){ int a=10,b=20,m int big(int x, int y); m=big(a,b); printf(“Max=%d”,m); } 10 20 a b 20 m 20 big int big(int x, int y){ if(x>y) return(x); else return(y); } 10 20 x y

  15. Write 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. Write 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) { inti, long intprob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } } return(prob); }

  17. Write 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) { inti, long intprob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } } return(prob); }

  18. Function Prototypes #include <stdio.h> 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); } • 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 int cubebyvalue(int);

  19. #include <stdio.h> 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. #include <stdio.h> 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); } • 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

  22. #include <stdio.h> void cubebyreference(int*); main() { inta,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; } • Call by reference • Passes original argument • Changes in function effect original • Only used with trusted function

  23. Recursion • 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

  24. #include <stdio.h> 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! • 4! = 4 * 3! • 3! = 3 * 2! • 2! = 2 * 1! • 1! = 1 • 5! = 5 * 4 * 3 * 2 *1 • 4! = 4 * 3 * 2 * 1 • 3! = 3 * 2 * 1 • 2! = 2 * 1 • 1! = 1 Factorial(5) Factorial(4) Factorial(3) Factorial(2) Factorial(1) 2 3 4 5 factorial(5) in main 3*2! 4*3! 5*4! 2*1 1 return(n*factorial(n-1))

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

  27. Thank You # End of Session#

More Related