Presentation is loading. Please wait.

Presentation is loading. Please wait.

Senem Kumova Metin // CS115 // 2006-20071 FUNCTIONS CHAPTER 5.

Similar presentations


Presentation on theme: "Senem Kumova Metin // CS115 // 2006-20071 FUNCTIONS CHAPTER 5."— Presentation transcript:

1 Senem Kumova Metin // CS115 // 2006-20071 FUNCTIONS CHAPTER 5

2 Senem Kumova Metin // CS115 // 2006-20072 1.FUNCTION DEFINITION 2.FUNCTION EXAMPLES 3.GLOBAL VERSUS LOCAL VARIABLES 4.RETURN STATEMENTS 5.FUNCTION PROTOTYPES 6.FUNCTION DEFINITION ORDER 7.CALL BY VALUE 8.DEVELOPING A LARGE PROGRAM 9.SCOPE RULES 10.STORAGE CLASSES 11.RECURSION

3 Senem Kumova Metin // CS115 // 2006-20073 Why we use functions? Taking a problem and breaking it into small, manageable pieces is critical to write large programs Each program consists of zero or more functions and one of them is main() !!! Program execution starts with main() function

4 Senem Kumova Metin // CS115 // 2006-20074 Function definition return_type function_name(input_parameter_list) { declarations/* body of the function */ statements return …… } return_type : * Zero or one data type ( int, char, float etc.) “void” keyword can be used if there is no return value * Write the type of the return variable… input_parameter_list : * Zero or more variables (as int x, float c etc.) * If more than one input parameter exist, then put commas between them * If there is no parameter, “void” keyword can be used

5 Senem Kumova Metin // CS115 // 2006-20075 Function definition : EXAMPLES int add( int x, int y) /* header */ {/* body starts here */ int result; result=x+y; return result; } double twice(double x) /* header */ { /* body starts here */ double result; result = x*2; return result; // return x*2 } /* this function is called “double()”, it will return a data of type double, it gets an input data of type double */ void menu() /* header */ {/* body starts here */ printf(“ A\n ”); printf(“ B\n ”); printf(“ C\n ”); }

6 Senem Kumova Metin // CS115 // 2006-20076 Function : EXAMPLE /* FUNCTIONS MUST BE DECLARED BEFORE CALLED */ #include int twice(int x) { return x*2; } int add( int x, int y) {int result; result=x+y; return result; // return x+y; } main() {int x=5, y=4, z=3; z= twice(4); x=twice(x); y=add(z,x); printf(“%d %d %d \n”, x,y,z);} OUTPUT ??

7 Senem Kumova Metin // CS115 // 2006-20077 Global versus Local variables Any variables declared in the body of functions are said to be “local” to that function Other variables may be declared external to the function, called “global” variables EXAMPLE : #include int a =3; // global variable to main() void main(void) {int b= 7; // local variable to main() printf(“%d”, a); printf(“%d”, b); printf(“%d”, a+b); printf(“%d”, a++); } OUTPUT ??

8 Senem Kumova Metin // CS115 // 2006-20078 Global versus Local variables #include int a =3; // global variable to main() and new_func int new_func( int y) {int b =1; // b is local to new_func() a++; y++; b++; return a+y+b; } void main(void) {int b=7, r =0; // local variables to main() printf(“a= %d \n”, a); printf(“b= %d \n ”, b); r=new_func(b); // value of variable b =7 is sent to function printf(“a= %d \n”, a); printf(“b= %d \n”, b); printf(“r= %d \n”, r); }

9 Senem Kumova Metin // CS115 // 2006-20079 #include int a = 3; // global variable to main() and new_func int y = 4; // global variable to main() and new_func int new_func( int y) { int a =1; return a+y; } void main(void) {int a =0 r=0; // local variables to main() printf(“a= %d \n”, a); r =new_func(a); printf(“a = %d \n”, a); printf(“r = %d \n”, r);} What happens if local variables have the same name with global variables ?? ANSWER : LOCAL VARIABLE IS DOMINANT OVER GLOBAL

10 Senem Kumova Metin // CS115 // 2006-200710 return statement The return statment may or may not include an expression The return statement terminates the execution of the function, and closes memory space opened for all local variables (kills all local variables of function) EXAMPLE: float f(int a, char b) { int i; ….. return i; /* value of i will be converted to float */ /* return (i); */ /* return value can also be written in braces */ }

11 Senem Kumova Metin // CS115 // 2006-200711 return statement : 4 EXAMPLES char func1( int a, int b) { return a+b; } double my_sqrt( int a ) { return sqrt(a); } int my_max1( int y, int z) { return y>z? y:z ; } int my_max2 (int y, int z, int t, int x); { int a, b; a= my_max1(y,z); b=my_max1(t,x); return my_max1(a,b);} 1 2 3 4 Functions can call other functions

12 Senem Kumova Metin // CS115 // 2006-200712 Function Prototypes Each function has to be declared before it is used There are two ways to achieve this rule.. 1.Define the functions before used 2.Write the function prototype of the function before used ( Just declaration not definition) Function prototype includes header information : 1.Return type 2.Function name 3.Input parameter list

13 Senem Kumova Metin // CS115 // 2006-200713 Function Prototypes: EXAMPLE #include double twice(double x); /* FUNCTION PROTOTYPE FUNCTION DECLARATION */ // double twice (double ); void main(void) { double y, r; r= twice(y); // twice()is used in this line printf(“result is %f\n”, r); } double twice (double x) /* FUNCTION DEFINITION */ { return x*2;}

14 Senem Kumova Metin // CS115 // 2006-200714 Function Definition Order EXAMPLE 1: #include int func_2(); int func_1(); int func_3(); void main(void) { func_1(); func_2(); func_3(); } int func_2(){…}; int func_1(){…}; int func_3(){…}; EXAMPLE 2: #include int func_2(); int func_1() { func_3(); } int func_3(); void main(void) { func_2(); func_1(); } int func_2(){…}; int func_3(){…};

15 Senem Kumova Metin // CS115 // 2006-200715 Function Declarations from the Compilers view point int f(x) // traditional C style double x; {…..} /* Compiler does not know about the parameter list and the type of x is double so f(1) will fail */ int f(double x) // ANSI C style {…..} /* Compiler knows about the parameter list so f(1) will NOT fail */

16 Senem Kumova Metin // CS115 // 2006-200716 CALL BY VALUE If a variable is passed to a function, the stored value in the calling environment will not be changed!!! EXAMPLE: #include int my_sum(int n); void main(void) {int n=9; printf(“%d\n”,n); printf(“%d\n”,my_sum(n)); // call function my_sum() by value printf(“%d\n”,n); } int my_sum(int n) { n=n+2; // stored value of n in my_sum() is changed return n; } OUTPUT ??

17 Senem Kumova Metin // CS115 // 2006-200717 Developing a large program read 5.8 (pg 209) /* my_program.c */ #include”my_header.h” void main(void) { int a=3, b=4, result=0; result=my_sum(a,b); printf(“%d\n”,result); result=my_subtract(a,b); printf(“%d\n”,result); printf(“%f\n”,PI); } /* my_header.h */ #include #define PI 3.14 int my_sum(int x,int y) { x++; y++; return x+y; } int my_subtract(int x, int y) {return x-y-1; }

18 Senem Kumova Metin // CS115 // 2006-200718 SCOPE RULES Each identifier is accessible only within the block in which it has been declared!!! EXAMPLE: #include void func_1 (int a) {int b, c; } void func_2 (int a, double b, float d) { char c; } void main () { int a,b,d; char c; } func1() variables abc abdc func2() variables abcd main() variables

19 Senem Kumova Metin // CS115 // 2006-200719 SCOPE RULES : EXAMPLE # include void main (void) {int a=2; float b=3.2; // outer block a printf("%d\n",a); {int a =5; // inner block a printf("%d\n", a); printf("%f\n", a+b); { int b=4; printf("%d\n“, a+b); } printf("%d\n",++a); } OUTPUT: 2 5 8.200000 9 3

20 Senem Kumova Metin // CS115 // 2006-200720 STORAGE CLASSES  EACH VARIABLE HAS A TYPE AND A STORAGE CLASS  STORAGE CLASSES 1.auto /* variables declared within function bodies are automatic by default */ 2.extern /* look for it elsewhere either in this file or in some other file */ 3.register 4.static  EXAMPLE extern int a=1; /* type of variable a is int, storage class of a is extern */ auto float f; float z; /* storage class is auto and data type is float */

21 Senem Kumova Metin // CS115 // 2006-200721 STORAGE CLASSES 2. EXTERN The keyword extern is used to tell the compiler to "look for it elsewhere, either in this file or in some other file" External variables never disappear. Because they exist throughout the execution life of the program, they can be used to transmit values across functions ( GLOBAL)

22 Senem Kumova Metin // CS115 // 2006-200722 STORAGE CLASSES 2. EXTERN /* file1.h */ int f(void) { extern int a; /* look for it elsewhere */ int b, c; // local b and c a = b = c = 4; // global b anc c are masked return (a + b + c); } /* prog1.c*/ #include #include ”file1.h” int a = 1, b = 2, c = 3; /* global variables */ int f(void); int main(void) { printf("%3d%3d%3d\n", a, b, c); printf("%3d\n", f() ); printf("%3d%3d%3d\n", a, b, c); return 0; } OUTPUT: 1 2 3 12 4 2 3

23 Senem Kumova Metin // CS115 // 2006-200723 STORAGE CLASSES 3. REGISTER Forces compiler that these variables should be stored in high speed memory registers Compiler may or may not store them in registers Reading data from registers is faster comparing to reading from memory EXAMPLES: register char c; register int a=1; /* IS EQUIVALENT TO */ register a=1; // default data type is int

24 Senem Kumova Metin // CS115 // 2006-200724 STORAGE CLASSES 4. STATIC Memory opened for the static variable will not be closed after the execution of the function Initialization for static variables is done just once in the first execution of the function EXAMPLE: #include int ver(); main() {printf(“1. value=%d\n”, ver() ); printf(“2. value=%d\n”, ver() ); printf(“3. value=%d\n”, ver() ); } int ver() {static int k =0; k=k+5; return k; } OUTPUT 1. value=5 2. value=10 3. value=15

25 Senem Kumova Metin // CS115 // 2006-200725 STORAGE CLASSES STATIC EXTERNAL VARIABLES Can be seen below the prototype/declaration but can not be seen in any other file Scope restricted Cannot be used with functions defined earlier or in any other files Program security !! EXAMPLE : #include void f(void) {...... } // v is not available static int v; // static external variable void g(void) {..... } // you can use v

26 Senem Kumova Metin // CS115 // 2006-200726 RECURSION An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task If a function calls itself, then it is called recursive !!! There are two parts to the definition of a recursive solution: –Base case: Simple, non-recursive solution –General method: Uses simpler version of the problem to solve harder problem

27 Senem Kumova Metin // CS115 // 2006-200727 RECURSION : EXAMPLES void main(void) { printf(“I am calling myself!\n”); main(); } /* NO BASE CASE SO IT WILL NEVER STOP */ int sum(int n) // what does this function compute ?? { if(n<=1) return n; else return(n+sum(n-1));} /* BASE CASE : n<=1  n METHOD :sum(n) = n + sum(n-1) */

28 Senem Kumova Metin // CS115 // 2006-200728 RECURSION & ITERATION EXAMPLE : FACTORIAL /*iterative version n! = 1*2*3* … * n = n*(n-1)*(n-2) * …*3*2*1 */ int factorial (int n) { int product=1; for(;n>1;--n) product=product*n; return product;} /* recursive version n! = n * (n-1)! */ int factorial(int n) { if(n<=1) return 1; else return ( n*factorial(n-1) ); } /* BASE CASE : n<=1  1 METHOD : n!=n*(n-1)! */ factorial(3) = 3 * factorial(2) = 3 * ( 2 * factorial(1) ) = 3 * ( 2 * ( 1 * factorial(0) )) = 3 * ( 2 * ( 1 * 1 ) )) = 6

29 Senem Kumova Metin // CS115 // 2006-200729 RECURSION EXAMPLE : FIBONACCI In mathematics, the Fibonacci numbers form a sequence definedmathematicssequence recursivelyrecursively by for n=0  fibonacci(0) = 0 fibonacci (n) = for n=1  fibonacci(1) = 1 for n >1  fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) int fibonacci(int n) { if(n<=1) return 1; else return ( fibonacci(n-1)+fibonacci(n-2) );} /* BASE CASE : n<=1  1 METHOD : fibonacci(n)=fibonacci(n-1)+ fibonacci(n-2)*/

30 Senem Kumova Metin // CS115 // 2006-200730 YOUR HOMEWORK TOWERS OF HANOI


Download ppt "Senem Kumova Metin // CS115 // 2006-20071 FUNCTIONS CHAPTER 5."

Similar presentations


Ads by Google