CSE 142 Programming I

Similar documents
CSE 142 Programming I

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

C Programming for Engineers Functions

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Lecture 04 FUNCTIONS AND ARRAYS

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Lecture 9 - C Functions

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

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

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

Technical Questions. Q 1) What are the key features in C programming language?

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Chapter 7 Functions. Now consider a more advanced example:

C Functions. 5.2 Program Modules in C

Functions. Systems Programming Concepts

Lecture 04 FUNCTIONS AND ARRAYS

Computer Science & Engineering 150A Problem Solving Using Computers

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

CSE 230 Intermediate Programming in C and C++ Functions

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

BİL200 TUTORIAL-EXERCISES Objective:

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

CSE123. Program Design and Modular Programming Functions 1-1

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

Chapter 4 Functions By C.K. Liang

Programming for Engineers Functions

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Multiple Choice Questions ( 1 mark)

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

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

Functions. Arash Rafiey. September 26, 2017

(2-2) Functions I H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (January 18, 2019) Washington State University

Dept. of CSE, IIT KGP

CSE / ENGR 142 Programming I

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

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

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

C Program Structures

CS16 Exam #1 7/17/ Minutes 100 Points total

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

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Lecture 5. Functions II. Functions with Arguments. CptS 121 Summer 2016 Armen Abnousi

Writing to and reading from files

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

COMP 208 Computers in Engineering

Chapter 5 C Functions

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Structured Programming. Dr. Mohamed Khedr Lecture 9

What are the most likely declarations of "month" in the old and new versions of the program?

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Functions. (transfer of parameters, returned values, recursion, function pointers).

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

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

Functions. Lecture 6 COP 3014 Spring February 11, 2018

C Programming Language

Computer Programming

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

Functions in C C Programming and Software Tools

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

2. Numbers In, Numbers Out

LAB 6 FUNCTIONS I School of Computer and Communication Engineering

CC112 Structured Programming

n Group of statements that are executed repeatedly while some condition remains true

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Functions. printf( %f cubed is %f,x,cube(x)); return 0; } return parameter type double cube(double var) { return (var*var*var);

Programming and Data Structure

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

Function Call Stack and Activation Records

NCSU ECE 209 Sections 602 Exam 1 Fall September, I have neither given not received unauthorized assistance on this test.

CS101 Introduction to computing Function, Scope Rules and Storage class

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Functions BCA-105. Few Facts About Functions:

This exam is to be taken by yourself with closed books, closed notes, no calculators.

(3-1) Functions II H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (September 5, 2018) Washington State University

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Arithmetic Expressions in C

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Pointers and scanf() Steven R. Bagley

Engineering program development 6. Edited by Péter Vass

BSM540 Basics of C Language

2. Numbers In, Numbers Out

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Assoc. Prof. Dr. Tansu FİLİK

Language comparison. C has pointers. Java has references. C++ has pointers and references

Iteration. CSE / ENGR 142 Programming I. while loops. Chapter 5. Motivating Loops. Loop to Add 5 Numbers 1996 UW CSE H - 1

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Function I/O. Last Updated 10/30/18

Programming in C Quick Start! Biostatistics 615 Lecture 4

A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.

Preview from Notesale.co.uk Page 2 of 79

Transcription:

CSE 142 Programming I Functions Chapter 3 lread it all! 3.1: Reusing program parts 3.2: Built-in math functions 3.3: Top-down design 3.4: Functions with no parameters 3.5: Functions with parameters 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-1 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-2 Control Structures What Functions Give Us Conditionals Functions Loops 3. á 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-3 lbreak the problem up into brain-sized chunks lmore easily reason about what the program what the program will do llet us write code once and use it many times lcentralize changes July, 2000 CSE 142 Summer 2000 Isaac Kunen G-4 Where We re Going to Go A Simple Program 1: Functions with no parameters no return value 2: Functions with parameters no return value 3: Functions with parameters return value (We ll explain parameters and return values) #include <stdio.h> int main(void) { /* produce some output */ /* produce more output */ /* produce even more output */ /* produce final output */ return (0) ; 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-5 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-6 1

What s Wrong With This? lwhat if we wanted to change the number of rows of asterisks? change the number of asterisks in a row? use hyphens instead of asterisks? print the date with each separator? The Big Idea Behind Functions lidentify a sub-problem that must be solved lwrite code to solve the sub-problem once lgive the code a name lnow you can execute all of that code just by calling (or invoking) the function 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-7 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-8 What about our example? Our example rewritten lwe keep printing banner lines over and over again lput those commands in a function lnow we can change the banner style everywhere by changing the code in one place #include <stdio.h> void printbanner(void){ int main(void) { /* produce some output */ printbanner(); /* produce more output */ printbanner(); /* produce even more output */ printbanner(); /* produce final output */ return (0) ; 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-9 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-10 Anatomy of a Simple Function void functionname(void){ /* local variable decls. */ /* code */ 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-11 Our Function void printbanner(void){ /* local variable decls. */ /* code */ 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-12 2

Local Variables leach function can have variables declared inside of it, these are local variables lthese variables do not have any meaning outside of the function lseveral functions can have variables with the same name they are different variables Each function has it s own name space A Silly Example void silly(void){ int foo; scanf( %d, &foo); printf( You entered %d!\n, foo); 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-13 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-14 Shortcomings So Far lno communication from caller to the function The function cannot see variables in main! Fix this with parameters lno communication from the function to the caller Fix this with the return value 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-15 Example With Parameters void printmonth(int month){ switch (month){ case 1: printf( January ); break; case 2: printf( February ); break; /* etc. */ 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-16 New Syntax void functionname(parameter list ){ lthe parameter list is a comma separated list of variables names with their types: (int foo, double bar) (int xval, int yval) How Do We Call This Function? lnow that we have the function printmonth() we want to use it. lwhat will these do? printmonth(1); printmonth(2*2); printmonth(); 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-17 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-18 3

Parameters and Arguments Another Example lparameters act like local variables except that they are initialized with the values that the function was called with lthe values passed in are called arguments #include <stdio.h> void printarea(double length, double width){ double area; area = length * width; printf( Area = %f\n, area); int main(void){ double x, y; printf( Length: ); scanf( %lf, &x); printf( Width: ); scanf( %lf, &y); printarea(x, y); return 0; 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-19 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-20 Cautions! lthe parameters and arguments should match in number, type, and order! lthe book uses different terminology parameter = formal parameter argument = actual parameter lfunctions must be declared before they are used! (We ll see how to get around this in just a bit.) 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-21 Local Variables and Parameters leach time you call a function you get new copies of each local variable old values cannot be carried over! linside a function, local variables and parameters behave exactly the same except: Local variables start uninitialized Parameters are initialized with their arguments 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-22 Return Values la function can return a value to its caller double circlearea(double radius){ return type function name argument list function body return lthe return command terminates and sets the value of the function lif the return type is void, then return takes no expression lif the return type is not void, then return must be given an expression 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-23 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-24 4

Example int square(int number){ return number * number; y = square(2) * PI; More on return la function can only return one value la function can have multiple return statements lwhich one gets used? 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-25 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-26 Example lwrite a function that finds the absolute value of a double: One Function Can Call Another llet s write a function to calculate the area of a washer: 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-27 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-28 Strategy: lthis problem has a sub-problem Calculate the area of a disk Solve the Sub-Problem lfind the area of a disk: lthe total algorithm: Calculate the area of the outer disk Calculate the area of the inner disk Subtract 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-29 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-30 5

Solve the Full Problem lfind the area of the washer: double washerarea(double inside, double outside){ 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-31 How This Interacts With main() lnow we can find the area of a washer int main(void){ double area; area = washerarea(1.0, 3.0); printf( Area: %f\n, area); return 0; 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-32 Abstraction lin this example, main() called washerarea() which called diskarea() to compute the value desired lwhat does main() need to know about how that calculation was carried out? lthis is called abstraction 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-33 main() is a Special Function lmain is a function just like any other lmain is special because it is automatically run when your program starts lwhat is the difference between return and exit()? lwhy does main() return a value? 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-34 Static Call Graph lshows how functions are related main() What Exactly Is Void? lif a function return void, then it passes no value back to its caller washerarea() printf() scanf() lif a function s parameter list is void, then it takes no arguments diskarea() 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-35 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-36 6

Function Prototypes lso far, if we want to use a function we must first provide a complete definition of it lthis is a pain lwe can instead give a prototype of the function, and define the function later Prototype Example double diskarea(double radius); /* we can now use diskarea() */ double washerarea(double out, double in){ return diskarea(out) - diskarea(in); double diskarea(double radius){ return radius * radius * PI; 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-37 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-38 Header Files la header file is a file that contains function prototypes, constant declarations, etc. lstdio.h is a header file 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-39 Built-In Functions lc has some built-in functions Standard library lto use them you must #include the correct header files I/O (printf, scanf, etc.) stdio.h Utility functions (exit, etc.) stdlib.h Math functions (sin, sqrt, etc.) math.h etc., etc., etc. 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-40 printf() and scanf() Revisited lboth printf() and scanf() have return values lprintf() returns an integer: the number of characters output lscanf() returns an integer: the number of variables assigned lhow might you use these? Global Variables lc lets you define global variables: variables that are not in any function. lin this course, they are not allowed! Only local variables are allowed #define constants are not variables lglobal variables have their uses, but more often they are a crutch and contribute to bad style 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-41 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-42 7

Functions Summary lmay take several parameters, or none. lmay return one value, or none. lfunctions help structure a program Perform repeated action easily Help you more easily reason about your program Abstract a service 3. July, 2000 CSE 142 Summer 2000 Isaac Kunen G-43 8