Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.

Similar presentations


Presentation on theme: "Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops."— Presentation transcript:

1 Algorithms and Programming Functions Lecture 28

2 Summary of Previous Lecture while statement for statement break statement Nested loops

3 Summary of C program execution steps

4 We are using Turbo C for editing source code

5 Our Source code is passed to the compiler which checks Errors and translate your source code into machine code

6 Object Code is formed after compilation, which is computer understandable code!

7 Linker Links Object Code formed with the other Library Files..

8 Library Files path settings which you already configured! All the header files including stdio.h are placed here!

9 After Linking, executable file is formed, Loader loads it into memory for further processes…

10 Today’s Topics Algorithms  Functions  Using functions in top-down design Functions in C Language  User defined Functions  Parameters  Return values

11 Functions A named sequence of instructions Also known as: modules, procedures, subroutines,... Give a name to a standard, frequently used sequence of actions Specify ("call") that sequence by name

12 Function Definition Define a function as: { }

13 Example: Inviting Saad to a party Function Definition InviteToParty { dial 9876-5432 say "Hello Saad, it's " sayMyName() say "Would you like to come to my party on 10 July?" say "It's at 1 Ravi Road." say "Great! See you then. Bye Saad" hangUp() }

14 Function Parameters Functions may have parameters They specify variations from call to call So that the same function can do different things Depending on the value of the parameters

15 Function Parameters … For example:  √4 = 2  √36 = 6 Both the above can be thought of as “calls” to the square root function But one returns 2, the other returns 6 Depending on the value of the parameter

16 Function Definition Define a function with parameters as: (,,... ) { }

17 Example: Inviting someone to a party Function Definition inviteToParty ( person, date, place) { ringUp(person) askToParty(date, place) sayGoodbye(person, date) }

18 Example: Inviting someone to a party Function Definition ringUp( person ) { set number to lookUpNumber(person) dial(number) say "hello," say person say "it's" sayMyName() }

19 Example: Inviting someone to a party Function Definition askToParty(date, location) { say " Would you like to come to my party on " say date say "It's at" say location }

20 Example: Inviting someone to a party Function Definition sayGoodbye ( person, date ) { say "Great! See you then. Bye" say person hangUp() }

21 Top Down Design and Functions Bottom Up Design  Determine what simple sequences you will need to solve the problem  Code those sequences from primitives  Build more complex sequences using the simpler ones as "pseudo-primitives"  Continue building increasingly complex sequences  Stop when you have a sequence which solves the entire problem

22 Build simple functions first Then use them as building blocks for more complex functions Example: Juggling Top Down Design and Functions

23 Top-Down and Bottom-Up Design are really two sides of the same coin Example: Getting a Degree - Top-Down  You don’t just walk in and pick up the piece of paper  You do first year, second year, third year (maybe fourth year), then hopefully you can get your degree! Top Down Design and Functions

24 But how do you do first year?  First semester, second semester How do you do first semester?  4 different subjects... How do you do a subject?... Top Down Design and Functions

25 Getting a degree - Bottom-Up Do an interesting-looking subject Do another... Keep doing them until you have enough for a degree... Top Down Design and Functions

26 Obviously you need direction Bottom-up degree-getting strategies might never see you with the right combination of subjects to actually graduate! So elements of Top-down guidance are needed - what are we aiming at? Top Down Design and Functions

27 Functions in C Language Topics  Functions  Parameters  Return values

28 printf() printf and scanf are functions, declared in the file stdio.h  The preprocessor directive (#) directs the compiler that the function which you are looking for is declared in the stdio.h file. #include

29 User-Defined Functions Create your own functions, similar to printf() or scanf() Recall a procedure in an algorithm - a named collection of instructions  InviteToParty  RingUp  MakeToParty A function implements the procedure or function parts of an algorithm.

30 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”

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

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

33 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(void) { sayHello(); return 0; }

34 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(void) { sayHello(); return 0; }

35 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(void) { sayHello(); return 0; }

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

37 /* 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)

38 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); }

39 int main(void) { 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

40 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.

41 int main(void) { 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); }

42 Example: badswap.c Output: 3 5 int main(void) { 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); }

43 Example: badswap.c Output: 3 5 5 3 int main(void) { 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); }

44 Example: badswap.c Output: 3 5 5 3 3 5 int main(void) { 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); }

45 Example: badswap.c Calling function’s environment: a: 3 b: 5 Called function’s environment: a: 5 b: 3 int main(void) { 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); }

46 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:

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

48 /* 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

49 /* 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.

50 /* 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.

51 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:

52 Summary We have studied,  What is a Function?  How to write algorithm for a function?  C Programming functions Parameters Return types Function Call


Download ppt "Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops."

Similar presentations


Ads by Google