1 / 19

CSC103: Introduction to Computer and Programming

CSC103: Introduction to Computer and Programming. Lecture No 16. Previous lecture. Pointer fundamental & operator * operator Pointer declaration Pointer type. Today’s lecture outline. Function call by address or pointer Function return value Recursive functions. Function Calls.

hadar
Download Presentation

CSC103: Introduction to Computer and Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC103: Introduction to Computer and Programming Lecture No 16

  2. Previous lecture Pointer fundamental & operator * operator Pointer declaration Pointer type

  3. Today’s lecture outline Function call by address or pointer Function return value Recursive functions

  4. Function Calls • Arguments can generally be passed to functions in one of the two ways: • sending the values of the arguments • sending the addresses of the arguments

  5. Example program 1 – function call by value Memory main() x a 10 10 6504 3205 4350 7505 b y 20 20 change_value() 20 40 Program Output a = 10 b = 20 Go to program x = 20 y = 40 a = 10 b = 20 Press any key to continue …

  6. Example program 1 – function call by address Memory change_value(6505, 7505); main() *x a 10 20 40 3205 6504 4350 7505 b *y 20 change_value() 7505 6504 Program Output a = 10 b = 20 *x =*(6504) = 10 Go to program x = 20 y = 40 a = 20 b = 40 Press any key to continue …

  7. Example program 2-function call by value Go to program

  8. Example program 2-function call by address Go to program

  9. Return value of a function

  10. Points to remember Passing arguments by value is not the most efficient means for programming in C When arguments are passed by value, the called function is unable to modify the original contents of the incoming parameters When arguments are passed by address, the called function is able to modify the original contents of the incoming parameters A function only return a single value

  11. Example program Write a program Write a program in which a user input radius of a circle. The program calculates the area and the perimeter of the circle using a single function

  12. Conclusion If we want that the value of an actual argument should not get changed in the function being called, pass the actual argument by value. If we want that the value of an actual argument should get changed in the function being called, pass the actual argument by reference. If a function is to be made to return more than one value at a time then return these values indirectly by using a call by reference.

  13. Recursive function In C, it is possible for the functions to call themselves A function is called ‘recursive’ if a statement within the body of a function calls the same function

  14. Example program - factorial Go to program

  15. Example program – factorial using recursion Go to program

  16. Cont.

  17. Cont. rec ( 2 ) { int f ; if ( 2 == 1 ) return ( 1 ) ; else f = 2 * rec ( 2 - 1 ) ; return ( f ) ; } main() { …. …. fact = rec(3); printf ( "%d ", fact); } rec ( 3 ) { int f ; if ( 3 == 1 ) return ( 1 ) ; else f = 3 * rec ( 3 - 1 ) ; return ( f ) ; } false false fact = 6 rec ( 1 ) { int f ; if ( 1 == 1 ) return ( 1 ) ; else f = 2 * rec ( 2 - 1 ) ; return ( f ) ; } f = 2 * 1; f = 2 f = 3 * 2; f = 6 true

  18. Example program 2 Write a program • Write a definition of a function that adds n integers using recursion and then return the sum. Prototype of the function is below • intsum_number(int);

More Related