Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.

Similar presentations


Presentation on theme: "FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant."— Presentation transcript:

1 FUNCTIONS

2 Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant. 3. Every C program ends with an END word. 4. main() is where the program begins its execution. 5. A line in a program may have more than one statement. 6. A printf statement can generate only one line of output. 7. The closing brace ‘}’ of the main() in a program is the logical end of the program. 8. The purpose of the header file such as stdio.h is to store the source code of a program. 9. Comments cause the computer to print the next enclosed between /* and */ when executed. 10. Syntax errors will be detected by the compiler.

3 Midterm questions (11-20) review 11. Every C program must have at least one user-defined function. 12. Only one function may be named main(). 13. Use of comments reduces the speed of execution. 14. Comments serve as internal documentation for programmers. 15. A comment can be inserted in the middle of a statement 16. In C, we can have comments inside comments. 17. Any valid printable ASCII character can be used in an identifier. 18. All variables must be given a type when they are declared. 19. Declarations can appear anywhere in a program. 20. C treats the variables name and Name to be same.

4 Reviewing Functions What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. printf() causes data to be printed on the screen. Why should you use functions? They save you from repetitious programming (save time and space) Using a function is worthwhile because it makes a program more modular, hence easier to read and easier to change or fix Main program Function AFunction BFunction C B1B2

5 Example Suppose, for example, that you want to write a program that does the following: Read in a list of numbers Sort the numbers Find their average Print a bar graph A function can be thought as a "black box" defined in terms of the information that goes in (its input) and the value or action it produces (its output).

6 Elements of user-defined functions Both function names and variables are considered identifies and therefore they must adhere to the rules for identifiers. Like variables, functions have types (such as int) associated with them. Like variables, function names and their types must be declared and defined before they are used in a program

7 What do you need to know about functions? You need to know how to define them properly How to call them up for use. And how to set up communication between functions. The specifier %s is used to print strings String constants

8 Definition of functions Function definition include function name; function type; list of parameters; local variable declarations function statements a return statement All the six elements are grouped into two parts, Function header (first three elements); Function body (second three elements);

9 Function Arguments

10 Defining a Function with an Argument: Formal Parameters The function definition begins with the following ANSI C function header: You can't use a list of variables of the same type

11 Prototyping and Calling a Function with Arguments We can omit variable names in the prototype: Declaration on an ANSI function prototype: We give ch and num values by using actual arguments in the function call. Consider the first use of show_n_char(). The actual argument can be a constant, a variable, or an even more elaborate expression

12 The Black-Box Viewpoint Taking a black-box viewpoint of show_n_char(), the input is the character to be displayed and the number of times to be repeated. The input is communicated to the function via arguments. This information is enough to tell you how to use the function in main(). The fact that ch, num, and count are local variables private to the show_n_char() function is an essential aspect of the black box approach. If you were to use variables with the same names in main(), they would be separate, independent variables.

13 Returning a Value from a Function with return The function returns the value that was assigned to min Functions can be used as part of an expression

14 Function Types Functions should be declared by type. A function with a return value should be declared the same type as the return value. Functions with no return value should be declared as type void. the following function heading indicates that a defined function takes two type int arguments but that returns a type double value.

15 Function prototypes All identifiers in C need to be declared before they are used. For functions the declaration needs to be before the first call of the function. A full declaration (prototype) includes the return type and the number and type of the arguments. Having the prototype available before the first use of the function allows the compiler to check that the correct number and type of arguments are used in the function call and that the returned value, if any, is being used reasonably.

16 Function prototypes prototype With function prototype Without function prototype

17 Recursion C permits a function to call itself. This process is termed recursion. Recursion often can be used where loops can be used. Note that each level of recursion uses its own private n variable It's vital that a recursive function contain something to halt the sequence of recursive calls

18 Tail Recursion Tail recursion acts like a loop. The recursive call is at the end of the function, just before the return statement. n! = n x (n-1)! Although the recursive call to rfact() is not the last line in the function, it is the last statement executed when n > 0, so it is tail recursion.

19 Recursion and Reversal Write a function that prints the binary equivalent of an integer. putchar( r ? '1' : '0');

20 Recursion Pros and Cons + recursion offers the simplest solution to some programming problems. - some recursive algorithms can rapidly exhaust a computer's memory resources Example: Suppose you use the function call Fibonacci(40). It allocates a variable called n and evokes Fibonacci() twice, creating two more variables called n at the second level of recursion. Each of those two calls generates two more calls, requiring four more variables called n at the third level of recursion, for a total of seven variables. This processes results in a enormous number of allocated variables.


Download ppt "FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant."

Similar presentations


Ads by Google