QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Similar documents
QUIZ: What value is stored in a after this

TDDE18 & 726G77. Functions

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Chapter 7 Functions. Now consider a more advanced example:

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Lesson 7. Reading and Writing a.k.a. Input and Output

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3


CSE 230 Intermediate Programming in C and C++ Functions

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

BITG 1113: Function (Part 2) LECTURE 5

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Question 2. [5 points] Given the following symbolic constant definition

Fundamentals of Programming Session 13

M.CS201 Programming language

Object Oriented Design

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Fundamentals of Programming Session 4

Functions and Recursion

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

ENERGY 211 / CME 211. Functions

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

C Functions. Object created and destroyed within its block auto: default for local variables

QUIZ. What is wrong with this code that uses default arguments?

15 FUNCTIONS IN C 15.1 INTRODUCTION

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Fundamentals of Programming & Procedural Programming

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS113: Lecture 4. Topics: Functions. Function Activation Records

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

M1-R4: Programing and Problem Solving using C (JAN 2019)

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CS 170 Java Programming 1. Week 5: Procedures and Functions

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

4. Java language basics: Function. Minhaeng Lee

CHAPTER 4 FUNCTIONS. 4.1 Introduction

LAB 6 FUNCTION PART 1 School of Computer and Communication Engineering Universiti Malaysia Perlis

Functions. Arash Rafiey. September 26, 2017

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

CSE202-Lec#4. CSE202 C++ Programming

Unit 7. Functions. Need of User Defined Functions

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

DSP Mapping, Coding, Optimization

Introduction to Java Applications

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Procedural Programming

CS 376b Computer Vision

Data Structures and Programming with C++

Lecture 12 Modular Programming with Functions

Functions in C C Programming and Software Tools

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CSE 374 Midterm Exam 2/6/17 Sample Solution. Question 1. (12 points, 4 each) Suppose we have the following files and directories:

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

WARM UP LESSONS BARE BASICS

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Programming Language A

QUIZ Friends class Y;

Functions. Arizona State University 1

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Fundamental Concepts and Definitions

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Declaring a 2D Array

A Foundation for Programming

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

LAB: STRUCTURES, ARRAYS,

M e t h o d s a n d P a r a m e t e r s

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

EECS402 Lecture 02. Functions. Function Prototype

Lecture 3: C Programm

Chapter 6: Functions

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

PROGRAMMAZIONE I A.A. 2017/2018

QUIZ. Can you find 5 errors in this code?

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

FORM 2 (Please put your name and form # on the scantron!!!!)

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Computer Science 1 Honors

Lecture 4. Defining Functions

Transcription:

QUIZ Lesson 4 Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

QUIZ Lesson 4 Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged. Exercise 5: Now do the same using a conditional operator? :

QUIZ Lesson 4 What is the difference between relational operators and logical operators? Give 3 examples of each.

QUIZ Lesson 4

QUIZ Lesson 4 Is -42 true or false? What is a more economical way to write if (a!= 0)?

Lesson 5 Functions

Anatomy of a function Local variable (recognized only inside the function) Function header Function body

QUIZ: Write a function that returns the absolute value of its float argument Hint: use an if statement or a conditional operator

A function may have multiple return statements

but any call to a function always returns only one value! //the little function who tried unsigned multiple(unsigned){ } return 42; return 43; return 44; When the first return is encountered during program execution, the function exits, and the value from that return is returned.

actually a void function does not return any value! void no_return (int x){ printf("x is %d\n", x); } Do not confuse a value printed by a function with a value returned by a function!

Parameters vs. arguments Sometimes called formal parameters Sometimes called actual parameters

Conclusion: local variables within a function are distinct from any global variables having the same name, and from any other local variables with the same name within other functions.

To do in notebook for next time: Read and take notes pp.92-108 Answer end-of-chapter quizzes 1 6 Answer end-of-chapter exercise 1

QUIZ Write the declaration (prototype) for a function ssquares that returns the sum of the squares of its two double arguments. Then write the definition.

QUIZ Give an example of how the previous function would be used in the main program.

QUIZ What is the difference between formal and actual parameters (arguments)? Explain using the example of the previous function ssquares.

QUIZ What does this function return?

Why functions?

Two ways to write our functions If the function definition is placed before main, we don t need a protoype!

Recursive functions

How a recursive function works On the way down, five instances of the function are left pending, because their return value is not 1 yet known.

Because the results increase very fast, the most appropriate data type for factorial is unsigned long long 2.4 quintillion = 2.4 10 18

Of course, the range of any data type will be exceeded sooner or later 14.1 quintillion = 14.1 10 18 Correct value is 51.0 10 18 EOL2

QUIZ Write a function with two double arguments, that returns their product, as a float.

QUIZ Define a recursive function.

QUIZ What are the two parts of any recursive function?

Sum of numbers from 1 to n 0

Sum of numbers from 1 to n

More practice with recursive functions Draw the sequence of recursive calls, as we did in prev. examples. What is printed? What would be a better name for foo?

More practice with recursive functions What is printed?

More practice with recursive functions 4 What is printed?

More practice with recursive functions What is printed?

More practice with recursive functions What is printed?

Inline functions int fun(int a, int b) { return (a-b) + (a * b); } inline int fun(int a, int b) { return (a-b) + (a * b); } Generally, the compiler literally copies the code of the inline function into the body of any function that calls it (e.g. main)

Inline functions int fun(int a, int b) { return (a-b) + (a * b); } inline int fun(int a, int b) { return (a-b) + (a * b); } Trade-off between speed and memory!

MS Visual Studio 2012 only allows the keyword inline in C++ programs. In C, we have to use _inline instead

To do in notebook for next time: Read and take notes pp.109-115 Answer end-of-chapter quizzes 7 10 Answer end-of-chapter exercises 3, 4, 5

Homework for Chapter 5, due Wed, Feb.10: Exercises 7, 8, 11 Hint for 11: Multiplication is repeated addition! Not from text: Write a function that takes as arguments the radius and height of a cone and returns its volume. Test function with r = 2.5 and h = 3.5. Every time a function is required, you have to also write the main function, and call your function at least once! Capture a screenshot of both code and output. Give instructor one printout with the entire homework.