1 review of chapter 3--- flow of control how to specify conditions? relational, equality and...
Post on 19-Dec-2015
221 views
TRANSCRIPT
![Page 1: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/1.jpg)
1
Review of Chapter 3--- Flow of Control
How to specify conditions? Relational, Equality and Logical Operators
Statements Statements: compound statement and empty statement Select among alternative actions
The if and if-else statement The switch statements The conditional Operator
Achieve iterative actions The while statement The for statement The do statement The break and continue statements
Nested Flow of Control
![Page 2: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/2.jpg)
2
Review of Chapter 3
Representation of true and false in C false: represented by any zero value true: represented by any nonzero value
Three types of operators Relational operators: <,>,<=,>= Equality Operators: == and != Logical operators:!, &&, and || These operators yield either the intint value 0
(false) or the intint value 1 (true).
![Page 3: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/3.jpg)
3
Review of Chapter 3
statement Expression statement
An expression followed by a semicolon Compound Statement
A series of declarations and statements surrounded by braces.
Empty StatementA single semicolon.
![Page 4: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/4.jpg)
4
Review of Chapter 3
if and if-else statement
exp is enclosed by parentheses Where appropriate, compound statements
should be used to group a series of statements under the control of a single if expression
An if or if-else statement can be used as the statement part of another if or if-else statement.an else attaches to the nearest if.
if (expr) statement1
elsestatement2
if (expr) statement1
![Page 5: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/5.jpg)
5
Review of Chapter 3
switch statement Evaluate the
switch_cond. Go to the case label
having a constant value that matches the value of the switch_cond. If a match is not
found, go to the default label.
If there is no default label, terminate the switch.
Terminate the switch when a break statement is encountered, or by “falling off the end”.
switch ( switch_cond ){ case case_cond1: statements;
break; // optional
... case case_condn: statements;
break; // optional default:
statements; break;
}Next statement
//optional
![Page 6: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/6.jpg)
6
The switch Statement
switch ( switch_cond ){ case case_cond1: statements;
break; // optional case case_cond2 : statements; case case_cond3 : statements; …../* no break */case case_condi : statements;
break;……case case_condn: statements;
break; // optional ……}Next statement;
![Page 7: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/7.jpg)
7
The switch StatementExample
#include <stdio.h>int main(void){ int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: printf(“default\n”);break; } return 0;}
% gcc switch.c% a.outcase 3case 4case 5
![Page 8: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/8.jpg)
8
The switch Statement
switch ( switch_cond ){ case case_cond1: statements;
break; // optional ……case case_condi : statements;
break;……case case_condn: statements;
break; // optional default:
statements; break;
}Next statement;
![Page 9: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/9.jpg)
9
The switch StatementExample
#include <stdio.h>int main(void){ int x; x= 11; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: printf(“default\n”); break; } return 0;}
% gcc switch.c% a.outdefault
![Page 10: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/10.jpg)
10
The switch Statement
switch ( switch_cond ){ case case_cond1: statements;
break; // optional ……case case_condi : statements;
break;……case case_condn: statements;
break; // optional }Next statement;
![Page 11: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/11.jpg)
11
The switch StatementExample
#include <stdio.h>int main(void){ int x; x= 11; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; } return 0;}
% gcc switch.c% a.out%
![Page 12: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/12.jpg)
12
Review of Chapter 3
switch Statement switch_cond must be of integer type case conditions must be of integer type and
must all be unique
![Page 13: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/13.jpg)
13
Review of Chapter 3
Conditional Operator ?: General form: expr1? expr2: expr3 Semantics:
First, expr1 is evaluated. If it is nonzero (true), then expr2 is evaluated, and
this is the value of the conditional expression as a whole.
If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole.
Precedence: Just above the assignment operators Associativity: Right to left
![Page 14: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/14.jpg)
14
Review of Chapter 3
The while statement
First expr is evaluated. If expr is nonzero (true), then statement is
executed and control is passed back to the beginning of the while loop.
Statement is repeatedly until expr is zero (false)
Then control passes to next statement.
while (expr)statement;
next statement
![Page 15: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/15.jpg)
15
Review of Chapter 3
The for Statement First expr1 is evaluated. Then expr2 is evaluated.
If expr2 is nonzero (true), o then statement is executed,o expr3 is evaluatedo control passes back to the beginning of the for
loop again, except that evaluation of expr1 is skipped.
The process continues until expr2 is zero (false), at which point control passes to next statement.
for(expr1; expr2; expr3){
statement
} next statement
![Page 16: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/16.jpg)
16
Review of Chapter 3
The do Statement
First statement is executed, and expr is evaluated.
If the value of expr is nonzero (true), then control passes back to the beginning of the do statement, and process repeats itself.
When expr is zero (false), then control passes to next statement
do
statementwhile (expr);Next statement
![Page 17: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/17.jpg)
17
Review of Chapter 3
break statement An exit from the innermost enclosing loop or
switch statement
continue statement Causes the current iteration of a loop to
stop and the next iteration to begin immediately.
![Page 18: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/18.jpg)
18
Review of Chapter 3
Statements Expression statement Compound Statement Empty Statement Flow-of-control statement
if, if-elseswitchforwhiledobreak, continue
![Page 19: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/19.jpg)
19
Nested Flow of Control
switch ( switch_exp ){ case constant_exp1: statements;
break; // optional case constant_exp2 : statements;
break; // optional ... case constant_expn: statements;
break; // optional default:
statements; break;
}
//optional
if (expr) statement1
elsestatement2
if (expr) statement1
Expression statement Compound Statement Empty Statement Flow-of-control statement
if, if-else switch for while do break, continue
![Page 20: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/20.jpg)
20
Nested Flow of Control
while (expr)statement;
next statement
for(expr1; expr2; expr3){ statement } next statement
dostatement
while (expr); Next statement
Expression statement Compound Statement Empty Statement Flow-of-control
statement if, if-else switch for while do break, continue
![Page 21: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/21.jpg)
21
End of Review of Chapter 3
Read 3.1 – 3.22
![Page 22: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/22.jpg)
22
Chapter 4:
Functions and Structured Programming
![Page 23: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/23.jpg)
23
Introduction
An important feature of C: Structured Programming Language What is “Structured Programming”?
The construction of a program embodies top-down design.
o Decomposing a problem into smaller problems.
a collection of small problems or task.
![Page 24: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/24.jpg)
24
Introduction
How C supports structured programming? The function construct is used to write code
that solves the small problems that result from the decomposition.
These functions are combined into other functions and ultimately used in main() to solve the original problem.
![Page 25: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/25.jpg)
25
Outline of Chapter 4
How to write a function? Function Invocation Function Definition The return Statement Function Prototypes
More about function Function Declarations from the Compiler’s viewpoint Invocation and Call-by-Value Program Correctness: The assert() Macro
Top-Down Design
![Page 26: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/26.jpg)
26
How to write a function?
An Example: prints a message In the main function, function prn_message is
called to print the following messageA message for you: Have a nice day!
How?How to give the compiler information about
the function?How to pass control to the function?How to specify the behavior of the function?How to get control back from the function?
![Page 27: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/27.jpg)
27
How to write a function?
#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}
![Page 28: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/28.jpg)
28
How to give the compiler information about the function?How to pass control to the function?How to specify the function?How to get the control back?
How to write a function?
#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}
Function prototype:
returns no values to the calling environment and
tells the compiler this function takes no arguments
![Page 29: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/29.jpg)
29
How to write a function?
#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}
Function Invocation:The function is called
How to give the compiler information about the function?How to pass control to the function?How to specify the function?How to get the control back?
![Page 30: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/30.jpg)
30
How to write a function?
#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}
Function Definition:The function is defined
How to give the compiler information about the function?How to pass control to the function?How to specify the function?How to get the control back?
![Page 31: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/31.jpg)
31
How to write a function?
#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}
return statement:Control is passed back to the environment which calls the function
How to give the compiler information about the function?How to pass control to the function?How to specify the function?How to get the control back?
![Page 32: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/32.jpg)
32
How to write a function?
#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}
How to give the compiler information about the function?
Function prototype:How to pass control to the function?
Function InvocationHow to specify the function?
Function DefinitionHow to get the control back?
return statement
![Page 33: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/33.jpg)
33
Function Invocation
#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: Have a nice day!\n”); return;}
A programis made up of one or more functions.One of them is function main()
How control is passed among
functions?
![Page 34: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/34.jpg)
34
Function Invocation
How control is passed among functions?execution begins with main()When program control encounters a function name followed by parentheses,
the function is called, or invoked, which meansProgram control passes to the function
After the function does its work, program control is passed back to the calling environment, where program execution continues.
![Page 35: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/35.jpg)
35
Function Invocation#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}
execution begins with main()When program control encounters a function name followed by parentheses,
the function is called, or invoked, which meanscontrol passes to the function
After the function does its work,
control is passed back to the calling environmentexecution continues.
![Page 36: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/36.jpg)
36
Function Invocation
Function Invocation: A function is called in the following format:
a function name followed by parentheses Examples:
fun_name(); fun_name(argument1, argument2);
A function can be called in another function.
![Page 37: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/37.jpg)
37
Function Invocation
#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24));}
int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}
![Page 38: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/38.jpg)
38
How to write a function?
#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}
How to give the compiler information about the function?
Function prototype:How to pass control to the function?
Function InvocationHow to specify the function?
Function DefinitionHow to get the control back?
return statement
![Page 39: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/39.jpg)
39
Function Definition
Function Definition The C code that describes what a function does
#include <stdio.h>void prn_message(void);int main(void){ prn_message(); return 0;}void prn_message(void){ printf(“A message for you: Have a nice day!\n”); return;}
![Page 40: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/40.jpg)
40
Function Definition — Example#include <stdio.h>void prn_message(int no_of_messages);int main(void){ int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many);}void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}
![Page 41: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/41.jpg)
41
Function Definition
General form of function definitiontype function_name (parameter type list){
declarationsstatements
}
headerbody
void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}
header
body
declarations
statements
Example:
![Page 42: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/42.jpg)
42
Function Definition
General form of function definition (cont’d)
Head: type of the function:
o the type of the value returned by the function.o void: nothing is returned.
type function_name (parameter type list){
declarationsstatements
}
header
void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}
![Page 43: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/43.jpg)
43
Function Definition
General form of function definition (cont’d)
Head:Parameter type list:
o the number and types of the arguments that are passed into the function when it is invoked
o void: no arguments
type function_name (parameter type list){
declarationsstatements
}
header
void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}
![Page 44: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/44.jpg)
44
Function Definition
General form of function definition (cont’d)
Head:Parameters in parameter type list (called as
formal parameters) o Identifiers, can be used within the function bodyo Upon invocation, the value of the argument
corresponding to a formal parameter is used within the function body.
type function_name (parameter type list){
declarationsstatements
}
header
void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}
![Page 45: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/45.jpg)
45
Function Definition — Example#include <stdio.h>void prn_message(int no_of_messages);int main(void){ int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many); return 0;}void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}
![Page 46: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/46.jpg)
46
Function Definition
General form of function definition
Body: specifies the behavior of a function.Upon invocation, the value of the
argument corresponding to a formal parameter is used within the function body.
type function_name (parameter type list){
declarationsstatements
}
headerbody
![Page 47: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/47.jpg)
47
Function Definition
Summary Header:
Type: the type of the value returned by the function.
parameter type list: o the number and types of the arguments that are
passed into the function when it is called.o parameters, called formal parameter :
Identifiers, can be used within the function bodyUpon invocation, the value of the argument
corresponding to a formal parameter is used within the function body.
void: no arguments or no return value Body:specifies the behavior of a function.
type function_name (parameter type list){
declarationsstatements
}
![Page 48: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/48.jpg)
48
How to write a function?
#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}
How to give the compiler information about the function?
Function prototype:How to pass control to the function?
Function InvocationHow to specify the function?
Function DefinitionHow to get the control back?
return statement
![Page 49: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/49.jpg)
49
The return Statement
When a return statement is executed, Program control is passed back to the
calling environment; If an expression follows the keyword return,
the value of the expression is returned.This value is converted, if necessary, to
the type of the function, as specified in the header to the function definition.
![Page 50: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/50.jpg)
50
The return Statement
Two forms of a return statement: return; return expression;
Examples:o return 7;o return (a+b+c);
![Page 51: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/51.jpg)
51
The return Statement#include <stdio.h>void prn_message(int no_of_messages);int main(void){ int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many); return 0;}void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);} No return statement
![Page 52: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/52.jpg)
52
The return Statement
There can be zero or more than one return statements in a function. Zero return statement: “falling off the end”
control is passed back when the closing brace is encountered.
More than one return statementOnce a return statement is executed,
control is passed back.
![Page 53: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/53.jpg)
53
The return Statement#include <stdio.h>int min(int a, int b);int main(void){ int j, k, minimum; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); minimum = min(j,k); printf(“\n The minimum is %d.\n\n”, minimum); return 0;}int min(int a, int b){ if (a<b) return a; else return b;}
![Page 54: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/54.jpg)
54
How to write a function?
#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}
How to give the compiler information about the function?
Function prototype:How to pass control to the function?
Function InvocationHow to specify the function?
Function DefinitionHow to get the control back?
return statement
![Page 55: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/55.jpg)
55
Function Prototype
Functions should be declared before they are used. Function prototype: tells the compiler
The number and type of arguments that are to be passed to the function
The type of the value that is to be returned by the function
main function is specialUsually, the programmer does not supply
a function prototype for main().
![Page 56: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/56.jpg)
56
Function Prototypes
The general form of a function prototype type function_name(parameter type list);
Examples: int max(int a, int b);
type: intparameter type list: int a, int b
![Page 57: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/57.jpg)
57
Function Prototypes
#include <stdio.h>int max(int a, int b);int main(void){ int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0;}int max(int a, int b){ if (a>b) return a; else return b;}
![Page 58: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/58.jpg)
58
Function Prototypes
The general form of a function prototype type function_name(parameter type list); Parameter type list
A comma-separated list of typesIdentifiers are optional
o Identifiers are not used by the compiler;o Purpose: documentation
Example:o void f(char c, int i); is equivalent too void f(char, int);
![Page 59: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/59.jpg)
59
Function Prototypes
#include <stdio.h>int max(int, int);int main(void){ int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0;}int max(int a, int b){ if (a>b) return a; else return b;}
![Page 60: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/60.jpg)
60
Function Prototypes
Why function prototypes: All the compilers need to check the types of
values passed to a function. Values are coerced, where necessary. Example:
double f(double)Function call: f(4)
o 4 is promoted to a double
![Page 61: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/61.jpg)
61
Function Prototypes— Why function prototypes
#include <stdio.h>int fun(int a);int main(void){ double a = 17.2; printf("fun(a)=%d\n", fun(a));
}int fun(int a){ return (a/3*3);}
fun(a)=15
![Page 62: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/62.jpg)
62
Function Prototypes— Why function prototypes
#include <stdio.h>int fun(double a);int main(void){ double a = 17.2; printf("fun(a)=%d\n", fun(a));
}int fun(double a){ return (a/3*3);}
fun(a)=17
![Page 63: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/63.jpg)
63
How to write a function?
#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}
How to give the compiler information about the function?
Function prototype:How to pass control to the function?
Function InvocationHow to specify the function?
Function DefinitionHow to get the control back?
return statement
![Page 64: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/64.jpg)
64
How to write a function?Preprocessing directivesfunction prototype of fucntion 1function prototype of fucntion 1int main(void){ Body of function def}Header of function1 def{
Body of function def}Header of function1 def{
Body of function def}
function invocations
![Page 65: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/65.jpg)
65
How to write a function?
Question: write a code to computer the maximum value by using a function max()? Put its function prototype at the top of the
file Call the function as appropriate in main() Write its function definition at the bottom of
the file
![Page 66: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/66.jpg)
66
The return Statement
#include <stdio.h> Preprocessing directivesFunction prototypeint main(void){ ……. …… Call function ……
}Header of function1 def{
Body of function def
}
int max(int a, int b);int main(void){ int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0;}int max(int a, int b){ if (a>b) return a; else return b;}
C code for calculating the maximum value
![Page 67: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/67.jpg)
67
End of Class on Oct 12Read 4.1 to 4.5
![Page 68: 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1979d/html5/thumbnails/68.jpg)
68