1 / 21

Introduction to Computer (C Programming)

Introduction to Computer (C Programming). Chapter 6 User-defined Functions . Roadmap. 6.1 Introduction 6.2 Definition of functions 6.3 Function calls …. 6.1 Introduction. C functions are easy to define and use.

andeana
Download Presentation

Introduction to Computer (C 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. Introduction to Computer (C Programming) Chapter 6 User-defined Functions

  2. Roadmap • 6.1 Introduction • 6.2 Definition of functions • 6.3 Function calls • …

  3. 6.1 Introduction • C functions are easy to define and use. • We have used functions in every program that we have discussed so far, such as main, printf and scanf. • A function, such as printf, represents a set of programming steps used to perform a useful operation.

  4. 6.1.1 Need for Functions • A function is similar to a complete program. • Indeed, the programs you have seen up to now have been written as a function, which happens to have the name main. • The difference between a function and a program lies primarily in who or what makes use of it: • A user run a program. • A function is entirely internal to the program.

  5. 6.1.1 Need for Functions cont. • Problem: if we code any program utilizing only main function, the program may become too large and complex. • Solution: if a program is divided into functional parts, then each part may be independently coded and later combined into a single unit.

  6. 6.1.1 Need for Functions cont. • Advantages of this “division” approach: • It facilitates top-down modular programming • The length of a source program can be reduced by using functions at appropriate places. • It is easy to locate and isolate a faulty function for further investigations. • A function may be used by many other programs.

  7. 6.1.1 Need for Functions cont. • Advantages of this “division” approach: • It facilitates top-down modular programming • The length of a source program can be reduced by using functions at appropriate places. • It is easy to locate and isolate a faulty function for further investigations. • A function may be used by many other programs.

  8. 6.1.2 Multi-function Program • A function is a self-contained block of code that performs a particular task. • A function can be treated as a ‘black box’ that takes some data from the main program and return value. • The inner details of operation are invisible to the rest of the program. • All the program knows about a function is: What goes in and what comes out.

  9. 6.1.2 Multi-function Program cont. • Every C program can be designed using a collection of these black boxes known as functions. • Any function can call any other function. In fact, it can call itself. • A ‘called function’ can also call other functions. • A function can be called more than once. In fact, this is one of the main features of using functions.

  10. 6.1.3 Modular Programming • Modular programming is a strategy applied to the design and development of software system. • It is defined as organizing a large program into small, independent program segments called modules. • These modules are separately named and individually callable program units. • These modules are carefully integrated to become a entire software system. In C, each module refers to a function that is responsible for a single task.

  11. 6.1.3 Modular Programming cont. • Characteristics of modular programming: • Each module should do only one thing. • Communications between modules is allowed only by calling module. • A module can be called by one and only one higher module. • No communication can take place directly between modules that do not have calling-called relationship. • All modules are designed as single-entry, single-exit systems using control structures.

  12. 6.2 Definition of functions • A function definition, also known as function implementation shall include the following elements: • Function name • Function type • List of parameters • Local variable declarations • Function statements • A return statement Function Header Function Body

  13. 6.2.1 Function Header • Function header has the following form: return-type name (argument-specifiers) • return-type is the type of value the function returns. • name is the function name. • argument-specifiers is a list, separated by commas, of individual specifications for the argument types. • Example: int getWeekday(int year, int month, int date)

  14. 6.2.1.1 Return type • Return type of a function is the type of the function’s return value. • It is assume to be int if not specified. • It is void if return nothing. • It is a good programming practice to code explicitly the return type, even when it is an integer.

  15. 6.2.1.2 Function names • Similarities between functions and variables: • Both function names and variable names are considered identifiers. • Like variables, function names and their types must be declared and defined before they are used in a program. • We can define functions and use them like any other variables in C programs.

  16. 6.2.1.3 Argument-specifiers • Argument-specifiers forms a parameter List: • It contains the variables that will receive the data sent by the calling program. • It can also be used to send values to the calling programs. • void or empty – no value need to be sent.

  17. 6.2.2 Function Body • Function body has the following form: { Local variables declarations Function statements Return statement } Example: int max(int a, int b, int c){ int maxone; if (a>b){ maxone = a>c?a:c; }else{ maxone = b>c?b:c; } return (maxone); }

  18. 6.2.2.1 The return statement • If a function returns a result, its body must include at least one return statement, which specifies the value to be return. • The paradigmatic form for the return statement is: • return (expression); Where expression is the value to be returned. • return; If the function has no result.

  19. 6.2.2.2 Some notes • When a function reaches its return statement, the control is transferred back to the calling program. • In the absence of a return statement, the closing brace acts as a void return. • A local variable is a variable that is defined and used inside a function.

  20. 6.3 Function calls • A function can be called by simply using the function name followed by a list of actual parameters (or arguments). • Example: maxnum = max(3, 4, 5); int max(int a, int b, int c){ int maxone; if (a>b){ maxone = a>c?a:c; }else{ maxone = b>c?b:c; } return (maxone); }

  21. 6.3 Function calls cont. • A function which returns a value can be used in expressions like any other variable. • Example: value = max(3, 4, 5)*3; printf(“The max value is %d.”, max(3,4,5)); if(max(a,b,c)==a){…} • A function can not be used on the right side of an assignment statement. • A function that does not return any value can be called in to perform certain tasks specified in the function.

More Related