Functions, variables, operators, loops Modularity

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Various Methods of Populating Arrays Randomly generated integers.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
True or false A variable of type char can hold the value 301. ( F )
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Program Design and Development
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Python quick start guide
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 ICS103 Programming in C Lecture 8: Functions I.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
CIS199 Test Review 2 REACH.
Information and Computer Sciences University of Hawaii, Manoa
UMBC CMSC 104 – Section 01, Fall 2016
CprE 185: Intro to Problem Solving (using C)
Chapter 6: User-Defined Functions I
Hassan Khosravi / Geoffrey Tien
Decisions Chapter 4.
Functions and Structured Programming
Principles of programming languages 4: Parameter passing, Scope rules
About the Presentations
Algorithm Analysis CSE 2011 Winter September 2018.
Hassan Khosravi / Geoffrey Tien
User-Defined Functions
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Multiple Files Revisited
Local Variables variables which are declared within a
Object Oriented Programming COP3330 / CGS5409
Instructor: Ioannis A. Vetsikas
CS1100 Computational Engineering
6 Chapter Functions.
IFS410 Advanced Analysis and Design
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Conditionals.
Chapter 6 Methods.
C Programming Getting started Variables Basic C operators Conditionals
Chapter 6: User-Defined Functions I
Lecture Notes – Week 2 Lecture-2
Loops.
Fundamental Programming
Multiple Files Revisited
ECE 103 Engineering Programming Chapter 18 Iteration
Introduction to Computing Lecture 08: Functions (Part I)
CSC215 Lecture Control Flow.
Functions, Part 2 of 42 Topics Functions That Return a Value
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Functions, variables, operators, loops Modularity APSC 160 review Functions, variables, operators, loops Modularity September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Variable swap Suppose that var1 and var2 are variables of type int. Which of the following code segments swaps the value of these two variables? int temp = var1; var1 = var2; var2 = temp; int temp = var1; var2 = var1; var1 = temp; var1 = var2; var2 = var1; int temp1 = var1; int temp2 = var2; temp1 = temp2; var2 = var1; September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Operator precedence Assume that the following variable declarations have been made: int a = 16; int b = 4; double c = 1.5; What value is assigned to the variable d by the following statement? double d = c + a * b; 65.0 65.5 65 66 September 08, 2017 Hassan Khosravi / Geoffrey Tien

Division with integers and floating points Assume that the following variable declarations have been made: int a = 16; int b = 4; double c = 1.5; What value is assigned to the variable d by the following statement? double d = b / a; 1 0.0 0.25 4 September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Boolean logic Suppose that variable t is a variable that has a value evaluating to true, and f is a variable that has a value evaluating to false. Which one of the following expressions evaluates to false? t && !f !t && f t || f !(t && f) t || (!f && !t) September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Indentation Consider the following (poorly) indented code segment. What are the values of a, b, and r after this code segment has executed? int r; int a = -5; int b = 6; if (a < 0 || b > 0) r = 1; else r = 2; a = 0; a = 0, b = 6, r = 2 a = 0, b = 6, r = 1 a = -5, b = 6, r = undefined a = -5, b = 6, r = 2 None of the above September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Loops Consider the following code segment: What values do i and j have after this code segment has completed execution? int i = 1; int j = 0; while (i < 5 && j < 4) { j = j + i; i++; } printf("i = %d, j = %d", i, j); i = 4, j = 5 i = 5, j = 5 i = 4, j = 6 i = 5, j = 6 None of the above September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Loops How many times is the printf statement executed in the following C code? int x = 1; while ( x < 15/4 ) { printf("x = %d\n", x); x++; } never once twice three times four or more times (or infinite loop) September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Nested loops Consider the following code segment. What values do count1 and count2 have after this code segment has completed execution? int i; int j; int count1 = 0; int count2 = 0; for (i = 0; i < 3; i++) { count1++; for (j = 1; j < 4; j++) { count2++; } count1=3, count2=8 count1=3, count2=10 count1=2, count2=8 count1=2, count2=9 None of the above September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Modular programming Imagine a 10,000-line program that consists of only one function: main Extremely difficult to debug and maintain Extremely difficult to re-use any part of the code Modularity: A design principle used to manage the complexity of larger systems / programs used in many engineering disciplines in software development, modularity can be implemented using functions break the program into smaller modules (functions) September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Functions Information to function can come from parameters or from an input stream Parameters Input stream data Function Output stream data Return value Information from function can come through a return value or an output stream September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Function parameters Actual parameter Value(s) or variable(s) specified by the function caller Formal parameter Variables found in the signature/header of the function itself Formal parameters must match with actual parameters in order, number, and data type September 08, 2017 Hassan Khosravi / Geoffrey Tien

Calling functions What happens when a function is called Copy parameter values/addresses (if any) from caller to function, regardless of variable names Execute the function. Function ends when we reach any return statement Pass back the answer (if any) via the return statement Destroy all local variables in the function Return control to the caller Finish the rest of the calling statement (after replacing the function call with the return value, if any) September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Function parameters Parameters may be passed by value ("call-by-value") the value of the actual parameter is copied to the formal parameter when the function is called The actual parameters and formal parameters are different variables in memory, even if they are named the same If you change the value of the formal parameter, this does not affect the value of the actual parameter back in the caller's memory September 08, 2017 Hassan Khosravi / Geoffrey Tien

Function parameters and the call stack Example // ... int r = 3; double area = circleArea(r); double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } main memory 3 3 3.1415 3 r pi x radius September 08, 2017 Hassan Khosravi / Geoffrey Tien

Function parameters and the call stack Example // ... int r = 3; double area = circleArea(r); double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } main memory 3 3 3.1415 9.0 r pi sq_r radius September 08, 2017 Hassan Khosravi / Geoffrey Tien

Function parameters and the call stack Example // ... int r = 3; double area = circleArea(r); double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } main memory 3 28.274 r area September 08, 2017 Hassan Khosravi / Geoffrey Tien

Functions and parameters Consider the following code segment Fill in the blanks to show what is output to the screen when the program runs void myFunc(int a, int b) { a = a + 4; b = b – 4; printf("In myFunc a = %d b = %d\n", a, b); } int main() { int a = 5; int b = 7; myFunc(a, b); printf("In main a = %d b = %d\n", a, b); return 0; In myFunc a = ___ b = ___ In main a = ___ b = ___ September 08, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Next class APSC 160 review – arrays, algorithms, searching Labs start next week! Check website for details September 08, 2017 Hassan Khosravi / Geoffrey Tien