17
Using Pre-written C Functions • Need the following information – Name of the function – Types of the input parameters – Return type of the function • Use the necessary include – #include <math.h> for math functions – #include <stdlib.h> for rand function

Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Embed Size (px)

Citation preview

Page 1: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Using Pre-written C Functions

• Need the following information– Name of the function– Types of the input parameters– Return type of the function

• Use the necessary include– #include <math.h> for math functions– #include <stdlib.h> for rand function

Page 2: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Examples of Function Calls

• rand– ranNum = 1 + rand()%99;– ranYearNinties = 1990 + rand()%10;

• math– difference = abs(price-cost);– phi = (1 + sqrt(5))/2;– distance = sqrt(pow(x,2)+pow(y,2));

Page 3: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Conditional Expressions

• Relational Operators– > (greater than)– < (less than)– >= (greater than or equal to)– <= (less than or equal to)– == (equal to – notice the TWO equal signs)– != (not equal to)

Page 4: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Conditional Expressions

• Allow comparison of two primitive types

• Examples– b*b >= 4*a*c– money < 1000– score != 21

• Always evaluate to 1 or 0– 1 represents true– 0 represents false

Page 5: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Complex Boolean Expressions

• Boolean Operators– && (and)– || (or)– ! (not)

• Used to put together conditional expressions

• Examples– (age >= 21) && (age <= 65)– (b != 0) && (a/b > 0)

Page 6: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

And, Or, Not

• and (&&)– Only true if both clauses are true– IT IS INCORRECT TO DO SOMETHING LIKE 21 <=

age <= 65. The proper expression is (21 <= age) && (age <= 65).

• or (||)– True as long as at least one clause is true– Checks conditions in order, from left to right.

• not (!)– Negates value of the original expression

Page 7: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Standard If Statement

• if (<boolean expr>) • stmt1;• else • stmt2;• stmtA;• stmtB;

• Evaluate the boolean expression

• If it is true, execute stmt1

• If it is not, execute stmt2

• Then, in both cases, proceed to stmtA and continue.

Page 8: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Block of Statements• if (<boolean expression>)

{• stmt11;• stmt12;• .• .• stmt1n;• }• else {• stmt21;• stmt22;• .• stmt2m;• }• stmtA;• stmtB;

• Same as before• But if the boolean expression is

true, statements 11 through 1n all get executed in order

• Otherwise, if it is false, statements 21 through 2m all get executed in order

• In all cases, one or the other set of statements get executed, never both, and never neither.

• Afterwards, stmtA is always executed, regardless of the value of the boolean expression

Page 9: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Else-If Clause• if (<boolean expr1>) • <stmts1>• else if (<boolean expr2>) • <stmts2>• else if (<boolean expr3>) • <stmts3>• else• <stmtsn>• stmtA

• Now, we check boolean expressions in order

• When we find the FIRST true one, we execute the statement corresponding to it.

• If none are true, we execute the statement in the else clause

• Then, we continue to stmtA.

Page 10: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Notes about if statement

• All else if and else clauses are optional

• No “else if” or “else” is possible without a preceding and matching “if”.

• A block of statements can be used for any clause, but is not necessary. They can be mixed with clauses that don’t have a block.

• Be very careful about forming boolean expressions. Their order and form matter.

Page 11: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Nested If Statements

• Definition: an if statement inside of ANY clause of an if statement.

• Matching else problem– if no curly braces {} are used, this can occur– two ifs, but only one else.– Rule – the else ALWAYS matches the inner

most if

Page 12: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Matching-Else Problem Example

• if (month == 4)• if (day > 15)• printf("Your taxes are late.\n");• else• printf("File your taxes by 4/15.\n");• If month is NOT 4

– Nothing gets printed• If month is 4 and day is 15 or less

– File your taxes by 4/15 is printed• If month is 4 and day is 16 or more

– Your taxes are late is printed

Page 13: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Forgetting {}

• if (month == 4)• printf(“It is close to tax time.\n”);• printf(“Finish by the 15th.\n”);• else• printf(“It is not tax month.\n”);

• This is a syntax error• Finish by the 15th would always print, and no statement is allowed to start with else.

• Curly braces for the if fix it.

Page 14: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Forgetting {}

• if (month == 4)• printf(“It is close to tax time.\n”);• else• printf(“It is not tax month.\n”);• printf(“Find something else to do.\n”);

• This is perfectly valid.• But, the statement, “Find something else to do” will always print no matter what.

Page 15: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Difference between 1 if and separate ifs

• With one if statement, only one clause of statements can be executed

• With several separate if statements, one after another, there is the potential for multiple if statements to be executed

Page 16: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

Example of separate ifs

• if (grade >= 90)– printf(“A\n”);

• if (grade >= 80)– printf(“B\n”);

• if (grade >= 70)– printf(“C\n”);

• if (grade >= 80)– printf(“D\n”);

• Else– printf(“F\n”);

Page 17: Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the

One Last Note

• Order of Operations of && is greater than ||

• Easiest to just use extra parentheses.