200 likes | 346 Vues
FUNCTIONs . Midterm questions (1-10) review. Every line in a C program should end with a semicolon. In C language lowercase letters are significant . Every C program ends with an END word. main () is where the program begins its execution.
E N D
Midterm questions (1-10) review • Every line in a C program should end with a semicolon. • In C language lowercase letters are significant. • Every C program ends with an END word. • main() is where the program begins its execution. • A line in a program may have more than one statement. • A printf statement can generate only one line of output. • The closing brace ‘}’ of the main() in a program is the logical end of the program. • The purpose of the header file such as stdio.h is to store the source code of a program. • Comments cause the computer to print the next enclosed between /* and */ when executed. • Syntax errors will be detected by the compiler.
Midterm questions (11-20) review • Every C program must have at least one user-defined function. • Only one function may be named main(). • Use of comments reduces the speed of execution. • Comments serve as internal documentation for programmers. • A comment can be inserted in the middle of a statement • In C, we can have comments inside comments. • Any valid printable ASCII character can be used in an identifier. • All variables must be given a type when they are declared. • Declarations can appear anywhere in a program. • C treats the variables name and Name to be same.
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 A Function B Function C B1 B2
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).
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
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. String constants The specifier %s is used to print strings
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);
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
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
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.
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
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 intarguments but that returns a type double value.
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.
Function prototypes Without function prototype With function prototype prototype
Recursion • C permits a function to call itself. This process is termed recursion. • Recursion often can be used where loops can be used. It's vital that a recursive function contain something to halt the sequence of recursive calls Note that each level of recursion uses its own private n variable
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.
Recursion and Reversal Write a function that prints the binary equivalent of an integer. putchar( r ? '1' : '0');
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.