Download presentation
Presentation is loading. Please wait.
Published byCharity Morrison Modified over 8 years ago
1
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem into manageable parts is called the top-down design. In top-down design, a program is divided into a main module and its related modules (functions ). Each module is in turn divided into submodules until the resulting modules are intrinsic; that is until they are implicitly understood without further division. The top-down design is usually done using a visual representation of the modules know as a structure chart. Main Module Module 1Module 2 Module 1a Module 1b Module 1c Module 2a Module 3a Module 3 Module 3b Calling modules Called modules No communication can take place directly between modules that do not have a calling-called relationship
2
Functions The technique used to pass data to a function is known as parameter passing. Data are passed to a function using one of two techniques: pass by value - copy of the data is made and the copy is sent to the function. pass by reference - sends the memory address of the data rather than a copy. Functions in C A C program is made of one or more functions, one and only one of which must be called main. The execution of the program always starts and ends with main but this function can call other functions to do special tasks. The function main is called by the operating system; main in turn calls other functions. When main is completed, control returns to the operating system. A function receives zero or more pieces of data, operates on them, and returns at most one piece of data. At the same time, a function can have a side effect. Zero or more pieces of data go in Side effects Function At most one piece of data comes out
3
Functions The side effect can involve accepting data from outside the program, sending data out of the program to the monitor or a file, or changing the value of a variable in the calling function. There are several reasons for using functions in C: divide the problem into understandable and manageable steps provide a way to reuse code standard and personal libraries protect data (data local to a function are available only to that function) User-Defined Functions These are functions that are defined by the user. Like any other objects in C, functions must be both declared and defined. The function declaration is done with a prototype statement. The function definition contains the local variables and code needed to complete the task. You use a function by calling it. The name of the function is used in three ways: for declaration, in a call, and for definition.
4
Functions #include int main (void) { /* Prototype Declarations to be seen only by main*/ int multiply ( int num1, int num2); /* Local declarations */ /* Statements */ … product = multiply ( multiplier, multiplicand); … return 0; } int multiply (int num1, int num2) { /* Local declarations */ /* Statements */ … return ( num1 * num2); } Definition is done after the calling function Declaration is coded first Calling is done in the statement section semicolon no semicolon
5
Functions Function Definition The function definition contains the code for the function. It is made up of two parts: return_type function_name ( formal parameter list ) { /* Local Declarations */ /* Statements */ } Function header Function body The function header consists of three parts: the return type - this will be the data type of the one returned value from the function. This is the value returned by the return statement. The function name(must follow the rules of identifiers the function body - contains the local declarations and statements for the function. The function body is terminated with a return statement.
6
Formal parameters are variables that are declared in the header of the function definition. The function’s formal parameter list defines and declares the variables that will contain the data received by the function from the calling function. If the parameter list is empty then use the keyword void. All parameters must indicate the type and name and must be separated by commas. Functions Prototype declaration consists only of the function header; they contain no code. Unlike the header for the function definition, prototype declarations are terminated with a semicolon. In the parameter list the names do not need to be the same in the prototype declaration and the function definition but the types must be the same or you will get a compile time error. The prototype must be placed before the physical call. int average (int x, int y ) { double sum; sum = x + y; return (sum / 2); } Parameter variables x y local variable sum Two values are received from the calling function
7
Functions int main (void) { int multiply ( int multiplier, int multiplicand ); int product; … product = multiply (6, 7 ); … return 0; } int multiply (int x, int y ) { return x + y ; } Prototype declaration 6767 Function definition 42 values copied x y Function call The Function call The actual parameters of the function call identify the values that are to be sent to the called function. They match the function’s formal parameters in type and order in the parameter list. Actual parameters of the function call can be primary expression, binary expression or any expression that reduces to a single value. multiply ( multiply ( 2, 3),1 + 5 );
8
Functions Void functions with no parameters. int main (void ) { void greeting (void); /* statements */ greeting( ); return 0; } void greeting (void ) { printf ( “Hello World”); return ; } Hello world declaration Call Side effect
9
Functions Void functions with parameters. int main (void ) { void printTwo (int, int ); int a = 5; int b = 10; printTwo( a, b); return 0; } void printTwo (int x, int y ) { printf ( “%d %d”, x, y); return ; } 5 10 declaration Call Side effect Nothing is returned to the calling function a b xy Values copied
10
Functions Functions that return a value. int main (void ) { int sqr (int x ); int a; int b; scanf(“%d”, &a ); b = sqr (a); printf(“%d squared: %d\n, a, b); return 0; } int sqr (int x ) { return ( x * x); } Call a b x copied Returned value here declaration
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.