Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.

Similar presentations


Presentation on theme: "Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module."— Presentation transcript:

1 Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module ( or function at it is called in C) is a small self-contained section of an algorithm. In modular design, each major program step is implemented as an independent program module or subprogram. These modules are then combined to form a complete program. It is much easier to develop and test separate smaller modules than it is to test the entire program at once. In fact, most professional programmers agree that modular design is the best strategy to use to write a complex program.

2 6.2 Functions The C programming language fully supports modular design through the concept of functions. A C function is a self- contained and independent block of statements. Every C program consists of one or more functions. All C functions have equal importance, which means that any C function can call any of the other functions in a program including itself and be called by any other function. The function main() is somewhat special in that program execution begins with the first statement in main(). All C programs must have a function called main().

3 6.3 The Structure of a C Function A C function consists of two parts: a function header and a body. A C function begins with a header statement. The function header specifies the name of the function, the type of value that the function returns, a list of parameters, and the data type of each parameter. The general syntax of the function header is Type function_name(type1 parm1,type2 parm2,…,typeN parmN) function_name is the name given to the function, type is the data type of the value returned by the function, type1 is the data type of the first parameter, parm1 is the data type of the parameter.

4 main() { … volume(x);Function main()... } double volume(double radius) { …Function volume()... }

5 We will show the structure of function as an example. int max (int a, int b, int c)function header { int max_value; max_value = a; if ( b > max_value) max_value = b;function body if ( c > max_value) max_value = c; return (max_value); }

6 6.4 How to Call Function A function can be invoked in two different ways. If the function does not return a value then function is invoked by simply using its name followed by a list of arguments. Function_name(arg1, arg2,…,argN) The arguments are separated by commas and are enclosed in parentheses. The arguments in the function call correspond to parameters in the function definition.

7 We will show how to call function from the main program as an example. main() { int x, y, z, largest; … largest = max3 ( x, y, z); … } int max(int a, int b, int c) { int max;... } Main function Function

8 #include void max3 (int,int,int);/* function prototype */ main() { int x,y,z; x = 10; y = 30; z = 15; max3(x,y,z);/* call function */ } void max3 (int a, int b, int c) { int max; max = a; if (max < b) max = b; if (max < c) max = c; printf(“\n The largest of %d %d %d is %d”, a,b,c, max); }

9 6.5 Function That Return A Value Almost all functions return a value. The only exception to this is a function of type void. The type of value returned by a function by a function is specified in the function header. For example, the function square defined here returns a value of type float For example float square(float x) { … }

10 The return statement provides the mechanism for returning a value to the calling function. The syntax of the return statement is return ( expression ); Where expression is any valid C expression. 6.6 Function Prototypes So that the C compiler can correctly interpret function calls it is necessary to declare each C function before it is used in a program. The C language provides a statement for declaring function called a function prototype. A function prototype informs the compiler about the type of value returned by the function and the type and number of arguments that the function expects. For example, the function prototype for the function max3() is int max3 (int, int, int);

11 6.7 Call by Value A C function is typically passed information from the calling function. The called function uses this information to perform its task and then returns a result to the calling function. Although a function can use the values of the arguments provided by the calling function, it cannot change the value of these arguments. This is because when a function is called the compiler evaluates each argument and makes a copy of the argument. It is this copy that is passed to the function instead of the actual argument itself. This mechanism of passing information to a function is known as call by value. Since the function operates on copies of the arguments, the original arguments in the calling function are not affected by anything that happens within the function.

12 Example: Call by Value #include void change_it(int); main() { int i = 10; printf(“\n value of I before function is called %d”, i); change_it(i); printf(“\n Value of I after function is called %d”, i); } void change_it(int j) { printf(“\n Initial value of J in function %d”, j); j = 2 * j + 5; printf(“\n Final value of J in function %d”, j); }

13 6.8 Local Variable Variable that are declared within the body of a function such as temporary variables for storing intermediate results of calculations are local variables. Local variables are visible only to the function in which they are defined and are invisible to all other functions. Thus local variables can be accessed only from within the function. They cannot be accessed by any other function. Local variables are also called automatic variables since they are automatically created each time the function is called and are destroyed after the function has completed execution.

14 #include void function1 (void); main() { int I = 1; … function1();... } void function1(void) { int I = 10;... }

15 6.9 Global Variables External or global variables, as they are commonly called, are variables that are defined outside a function. Global variables are visible to all functions that follow the definition. The example that follows creates two global variables seed and new_seed. The variable seed is visible to the function main(), random(),and reseed(). The variable new seed is visible to random() and reseed() but not visible to main(), since it was defined after main().

16 int random(void) int reseed(void) int seed;/* global variable */ main() {... } int new_seed;/* global variable */ int random(void) { … } int reseed( void) { … }

17 Any Questions


Download ppt "Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module."

Similar presentations


Ads by Google