270 likes | 378 Views
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.
E N D
TMC1414/TMC1413Introduction 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. • The mechanisms used to pass information between functions. • Simulation techniques using random number generation. • How to write and use recursive functions, i.e., functions that call themselves.
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
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
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
Math Library Functions Include the math header by using the preprocessor directive #include<math.h>when using functions in the math library.
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
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 function 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.
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); }
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); }
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
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); }
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); }
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); }
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);
#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; }
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
#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
#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
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
#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)); } }
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))
Check this out on youtube. • http://www.youtube.com/watch?v=iOS5sPivuJA&NR=1&feature=fvwp
Thank You # End of Session#