Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.

Similar presentations


Presentation on theme: "Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI."— Presentation transcript:

1 Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI

2 Dale Roberts Functions Support Decomposition Functions CategoryProgrammersDurationSize of Code Trivial 11-2 weeks< 500 lines (student homework) Small 1 - 3Few Weeks500 -2000 lines (term projects) Medium 2 - 5Few Months2000 -10000 (research project) Large 5-251 - 3 years10,000-100,000(current applications) Very Large 25-1003 - 5 years100,000 - 1M (real-time operations) Extremely Large > 100> 5 years>1M (advanced military work) Divide and conquer Large programs cannot be monolithic Construct a program from smaller pieces or components. These smaller pieces are called modules Each piece is more manageable than the original program

3 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() 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

4 Dale Roberts Math Library Functions Math library functions perform common mathematical calculations #include #include gcc compiler requires –lm parameter Format for calling functions FunctionName( argument ); FunctionName( argument ); If multiple arguments, use comma-separated list printf( "%.2f", sqrt( 900.0 ) ); printf( "%.2f", sqrt( 900.0 ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions

5 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

6 Dale Roberts Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function-name: any valid identifier Return-value-type: data type of the result (default int ) void – indicates that the function returns nothing An unspecified return-value-type is always assumed by the compiler to be int Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter, unless the parameter is of 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 return; or, until reaches right brace If something returned return expression ;

7 Dale Roberts Function Prototypes Function prototype Function name Parameters – what the function takes in Return type – data type function returns (default int ) Used to validate functions Prototype only needed if function definition comes after use in program The function with the prototype int maximum( int, int, int ); Takes in 3 int s Returns an int Promotion rules and conversions Converting to lower types can lead to errors

8 Dale Roberts 1/* Fig. 5.4: fig05_04.c 2 Finding the maximum of three integers */ 3#include 4 5int maximum( int, int, int ); /* function prototype */ 6 7int main() 8{8{ 9 int a, b, c; 10 11 printf( "Enter three integers: " ); 12 scanf( "%d%d%d", &a, &b, &c ); 13 printf( "Maximum is: %d\n", maximum( a, b, c ) ); 14 15 return 0; 16} 17 18/* Function maximum definition */ 19int maximum( int x, int y, int z ) 20{ 21 int max = x; 22 23 if ( y > max ) 24 max = y; 25 26 if ( z > max ) 27 max = z; 28 29 return max; 30} Enter three integers: 22 85 17 Maximum is: 85 1. 1. Function prototype (3 parameters) 2. Input values 3. Call function 4. 4. Function definition Program Output

9 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)

10 Dale Roberts Acknowledgements Some examples were obtained from the course textbook.


Download ppt "Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI."

Similar presentations


Ads by Google