Download presentation
Presentation is loading. Please wait.
Published by步帅 龚 Modified over 7 years ago
1
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition
2
Slides are at http://www.comp.nus.edu.sg/~yanhwa/
HELLO! Slides are at
3
Have you submitted your Lab #2?
Do Read qn carefully, plan Give meaningful names for variables (no t1, t2, a, b, c…) Appropriate commenting Need to use functions Add spaces, empty lines appropriately Balance the number of vars created Must do Double/triple check your indentation (gg=G in vim will auto-indent your program nicely!) Add function description, preconditions and postconditions (if any) Do not Add function description elsewhere other than on the top of the function Follow lab guidelines
4
Some tips If else Shorthand notation (Ternary Logic)
return (answer1 > answer2 ? answer1 : answer2); Padding of zeros printf("%06d", zipCode);
5
.bashrc and .vimrc
6
Number, order, type of parameters Function description
Lecture Summary Functions recap Function prototypes Parameter names may be omitted in the prototype, but not their type Number, order, type of parameters Function description Precondition, postcondition A function should perform either computation or I/O, not both.
7
Lecture Summary warning: implicit declaration of function 'f‘ warning: conflicting types for 'f' 1 #include <stdio.h> 2 3 int main(void) { 4 f(100, 7); 5 return 0; 6 } 7 8 void f(int a, int b) { 9 return a*b*b; 10 } Why will this happen? Without function prototype, compiler assumes function f to be an int function when it encounters f(100, 7); in main(). But it later conflicts with void f() A ‘type-less’ function has default return type of int
8
Lecture Summary What is pass-by-value?
9
Short-circuit evaluation (if A && B) (if A || B)
Lecture Summary (5<2) + (6==5) A && B A || B !A == vs = Short-circuit evaluation (if A && B) (if A || B) Boolean flags “is…” “has…” Switch statement
10
Switch statement Lecture Summary
switch ( <variable or expression> ) { case value1: Code to execute if <variable or expr> == value1 break; case value2: Code to execute if <variable or expr> == value2 case value3: Code to execute if <variable or expr> == value3 ... default: Code to execute if <variable or expr> does not equal to the value of any of the cases above }
11
Lab CodeCrunch Lab #2 Have you submitted?
12
while ( condition ) { // loop body } Repeat if a condition is true
Lecture Summary Repeat if a condition is true while ( condition ) { // loop body }
13
Repeat for a certain number of times
Lecture Summary Repeat for a certain number of times Steps: n=1; if (n<=10) { printf(…); n++; Go to step 2 } Exit the loop int n; for (n=1; n<=10; n++) { printf("%3d", n); } Same as “for n 1 to 10 STEP 1”
14
Square-free integer? What’s that?
Lab CodeCrunch Lab #3 Square-free integer? What’s that?
15
Tutorial 3 Q1a The expression is evaluated from left to right: (0 < value), being a relational operation (also called comparison operation), evaluates to True when value = 0.5 The hint will print out 1 because the return value for true is 1 in C
16
Exploration What is a string? %s What is a char? %c max = ?
17
Tutorial 3 Q6 #define FEMALE 0 #define MALE 1 #define ACCEPTABLE 0 …. int body_type(int gender, double wt, double ht) {
18
Tutorial 2 Q7 Modularity: Reuse of codes, top-down design, ease of debugging, ease of editing function without affecting other parts of the program Do not mix input/output with computation
19
Tutorial 2 Q9 Confusing local scope:
20
Functions and call stack https://www.youtube.com/watch?v=jRcll9qY6b 0
Functions call stack Functions and call stack 0
21
What are functions and why they are useful
Summary What are functions and why they are useful Local scope of variables in functions Function prototype, description, precond Selection statements (if-else, boolean true false, switch statements). Neat logic!! Repetition (will be covered more next week)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.