Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Dilshad M. Shahid New York

Similar presentations


Presentation on theme: "Functions Dilshad M. Shahid New York"— Presentation transcript:

1 Functions Dilshad M. Shahid New York University @1998

2 Today Function definition return statement Function prototypes Function invocation Examples

3 Function definition Heart of effective problem solving is problem decomposition Taking a problem and breaking it into small, manageable pieces is critical to writing large programs This is known as “top-down” method of programming

4 Function definition Another motivation is software reusability - using existing functions as building blocks to create new programs

5 Function definition Format return-value-type function-name(parameter_list) { declarations statements return value }

6 Function definition Function-name is any valid identifier return-type is the data type of the result returned to caller a return-type void indicates a function does not return a value if you don’t specify a return-type, the compiler assume int

7 Return statement When a return statement is encountered, execution of function is terminated and control is passed back to the calling environment If the return statement contains an expression, then value of that expression is passed back as well Morever, this value will be converted, if necessary, to the specified type

8 Return statement Some examples are: return; return x; return ++a; return (a*b);

9 Function prototypes Functions have to be declared before they can be used A function prototype tells the compiler: –the number and types that are to be passed to the function –the type of value that is to be returned by the function

10 Function prototypes General form return-value-type function-name(parameter_list); The compiler uses function prototypes to validate function calls Also values passed to functions are properly coerced, if possible

11 Function prototypes If the first line of your function is: void sum_it (int a, int b) { then an easy way to declare your function prototype is copy and paste this line, putting ; instead of { at the end, as in: void sum_it (int a, int b); But the compiler actually ignores the names of the variables in the parameter list so the following would be equally valid: void sum_it (int, int); It’s up to you - whatever you find easiest

12 Function invocation Program execution always begins with main() When program control encounters a function name, the function is called or invoked This means that control passes to that function After the function does its work, program control passes back to the calling environment, which then continues with its work

13 Function invocation Examples getchar(); max(x, y); sum=compute_sum(n);

14 Function Invocation General layout function1 prototype; function2 prototype; main { variable declarations function1 call function2 call statements } function1 { } function2 { }

15 Function invocation Example cleaned up and moved to web page under Programs Discussed in Class


Download ppt "Functions Dilshad M. Shahid New York"

Similar presentations


Ads by Google