Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 161: Introduction to Programming Function

Similar presentations


Presentation on theme: "CSCI 161: Introduction to Programming Function"— Presentation transcript:

1 CSCI 161: Introduction to Programming Function

2 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection of statements to perform a task Motivation for modular programming: Improves maintainability of programs Simplifies the process of writing programs

3 Functions Function - set of statements collected together and given a name Allow programmer to signify entire set of operations with single name Make programs shorter and simpler May include the computation of a value

4 Defining and Calling Functions
Function call: statement causes a function to execute Function definition: statements that make up a function

5 Function Call e.g. f(x, y); Call
act of executing statements associated with function done by writing name of function, followed by list of expressions enclosed in parenthesis arguments - allow passage of information to function function call is simply an expression

6 Function Return e.g. z = f(x, y); Return
once function completes, returns to program step where call was made and continues may also send back results to calling program returning a value

7 math library #include <cmath>
functions that take value of type double as argument and return value of type double sqrt, exp, log, sin, cos e.g. root = sqrt(3.0); distance = sqrt(x*x + y*y); tangent = sin(x) / cos(x);

8 Function Prototype(Declarations)
name of the function type of each argument and descriptive names type of value that function returns Has form result-type name(argument-specifiers); where result-type – data type of function result name – name of function itself argument-specifiers – argument data types (optionally followed by descriptive names)

9 More on Prototypes e.g. double sqrt(double);
Function sqrt takes one argument of type double and returns a value of type double e.g. double sin(double radians); Name for argument provides further information for programmer using function

10 void data type Keyword void is used to indicate no arguments or no return value e.g. int getNumber(void); void menu(void);

11 Writing Your Own Functions
Two distinct steps 1. Specify function prototype (after the includes, before main) - shows format 2. Provide implementation of function (actual steps involved) - shows details

12 Function Body Block consisting of statement in { } Similar to main
May include variable declarations Return statement - required if function returns a result back to caller return (expression);

13 Celsius to Fahrenheit Prototype double CtoF(double c); Implementation
{ return (9.0/5.0 * c + 32); } Another implementation double f; f = (9.0/5.0 * c + 32); return (f);

14 Main Program for CtoF To call function, could use
f_value = CtoF(30.0); cout << f_value; Or simply use cout << CtoF(30.0); or double c_value = 30.0; f_value = CtoF(c_value);

15 Function Prototypes Ways to notify the compiler about a function before a call to the function: Place function definition before calling function’s definition Use a function prototype (function declaration) – like the function definition without the body Header: void printHeading() Prototype: void printHeading();

16 Prototype Notes Place prototypes near top of program
Program must include either prototype or full function definition before any call to the function – compiler error otherwise When using prototypes, can place function definitions in any order in source file

17 Function Definition Definition includes:
return type: data type of the value that function returns to the part of the program that called it name: name of the function. Function names follow same rules as variables parameter list: variables containing values passed to the function body: statements that perform the function’s task, enclosed in {}

18 Other Parameter Terminology
A parameter can also be called a formal parameter or a formal argument An argument can also be called an actual parameter or an actual argument

19 Parameters, Prototypes, and Function Headers
For each function argument, the prototype must include the data type of each parameter inside its parentheses the header must include a declaration for each parameter in its () void evenOrOdd(int); //prototype void evenOrOdd(int num) //header evenOrOdd(val); //call

20 Example output if each entered value is even or not, terminate when the number entered is -1 Done in class

21 Function Call Notes Value of argument is copied into parameter when the function is called A parameter’s scope is the function which uses it Function can have multiple parameters There must be a data type listed in the prototype () and an argument declaration in the function header () for each parameter Arguments will be promoted/demoted as necessary to match parameters

22 Passing Multiple Arguments
When calling a function and passing multiple arguments: the number of arguments in the call must match the prototype and definition the first argument will be used to initialize the first parameter, the second argument to initialize the second parameter, etc.

23 (Program Continues)


Download ppt "CSCI 161: Introduction to Programming Function"

Similar presentations


Ads by Google