Download CSE 150A Spring 2009 Class Notes: Functions and Decision Making and more Study notes Computer Science in PDF only on Docsity!
CSE 150A – Spring 2009
Instructor: Derrick Stolee
February 4, 2009
- Reusing code
- Less effort, (Why re-invent the wheel!)
- Less chance of errors (using previously debugged code, assuming it was debugged)
- Math library
- Header file to include as preprocessor directive: <math.h>
- List of functions available from this library is given on page 109 of text book
- User defined function - general outline:
return_type function_name( arg_type arg_1, arg_type arg_2, ...) { Declare variables used inside the function that are not arguments (multiple lines ) Executable statements ( could call other functions ) multiple lines return value (if function has a return value) }
- User defined function - itemized explanations:
- Return type: this is the type of the value the function returns (int, double, char. etc.) If the function does not have a returned value, the return type is void.
- Function name: the name by which the function is called (invoked, used). No spaces are allowed in the function name.
- Arguments: List the argument type and name of all values the function needs to perform its task. The type must precede each name separated by a space. If more than one argument is needed, the type-name pair of each value must be separated by a comma. These arguments are enclosed in parenthesis.
- The body of the function starts with an opening brace, {, and ends with the closing brace, }. The local variable declarations, executable statements and return statement are written between these braces.
- Local variables (discussed later) need to be declared here. For the time being, we will say that the function can see (use, or change the value of) only the variables in the argument list of the function. So, any other variable to be used must be declared before it is used.
- After the local variables are declared, the task of the function (math calculations, data sorting, etc.) can be performed using the appropriate statements.
- If the function has a return value, the last line in the function must contain the return state- ment followed by an expression or variable that has the same type named in the first line of the function definition.
- Function prototypes:
- This is the declaration of the user defined functions you have defined within the source code file.
- This comes immediately following the preprocessor directives, before the main function.
- Consists of the first line in the function definition followed by a semicolon!
- Calling a function:
- The function is called by the function name followed by a list of variables that contain the values that match the argument list of the function in number, order and type, (not).
- This comes immediately following the preprocessor directives, before the main function.
- Consists of the first line in the function definition followed by a semicolon!
- If the function returns no value (type void), then no assignment is needed in conjunction with the function call (see ch3sam1.c).
- However, if the function returns a value, and the value is needed in calculations further down the line in the program, than the return value must be stored in a variable of the same type as the return type of the function (see ch3sam4.c).
- Tools for Decision Making
- Relational operators − < <= == >= >! =
- Binary operators: Compares 2 values and/or variables
- Type can be char, double, int.
- Result should be interpreted as a truth value, 0 for false, non-zero for true
- logical operators - A&&B !A A||B
- All but! are binary operators
- Work on logical values (0 or nonzero, false or true)
- Results should be interpreted as a truth value, 0 for false, non-zero for true
- Control Structure for Decision Making
- if Structure: If the logical expression is true (nonzero), the single executable statement OR block of exectuable statements are executed If logical expression is false (zero), the single executable OR block of executable statement are NOT executed. In other words, the logical statement determines if the code following the statement is skipped or not.
if ( logical expression ) executable statement;
OR
if ( logical expression ) { executable statement; executable statement; }
- Compile source code doll2dougsam1.c and run entering the number 9 first. Run a second time entering the number 10. Open the source code and study the if statement structure.
- Compile source code doll2dougsam2.c and run entering the number 9 first. Run a second time entering the number 10. Open the source code and study the if statement structure. What is the difference between the two programs?.
- if/else if/else Structure: If the first logical expression is true (nonzero), the single executable statement OR block of exectuable statements are executed. If the logical expression is false (zero), the program goes to the next else if logical statement and checks that condition The program will execute only the block where either the logical statement is true or there is no logical statement (else only). In other words, the logical statements determine which of several possible directions the program can go in. If there is no ending else, then no statements are executed as default.
if ( logical expression ) { executable statement; executable statement; } else if ( logical expression ) { executable statement; executable statement; } else { executable statement; executable statement; }
- Compile source code doll2dougsam4.c and run entering the number 9 first. Run a second time entering the number 10. Run a third time entering the number 20. Open the source code and study the if else if else statement structure. What is the difference between doll2dougsam3.c and doll2dougsam4.c?
- Compile source code doll2dougsam4 buggy.c and run entering the number 25 first. Is the result correct. Why or why not! How can it be fixed?
- Error checking
- We often want to check to see if a user enters a valid input.
- For this program:
#include <stdio.h> #include <stdlib.h>
int main() { int day, month, year;
printf("Enter the month > "); scanf("%d", &month);
// Check to see if this is a valid month if( ) {
printf("%d is an invalid month!\n"); exit(0); }
// More code.
return 0; }
Write a valid condition to check to see if the input month is valid or not.