Sei sulla pagina 1di 10

Dale Roberts

CSCI 230
Functions
Declarations
Department of Computer and nformation Science,
School of Science, UPU
Dale Roberts, Lecturer Dale Roberts, Lecturer
IUPUI IUPUI
droberts@cs.iupui.edu droberts@cs.iupui.edu
Dale Roberts
Functions Support Decomposition Functions Support Decomposition
Functions Functions
Category Programmers Duration Size of Code
Trivial Trivial 1 1 1 1- -2 weeks 2 weeks < 500 lines (student homework) < 500 lines (student homework)
Small Small 1 1 - - 3 3 Few Weeks Few Weeks 500 500 - -2000 lines (term projects) 2000 lines (term projects)
Medium Medium 2 2 - - 5 5 Few Months Few Months 2000 2000 - -10000 (research project) 10000 (research project)
Large Large 5 5- -25 25 1 1 - - 3 years 3 years 10,000 10,000- -100,000(current applications) 100,000(current applications)
Very Large Very Large 25 25- -100 100 3 3 - - 5 years 5 years 100,000 100,000 - - 1M (real 1M (real- -time operations) time operations)
Extremely Large > 100 Extremely Large > 100 > 5 years > 5 years >1M (advanced military work) >1M (advanced military work)
Divide and conquer Divide and conquer
Large programs cannot be monolithic Large programs cannot be monolithic
Construct a program from smaller pieces or components. These Construct a program from smaller pieces or components. These
smaller pieces are called modules smaller pieces are called modules
Each piece is more manageable than the original program Each piece is more manageable than the original program
Dale Roberts
!rogram ModuIes in C !rogram ModuIes in C
Functions Functions
Modules in C Modules in C
Programs combine user Programs combine user- -defined functions with library functions defined functions with library functions
C standard library has a wide variety of functions C standard library has a wide variety of functions
Math function, /O, string function, such as Math function, /O, string function, such as printf() printf() scanf() scanf()
Function caIIs Function caIIs
nvoking functions nvoking functions
Provide function name and arguments (data) Provide function name and arguments (data)
Function performs operations or manipulations Function performs operations or manipulations
Function returns results Function returns results
Write function once and call it many times Write function once and call it many times
Function call analogy: Function call analogy:
Boss asks worker to complete task Boss asks worker to complete task
Worker gets information, does task, returns result Worker gets information, does task, returns result
nformation hiding: boss does not know details nformation hiding: boss does not know details
Also called Encapsulation Also called Encapsulation
Dale Roberts
Math Library Functions Math Library Functions
Math Iibrary functions Math Iibrary functions
perform common mathematical calculations perform common mathematical calculations
include <math.h> include <math.h>
gcc compiler requires gcc compiler requires - -lm lm parameter parameter
Format for caIIing functions Format for caIIing functions
unctionName( unctionName( argument argument ); );
f multiple arguments, use comma f multiple arguments, use comma- -separated list separated list
printf( "%.2f", sqrt( 900.0 ) ); printf( "%.2f", sqrt( 900.0 ) );
Calls function Calls function sqrt sqrt, which returns the square root of its argument , which returns the square root of its argument
All math functions return data type All math functions return data type double double
Arguments may be constants, variables, or expressions Arguments may be constants, variables, or expressions
Dale Roberts
Functions Functions
Functions Functions
Modularize a program Modularize a program
All variables declared inside functions are local variables All variables declared inside functions are local variables
Known only in function defined Known only in function defined
Parameters Parameters
Communicate information between functions Communicate information between functions
Local variables Local variables
enefits of functions enefits of functions
Divide and conquer Divide and conquer
Manageable program development Manageable program development
Software reusability Software reusability
Use existing functions as building blocks for new programs Use existing functions as building blocks for new programs
Abstraction Abstraction - - hide internal details (library functions) hide internal details (library functions)
Avoid code repetition Avoid code repetition
Dale Roberts
Function Definitions Function Definitions
Function definition format Function definition format
return return- -value value- -type function type function- -name( parameter name( parameter- -list ) list )
{ {
declarations and statements declarations and statements
} }
Function Function- -name: any valid identifier name: any valid identifier
Return Return- -value value- -type: data type of the result (default type: data type of the result (default int int) )
void void indicates that the function returns nothing indicates that the function returns nothing
An unspecified return An unspecified return- -value value- -type is always assumed by the compiler to be type is always assumed by the compiler to be int int
Parameter Parameter- -list: comma separated list, declares parameters list: comma separated list, declares parameters
A type must be listed explicitly for each parameter, unless the parameter is of type A type must be listed explicitly for each parameter, unless the parameter is of type int int
Declarations and statements: function body (block) Declarations and statements: function body (block)
Variables can be declared inside blocks (can be nested) Variables can be declared inside blocks (can be nested)
Functions can not be defined inside other functions Functions can not be defined inside other functions
Returning control Returning control
f nothing returned f nothing returned
return; return;
or, until reaches right brace or, until reaches right brace
f something returned f something returned
return return expression expression; ;
Dale Roberts
Function !rototypes Function !rototypes
Function prototype Function prototype
Function name Function name
Parameters Parameters what the function takes in what the function takes in
Return type Return type data type function returns (default data type function returns (default int int) )
Used to validate functions Used to validate functions
Prototype only needed if function definition comes Prototype only needed if function definition comes after after
use in program use in program
The function with the prototype The function with the prototype
int maximum( int, int, int ); int maximum( int, int, int );
Takes in 3 Takes in 3 int intss
Returns an Returns an int int
!romotion ruIes and conversions !romotion ruIes and conversions
Converting to lower types can lead to errors Converting to lower types can lead to errors
Dale Roberts
ig. 5.4: fig05_04.c
2 inding the maximum of three integers
include <stdio.h>
4
5 int maximum( int, int, int ); function prototype

int main()
8 {
9 int a, b, c;
0
printf( "Enter three integers: " );
2 scanf( "%d%d%d", &a, &b, &c );
printf( "Maximum is: %d\n", maximum( a, b, c ) );
4
5 return 0;
}

8 unction maximum definition


9 int maximum( int x, int y, int z )
20 {
2 int max = x;
22
2 if ( y > max )
24 max = y;
25
2 if ( z > max )
2 max = z;
28
29 return max;
0 }
Enter three integers: 22 85
Maximum is: 85
Function prototype
(3 parameters)
2 Input vaIues
3 CaII function
4 Function definition
Program Output
Dale Roberts
ampIes ampIes
float plus(float
x,y)
{
float sum; float sum;
sum = x + y; sum = x + y;
return sum; return sum;
} }
plus2(int x,y)
{
int sum;
sum = x + y;
return sum;
}
W The minimal function, called a null function, is dummy(){}
W Again, the return statement can be
return expression; Example: return aa;
return (expression); Example: return ((a > 0)? a : -a));
return;
W n 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)
Dale Roberts
cknowIedgements cknowIedgements
Some eampIes were obtained from the course Some eampIes were obtained from the course
tetbook tetbook

Potrebbero piacerti anche