Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)

Similar presentations


Presentation on theme: "1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)"— Presentation transcript:

1 1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)

2 2 Topics Functions Parameters Return values Reading: D&D (2/e or 3/e) Chapter 5, Sections 5.1 to 5.5

3 3 User-Defined Functions Create your own functions, similar to printf() or sqrt() Recall a procedure in an algorithm - a named collection of instructions –Drive_to_Uni –Do_Wednesday –Do_Week A function implements the procedure or function parts of an algorithm.

4 4 User-Defined Functions Maths functions return a value: x = sin y = x 3 + 2x 2 + 4x + 6 C functions also sometimes return a value - it’s called the “return value” of the function

5 5 User-Defined Functions Functions have parameters: x = sin y = √x printf(“hello world”); printf(“a = %d, b = %f\n”,a,b);

6 6 Writing User-defined Functions Need to specify: –the name of the function –its parameters –what it returns –block of statements to be carried out when the function is called The block of statements is called the “function body”

7 7 Prints a simple greeting. procedure sayHello { output “Hello World!” } Main Program { do procedure sayHello } Example: hello1.c

8 8 #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which * prints a simple greeting. */ int main() { sayHello(); return 0; } Prints a simple greeting. procedure sayHello { output “Hello World!” } Main Program { do procedure sayHello } Example: hello1.c

9 9 Function definition Function call #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which * prints a simple greeting. */ int main() { sayHello(); return 0; }

10 10 Example: hello1.c Function name Function body #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which * prints a simple greeting. */ int main() { sayHello(); return 0; }

11 11 Example: hello1.c Return type Formal Parameter List #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which * prints a simple greeting. */ int main() { sayHello(); return 0; }

12 12 Parameters Information passed to a function. “Formal” parameters are local variables declared in the function declaration. “Actual” parameters are variables or values passed to the function when it is called.

13 13 /* Print two numbers in order. */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); } Example: badsort.c Parameters (aka Arguments)

14 14 Example: badsort.c /* Print two numbers in order. */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); }

15 15 int main() { int x = 3, y = 5; badSort ( 10, 9 ); badSort ( y, x+4 ); return 0; } Example: badsort.c /* Print two numbers in order. */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); } Formal parameters Actual parameters

16 16 Parameters (cont.) Parameters are passed by copying the value of the actual parameters to the formal parameters. Changes to formal parameters do not affect the value of the actual parameters.

17 17 int main() { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } Example: badswap.c /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

18 18 Example: badswap.c Output: 3 5 int main() { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

19 19 Example: badswap.c Output: 3 5 5 3 int main() { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

20 20 Example: badswap.c Output: 3 5 5 3 3 5 int main() { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

21 21 Example: badswap.c Calling function’s environment: a: 3 b: 5 Called function’s environment: a: 5 b: 3 int main() { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

22 22 Parameters (cont.) If a function does not take parameters, declare its formal argument list void. void sayHello ( void ) { printf(“Hello World!\n”); } sayHello(); Function call: Declaration:

23 23 Return Values Values are returned by copying a value specified after the return keyword.

24 24 /* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c Return type

25 25 /* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c For example: The value of the expression max(7,5) is the integer 7.

26 26 /* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c This style okay.

27 27 Return Values (cont.) If a function does not return a value, declare its return type void. void sayHello ( void ) { printf(“Hello World!\n”); } sayHello(); Function call: Declaration:

28 28 Next Lecture Reading: D&D (2/e or 3/e) Chapter 5 Sections 5.6 to 5.8 Section 5.12


Download ppt "1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)"

Similar presentations


Ads by Google