kp_holyer
Uploaded by
87 SLIDES
870 VUES
870LIKES

functions

DESCRIPTION

functions in c language

1 / 87

Télécharger la présentation

functions

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. Programming for Problem Solving using C FUNCTIONS

  2. Functions Objectives • To understand the software engineering principles of Structured programming and modularity (top-down development) • To understand the function declaration, function call, and function definition • To understand inter- function communication through parameters • To understand the four basic function designs • To understand how function communicate through parameters • To understand Standard functions • To understand how to use arrays in functions • To understand how to use pointers in functions. • To understand recursion.

  3. Designing Structured Programs • Simple programs studied so far can be solved and understood without too much effort. • Large programs need to be reduced into elementary parts for better understanding. • The principles of top-down design and structured programming dictate that a program should have a main module and its related modules. • Each module can be further divided in to sub modules. • Breaking a complex problem into smaller parts is known as factoring.

  4. Hierarchy of modularization • Module 1,2,3 are sub modules of main module • Module 1a,1b,1c are sub modules of module1 • Module 2a is sub module of module 2 • Module 3a,3b are sub modules of module 3

  5. Modular Programming - Manageable Same book published in several volumes. Easily manageable Huge Book of 3000 pages

  6. Functions in C • Top-down design is implemented by using Functions in C • A Program in C is made up of one or more functions, one and only one of which must be named as main. • The execution of the program always starts and ends with main, but it can call other functions to do specific task. • main ( ) is called by the OS and control returns to the OS after completion of main function.

  7. Structure Chart for a C Program

  8. Functions in C • A function is a section of a program that performs a specific task • Solving a problem using different functions makes programming much simpler with fewer defects • A function receives zero or more pieces of data, operate on them and return at most one piece of data

  9. Advantages of Functions (1 of 2) • Problems can be factored in to understandable and manageable steps(easy to code and debug). • Functions can be developed by different people and can be combined together as one application. • Functions support reusability. That is, once a function is written, it can be called from any other module without having to rewrite the same. This saves time in rewriting the same code.

  10. Advantages of Functions (2 of 2) Work Allotment Application Development

  11. Types of Functions

  12. Types of Functions • Library Functions/ Standard Functions: are those functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. • You just need to include appropriate header files to use these functions. • User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. • It reduces complexity of a big program and optimizes the code.

  13. Function Definition (1 of 4) • A function is always associated with following three steps • Function definition • Function declaration • Function call • Function definition contains the code for a function • Function definition is made up two parts • Function Header • Function body (Compound statement within opening and closing braces)

  14. Function Definition (2 of 4) Function Header • A function header consist of 3 parts • return type • function name • formal parameter list • A semicolon is not used at end of function definition header Function Body • The function body contains local declarations and function statements in between braces. • Local declarations specify the variables needed by the function. • The function statement terminated by a return statement • If the function return type is void, a return statement is optional.

  15. Function Definition (3 of 4) Parameters • Formal parameters are variables that are declared in the header of the function definition • Actual parameters are used in the calling statement or sometimes they may be an expression • Formal and actual parameters must match exactly in type, order, and number. Their names, however, no need to match.

  16. Function Definition (4 of 4) Parameters Formal and actual parameters must match exactly in type, order, and number. Their names, however, need not match. double average(int x,int y); void main() { double sum1; sum1=average(10,20);// calling function } actual parameters x,y formal parameters Execute

  17. Function Declaration (1 of 2) • Function declaration consists only of a function header (no code) • Function declaration header consists of three parts: the return type, the function name and formal parameters • Function declarations are terminated with a semicolon • Declarations are placed in global declaration section before • the main function • Function declaration is also known as function prototype

  18. Function Declaration (2 of 2) The general form of a function Prototype or Declaration

  19. Function Call (1 of 4) • Actual parameters identify the values that are to be sent to called function and must match the formal parameters in type and order • Multiple actual parameters are separated by comma • Function call transfers the control to the function definition (called function) • After execution of the function, it returns the result to the calling function • Function having a return type void can be used only as a stand-alone statement

  20. Function Call (2 of 4) General form Actual parameters return type function-name(name1,name2,name3,……….,namen); Return type of function Each parameter is separated by comma and function call ends with semicolon

  21. Function Call (3 of 4)

  22. Function Call (4 of 4) Examples of Function Calls

  23. How Functions Work? main() User defined function Function call

  24. Sample Program with function

  25. Sample Program with function

  26. Sample Program with function

  27. User Defined Functions • Functions must be both declared and defined. • Function declaration gives the whole picture of the function . • Function declaration must be placed before the function call. • Function declaration consists of: • name of the function, • return type, • type and order of parameters • Function definition contains the code that needs to complete the task

  28. Declaring, Calling and Defining Functions

  29. Basic Function designs (1 of 5) • Basic function design is based on their return value and parameter list • There are four basic designs • 1. Void Functions without Parameter. • Function with no arguments and no return value. • 2. Void Functions with Parameter. • Functions with arguments and no return values. • 3. Non Void Functions without Parameters • Functions with no argument and return values. • 4. Non Void Functions with Parameters • Functions with argument and return values.

  30. Basic Function designs (1 of 5)

  31. Basic Function designs (2 of 5) • 1. Void Functions without Parameters • Void functions without parameters does not receive any parameters and also does not return any value . • This can be used only as a statement because a Void function does not return a value • This cannot be used in an expression • Example : result=greeting(); //error

  32. Basic Function designs (3 of 5) • 2. Void Functions with Parameter • The function that receives parameter but does not return any value • This function takes arguments(parameter list) • It can be used only as statement • It cannot be used in an expression • Example: result=greeting(); //error

  33. Basic Function designs (4 of 5) • 3. Non Void Functions without Parameters • The function returns a value but does not receive parameters • It cannot be used as statement • It can be used in an expression • Example: result=greeting(); //correct

  34. Basic Function designs (5 of 5) • 4. Non Void Function with Parameters • The function receives parameters and return values • It cannot be used as a statement • It can be used in an expression • Example : result=greeting(); //correct

  35. Inter-Function Communication (1 of 5) • The calling and called functions need to communicate to exchange data. • The data flow between the calling and called functions can be divided into three strategies: • a downward flow, • an upward flow, and • a bi-directional flow. • A downward flow from calling function to called function • An upward flow from called function to calling function • A bi-directional flow in both directions

  36. Inter-Function Communication (2 of 5) Data Flow Strategies

  37. Inter-Function Communication (3 of 5) Downward flow • Calling function sends data to called function (one way communication) • Copy of data items are passed from calling function to called function • Called function may change the values passed, but the original values in the calling function remains unchanged • Use call by value mechanism to implement downward flow • Rules: • Use values in function call to pass the data • Use appropriate data types in function parameter list to receive data values • Use parameter identifiers in the called function to access the local copies of the data

  38. Inter-Function Communication (4 of 5) Upward flow • Called function sends data back to the calling function (one way communication) • Data items are returned to the calling function from the called function • Calling function may use the values passed from called function • Original values of called function remains unchanged • Use call by reference or return mechanism to implement upward communication • Rules: • Use &variable name in function call to pass a reference to the variable • Use types * in the function parameter list to receive the variable’s address • Use *parameter name in the function to reference the original variable

  39. Inter-Function Communication (5 of 5) Bi-directional flow • Calling function sends data down to called function • Called function sends data up to calling function during or at the end of the process. • Use call by reference or return mechanism to implement • bi-directional communication • Rules: • Use &variable name in function call to pass a reference to the variable • Use types * in the function parameter list to receive the variable’s address • Use *parameter name in the function to reference the original variable

  40. Inter-Function Communication ‘C’ implementation • Most programming languages have three strategies for Inter-function communication: Pass by value, pass by reference and return • C Language uses two mechanisms for Inter function communication: • Pass by values • return

  41. Inter-Function Communication Downward Communication in C • Pass-by-Value is a perfect solution for communication in downward direction • Two data items are passed from main to the down function. • Called function does not return any data item

  42. Inter-Function Communication An example for Downward Communication

  43. Inter-Function Communication Upward Communication • Return statement provides a mechanism for the return of data items in upward direction flow • This mechanism allows only one data item to be returned to the calling function. • Called function needs to access variables in the calling function for passing multiple data items. But C does not allow direct reference to a variable in the calling function. • The solution to this problem is as follows. • Calling function pass the address of a variable: • Ex: upfun (&x, &y) • Called function declares a pointer variable to receive the address: • Ex: void upfun (int * ax, int * ay). • Called function can then put the data in the address of the variable given by the calling function.

  44. Inter-Function Communication Upward Communication in C

  45. Inter-Function Communication An example for Upward Communication

  46. Inter-Function Communication Bi-directional Communication • Strategy used for upward direction can be applied for communication in both directions with a little difference. • Use indirect reference in both sides of the assignment statement • The variable in the called function is first accessed for retrieving the address variable in the right hand side • Access the same parameter again to store a value in the left hand side

  47. Inter-Function Communication An example for Bi-directional Communication turbo c example10.c also show error simulation

  48. Inter-Function Communication An Exchange Function

  49. Standard Functions (1 of 15) • A Function whose definitions have been written and are available, which can be used in a program. • Declare the standard function that is to be used in the function declaration. • Function declaration for these functions are grouped together and collected in several header files • Instead of using function declarations, standard functions can be included in the header files at top portion of the program • Ex: printf(),scanf()

  50. Standard Functions (2 of 15 ) Library Functions and the Linker

More Related