Download presentation
Presentation is loading. Please wait.
Published byJason Welch Modified over 5 years ago
1
Introduction to Computing Lecture 08: Functions (Part I)
Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
2
Topics Uses of functions Functions Parameters Return values
3
Uses of functions Top-Down design or divide-and-conquer
It is easier to solve multiple smaller problems instead of one big problem
4
Top-down design Suppose, we want to list parts of an automobile
Automobile self starter Bench seat Brakes Bumper Buzzer Car battery Car doors Stop lamp Clutch Dashboard Exhaust pipe Fuel pump Grille Headlight Muffler Odometer …
5
Top-down design It is easier to first divide the problem into sub problems: Chassis Motor parts Electronic parts … and then, list the parts under sub categories
6
Uses of functions Top-Down design or divide-and-conquer
It is easier to solve multiple smaller problems instead of one big problem It is easier to debug sub problems In case of an error, it is easy to isolate the problem to a specific section of code (a specific function)
7
Uses of functions Top-Down design or divide-and-conquer
It is easier to solve multiple smaller problems instead of one big problem It is easier to debug sub problems A team of programmers can work on different sub problems and join sub problems later to form the solution
8
Uses of functions Top-Down design or divide-and-conquer Code reuse
It is easier to solve multiple smaller problems instead of one big problem It is easier to debug sub problems A team of programmers can work on different sub problems and join sub problems later to form the solution Code reuse
9
Code reuse Suppose we want to find ab and cd power1 = 1;
for (count = b ; count > 0 ; count--) power1 *= a; power2 = 1; for (count = d ; count > 0 ; count--) power2 *= c;
10
Code reuse Suppose we want to find ab and cd power1 = power(a,b);
power2 = power(c,d); No need to write the same code each time if we use functions We can also use this function in another program, either simply by copy-paste or adding it to a user defined library as we will see later
11
User-Defined Functions
Can create your own functions, similar to printf() or sqrt() Implements the subroutine/ procedure/ module/ function definitions of an algorithm
12
Writing User-defined Functions
Need to specify: the name of the function its parameters (input arguments) what it returns (output arguments) block of statements to be carried out when the function is called The block of statements is called the “function body”
13
Writing User-defined Functions
type-specifier function_name(parameter list) { body of function } type-specifier is the type of value the function returns using the return statement Can be any valid type If no type specified, assumes int Can be void -> does not return anything
14
Example: hello1.c Prints a simple greeting. procedure sayHello {
output “Hello World!” } Main Program do procedure sayHello
15
Example: hello1.c #include <stdio.h> /*
* 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
16
Function definition Function call
Example: hello1.c #include <stdio.h> /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } * Call a function which prints a * simple greeting. int main() sayHello(); return 0; Function definition Function call
17
Function name Function body
Example: hello1.c #include <stdio.h> /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } * Call a function which prints a * simple greeting. int main() sayHello(); return 0; Function name Function body
18
Return type Formal Parameter List
Example: hello1.c #include <stdio.h> /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } * Call a function which prints a * simple greeting. int main() sayHello(); return 0; Return type Formal Parameter List
19
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
20
Parameters (aka Arguments)
Example: badsort.c /* Print two numbers in order. */ void badSort ( int a, int b ) { if ( a > b ) printf("%d %d\n", b, a); } else printf("%d %d\n", a, b); Parameters (aka Arguments)
21
Old style. Example: badsort2.c /* Print two numbers in order. */
void badSort ( a, b ) int a; int b; { if ( a > b ) printf("%d %d\n", b, a); } else printf("%d %d\n", a, b); Old style.
22
Example: badsort.c /* Print two numbers in order. */
void badSort ( int a, int b ) { if ( a > b ) printf("%d %d\n", b, a); } else printf("%d %d\n", a, b);
23
Formal parameters Actual parameters
Example: badsort.c Formal parameters /* Print two numbers in order. */ void badSort ( int a, int b ) { if ( a > b ) printf("%d %d\n", b, a); } else printf("%d %d\n", a, b); Actual parameters int main() { int x = 3, y = 5; badSort ( 10, 9 ); badSort ( y, x + 4 ); return 0; }
24
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 This type of function call is called call-by-value
25
Example: badswap.c int main() { int a = 3, b = 5;
/* 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); } int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); return 0; }
26
Example: badswap.c Output: 3 5 int main() { int a = 3, b = 5;
/* 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); } int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); return 0; } Output: 3 5
27
Example: badswap.c Output: 3 5 5 3 int main() { int a = 3, b = 5;
/* 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); } int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); return 0; } Output: 3 5 5 3
28
Example: badswap.c Output: 3 5 5 3 int main() { int a = 3, b = 5;
/* 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); } int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); return 0; } Output: 3 5 5 3
29
Called function’s environment: Calling function’s environment:
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); } int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); return 0; } Called function’s environment: Calling function’s environment: a: 5 b: 3 a: 3 b: 5
30
Parameters (cont.) If a function does not take parameters, declare its formal argument list void or do not write anything. void sayHello ( void ) { printf(“Hello World!\n”); } Declaration: void sayHello () { printf(“Hello World!\n”); } Also valid: Function call: sayHello();
31
Parameters (cont.) The number of actual arguments used in a call to a function must be the same as the number of formal parameters listed in the function definition The order of arguments in the lists determines correspondence. The first actual argument corresponds to the first formal parameter, and so on.
32
Parameters (cont.) Each actual argument must be of a data type that can be assigned to the corresponding formal parameter
33
Functions – “Do”s and “Don’t”s
DO use a function name that describes the purpose of the function DON'T pass values to a function that it doesn't need DON'T try to pass fewer (or more) arguments to a function than there are parameters. In C programs, the number of arguments passed must match the number of parameters
34
Notice there can be multiple return statements
Return from a Function When the last statement is executed void sayHello ( void ) { printf(“Hello World!\n”); } When a return statement is encountered int max (int a, int b) if(a>b) return a; return b; Notice there can be multiple return statements
35
Return Values Values are returned by copying a value specified after the return keyword All functions, except those of type void, return a value
36
Example: maxmin.c Return type int max (int a, int b) { int result;
/* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) result = a; } else result = b; return result;
37
The value of the expression max(7,5) is the integer 7.
Example: maxmin.c /* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) result = a; } else result = b; return result; For example: The value of the expression max(7,5) is the integer 7.
38
Example: maxmin.c This style okay. int max (int a, int b) {
/* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) result = a; } else result = b; return result;
39
Example: maxmin.c Version 2: int max (int a, int b) { if (a > b)
/* Returns the larger of two numbers. */ int max (int a, int b) { if (a > b) return a; } else return b; Version 2:
40
Example: maxmin.c Version 3: Note:
/* Returns the larger of two numbers. */ int max (int a, int b) { if (a > b) b = a; } return b; Version 3: Note: Changing the value of b does not affect the actual parameter in the function call.
41
Example: maxmin.c Or you can write #include <stdio.h>
/* Returns the larger of two numbers. */ int max (int a, int b) { if (a > b) return a; } else return b; /* Returns the smaller of two numbers. */ int min (int a, int b) { if (a < b) return a; } else return b; int main() { int maxOfMins = max(min(1,2), min(3,4)); printf("%d\n", maxOfMins); return 0; } Or you can write printf("%d\n", max(min(1,2), min(3,4)));
42
Return Values (cont.) If a function does not return a value, declare its return type as void Declaration: void sayHello ( void ) { printf(“Hello World!\n”); } Function call: sayHello();
43
Return Values (cont.) As long as a function is not declared as void, it can be used as an operand in any valid C expression x = power(y); if(max(x,y) > 100) printf(“greater”);
44
Return Values (cont.) However, a function can not be the target of an assignment swap(x,y) = 100; /* incorrect statement */ A function declared as void can not be used in any expression void f(); ... t = f(); /* incorrect */
45
Return Values (cont.) If you do not assign the return value of a function it is lost int multiply(int a, int b); int main() { x = multiply(5,4); printf(“%d”, multiply(2,3)); multiply(8,3); /* lost */ }
46
Functions – “Do”s and “Don’t”s
DON'T try to return a value that has a type different from the function's type DON'T let functions get too long. If a function starts getting long, try to break it into separate, smaller tasks DO limit each function to a single task DON'T have multiple return statements if they aren't needed. You should try to have one return when possible; however, sometimes having multiple return statements is easier and clearer
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.