t06AFunctionsDeclarations - Functions Declarations

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Functions Declarations www.kite-legends.blogspot.com Dale Roberts Functions Support Decomposition Functions Category Programmers Duration Size of Code Trivial 1 Small 1-3 Medium 2-5 Large 5-25 applications) applications) Very Large 25-100 Extremely Large > 100 military work) military 1-2 weeks Few Weeks Few Months 1 - 3 years 3 - 5 years < 500 lines (student homework) 500 -2000 lines (term projects) 2000 -10000 (research project) 10,000-100,000(current 10,000-100,000(current 100,000 - 1M (real-time operations) > 5 years >1M (advanced >1M Divide and conquer Divide Large programs cannot be monolithic Large Construct a program from smaller pieces or components. These Construct smaller pieces are called modules smaller Each piece is more manageable than the original program www.kite-legends.blogspot.com Dale Roberts Program Modules in C Functions Modules in C Programs combine user-defined functions with library functions C standard library has a wide variety of functions Math function, I/O, string function, such as printf(), scanf() Math printf() scanf() Function calls Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results Write function once and call it many times Function call analogy: Boss asks worker to complete task Worker gets information, does task, returns result Information hiding: boss does not know details Also called Encapsulation www.kite-legends.blogspot.com Dale Roberts Math Library Functions Math library functions Math perform common mathematical calculations perform #include <math.h> #include gcc compiler requires –lm parameter parameter Format for calling functions FunctionName( argument ); FunctionName( argument ); If multiple arguments, use comma-separated list printf( "%.2f", sqrt( 900.0 ) ); Calls function sqrt, which returns the square root of its argument sqrt All math functions return data type double All double Arguments may be constants, variables, or expressions www.kite-legends.blogspot.com Dale Roberts Functions Functions Modularize a program All variables declared inside functions are local variables Known only in function defined Parameters Communicate information between functions Local variables Benefits of functions Divide and conquer Manageable program development Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) Avoid code repetition www.kite-legends.blogspot.com Dale Roberts Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function-name: any valid identifier Function-name: Return-value-type: data type of the result (default int) Return-value-type: int void – indicates that the function returns nothing An unspecified return-value-type is always assumed by the compiler to be int An int Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter, unless the parameter is of type int type int Declarations and statements: function body (block) Variables can be declared inside blocks (can be nested) Functions can not be defined inside other functions Returning control If nothing returned If return; return; or, until reaches right brace or, If something returned If return expression; return expression www.kite-legends.blogspot.com Dale Roberts Function Prototypes Function prototype Function Function name Function Parameters – what the function takes in Parameters Return type – data type function returns (default int) Return int Used to validate functions Prototype only needed if function definition comes after Prototype after use in program use The function with the prototype int maximum( int, int, int ); Takes in 3 ints Takes int Returns an int Returns int Promotion rules and conversions Converting to lower types can lead to errors www.kite-legends.blogspot.com Dale Roberts 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 /* Fig. 5.4: fig05_04.c Finding the maximum of three integers */ #include <stdio.h> int maximum( int, int, int ); int main() { int a, b, c; printf( "Enter three integers: " ); scanf( "%d%d%d", &a, &b, &c ); printf( "Maximum is: %d\n", maximum( a, b, c ) ); return 0; } /* Function maximum definition */ int maximum( int x, int y, int z ) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } /* function prototype */ 1. Function prototype (3 parameters) 2. Input values 3. Call function 1. Function definition Enter three integers: 22 85 17 Maximum is: 85 Program Output www.kite-legends.blogspot.com Dale Roberts Examples float plus1(float x,y) { float sum; sum = x + y; return sum; } plus2(int x,y) { int sum; sum = x + y; return sum; } • • The minimal function, called a null function, is dummy(){} Again, the return statement can be return expression; Example: return a*a; return (expression); Example: return ((a > 0)? a : -a)); return; • In conclusion, to call a function from another function, you should: 1. Declare the type of the called function, if not a int function 2. Use format of function_name(actual parameters) www.kite-legends.blogspot.com Dale Roberts Acknowledgements Some examples were obtained from the course Some textbook. textbook. www.kite-legends.blogspot.com Dale Roberts ...
View Full Document

This note was uploaded on 12/08/2010 for the course CSE 2017 taught by Professor Kittu during the Spring '10 term at APIIT.

Ask a homework question - tutors are online