1 Chapter 4 Functions By C.K. Liang
What you should learn? 2 To construct programs modularly from small pieces called functions Math functions in C standard library Create new functions Pass information between functions Simulation by random number genetation Write and use functions that call themselves
Introduction 3 Program design technique: Building blocks (modules)
Function format 4 Syntax: Data_type function_name(argument_list) Statements Return value; Notices: A function may have no arguments. The return statement will be missed if the function returns no values. The void datatype must be added when there is no returned value.
Function examples 5 Example 1: int max(int x, int y) if(x>y) return x; else return y; Example 2: void print_result(int x) printf( The result = %d\n,x);
Function prototype and definitions 6 Example: #include <stdio.h> Int main(void) int square(int); prototype int x; x = square(5); call int square(int x) definition return x*x;
Math library functions 7 Math library functions allow you to perform certain common mathematical calculations. Example: sqrt : square root of a number #include <math.h> printf( %7.2f, sqrt(900.0));
Math library functions 8
Math library functions 9
Function variables 10 All variables defined in functions are called local variables Local variables: Cannot be used outside the function int main(void) int square(int); int x; x = square(5); int square(int x) local variable return x*x;
Functions 11 #include <stdio.h> int max(int,int); int main(void) int x,y,z; printf( Maximum = %d\n, max(x,max(y,z))); int max(int a,int b) if(a>b) return a; else return b;
Argument passing 12 How to pass the arguments Called by value A copy of argument s value is made and passed to function Changes to the copy do not change the original value Called by address (reference) The memory address of argument s value is passed to function Changes to the argument s value means to change the original value How to return the value back to the calling function Called by address
Arguments passing 13 #include <stdio.h> void swap(int,int); int main(void) int x=3,y=5; swap(x, y); printf( x = %d, y = %d\n, x,y); void swap(int a,int b) int temp; temp = a; a = b; b = temp;
Random number generation 14 C standard library function: rand #include <stdlib.h> i = rand(); rand function generates an integer between 0 and RAND_MAX (32767)
Random number generation 15 To get different random sequences Provide a seed value Like a random starting point in the sequence The same seed will give the same sequence srand(seed) <cstdlib> Used before rand() to set the seed
Random number generation 16 #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) int i; srand(time(null)); time return seconds since 1970.1.1 for(i=1;i<=10;i++) printf( %d, (rand()%6) + 1);
Recursion 17 A function calls itself int factorial(int); int main(void) printf( 5! = %d\n, factorial(5)); int factorial(int n) definition if(n <=1 ) return 1 else return n * factorial(n 1); call itself
Recursion 18 Recursively calling of itself
Recursion example 19 Example: Greatest common divisor (GCD) GCD(M,N) = M if N = 0; = GCD(N,M) if M<N; = GCD(N, M % N) GCD(36,24) = GCD(24,12) = GCD(12,0) = 12
Recursion example 20 Example: Fibonacci series : 0,1,1,2,3,5,8,13,21, fib(n) = 0, if n = 0; = 1, if n = 1; = fib(n-1) + fib(n-2), if n>1 fib(3) = fib(2) + fib(1) = fib(1)+fib(0) + 1 = 1 + 0 + 1 = 2