UMBC CMSC 104 – Section 01, Fall 2016

Slides:



Advertisements
Similar presentations
CMSC 104, Version 9/011 Top-Down Design Topics Top-Down Design Top-Down Design Examples The Function Concept Reading Sections 3.9, 3.10.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
Guide To UNIX Using Linux Third Edition
Algorithms IV: Top-Down Design
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Programming With C.
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.
CPS120: Introduction to Computer Science Decision Making in Programs.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
CMSC 104, Version 8/061L17Top-DownDesign.ppt Top-Down Design Topics Top-Down Design Top-Down Design Examples The Function Concept Reading Sections 3.1.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
L061 Algorithms, Part 3 of 3 Topics Top down-design Structure charts Reading Sections , 3.3.
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
User-Written Functions
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Algorithms IV Top-Down Design.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
CSCI 161: Introduction to Programming Function
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
The while Looping Structure
Functions I Creating a programming with small logical units of code.
Chapter 4 void Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
The while Looping Structure
Creating your first C program
Algorithms IV Top-Down Design.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Loops and Simple Functions
Algorithms, Part 3 of 3 Topics Top down-design Structure charts
More Loops Topics Relational Operators Logical Operators for Loops.
Introduction to Computing Lecture 08: Functions (Part I)
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Functions I Creating a programming with small logical units of code.
The while Looping Structure
Functions, Part 2 of 42 Topics Functions That Return a Value
CPS125.
Algorithms, Part 3 of 3 Topics Top down-design Reading
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

UMBC CMSC 104 – Section 01, Fall 2016 Functions I

Notes & Announcements All instructors have been asked by the department to remind you of the following RE: course evaluations: “You will receive an email communication from StudentCourseEvaluations@umbc.edu with a link to the surveys for classes in which you are enrolled that are being evaluated.” We will have time in class to fill out the survey. You may use your own device or the computer in front of you. I don’t know what the day is yet.

Notes & Announcements Due to some miscommunication between Sergey and I, we are removing all point deductions for bad variable names on P4 and P5. Clarification – for simple for loops, using variable names i and j are ok. Outside of this, you must use well named variables. When in doubt, use good names! In general, formatting of submitted projects has been terrible. The formatting guide is posted on the website. You’re expected to follow it. We will be taking off more points for poorly formatted projects starting with Project 7. I’ve posted a .emacs file which will help you to better format your code (assuming you use emacs).

Installing a .emacs file A .emacs file is an emacs configuration file I’ve posted one tailored for C/C++ development on the main page of the course website To install: Create a new file in your home directory called .emacs. Copy the contents of the .emacs file on the website and paste it into the .emacs file you created -OR- From your home directory, run the following command: curl -O http://userpages.umbc.edu/~reicht1/classes/cmsc104/fall16/help/.emacs

Formatting with Visual Studio Code If you’re using Visual Studio Code, you can fix the formatting at any time by right-clicking anywhere and selecting the “Format Document” option. You can also format a subset of the document by selecting it, then right-clicking and selecting “Format Selection”

Top-Down Design If we look at a problem as a whole, it may seem impossible to solve because it is so complex. Examples: writing a tax computation program writing a word processor Complex problems can be solved using top-down design, also known as stepwise refinement, where We break the problem into parts Then break the parts into parts Soon, each of the parts will be easy to do

Advantages of Top-Down Design Breaking the problem into parts helps us to clarify what needs to be done. At each step of refinement, the new parts become less complicated and, therefore, easier to figure out. Parts of the solution may turn out to be reusable. Breaking the problem into parts allows more than one person to work on the solution.

Structured Programs We will use top-down design for all remaining programming projects. This is the standard way of writing programs. Programs produced using this method and using only the three kinds of control structures, sequential, selection and repetition, are called structured programs. Structured programs are easier to test, modify, and are also easier for other programmers to understand.

Another Example Problem: Write a program that draws this picture of a house.

The Top Level Draw the outline of the house Draw the chimney Draw the door Draw the windows Main Draw Outline Draw Chimney Draw Door Draw Windows

Pseudocode for Main Call Draw Outline Call Draw Chimney Call Draw Door Call Draw Windows

Observation The door has both a frame and knob. We could break this into two steps. Main Draw Outline Draw Chimney Draw Door Draw Windows Draw Door Frame Draw Knob

Pseudocode for Draw Door Call Draw Door Frame Call Draw Knob

Another Observation There are three windows to be drawn. Main Draw Outline Draw Windows . . . Draw Window 1 Draw Window 2 Draw Window 3

One Last Observation But don’t the windows look the same? They just have different locations. So, we can reuse the code that draws a window. Simply copy the code three times and edit it to place the window in the correct location, or Use the code three times, “sending it” the correct location each time (we will see how to do this later). This is an example of code reuse.

Reusing the Window Code Main Draw Outline Draw Windows . . . Draw a Window

Pseudocode for Draw Windows Call Draw a Window, sending in Location 1 Call Draw a Window, sending in Location 2 Call Draw a Window, sending in Location 3

Review of Top-Down Design Involves repeatedly decomposing a problem into smaller problems Eventually leads to a collection of small problems or tasks each of which can be easily coded The function construct in C is used to write code for these small, simple problems.

Functions A C program is made up of one or more functions, one of which is main( ). Execution always begins with main( ), no matter where it is placed in the program. By convention, main( ) is located before all other functions. When program control encounters a function name, the function is called (invoked). Program control passes to the function. The function is executed. Control is passed back to the calling function.

Sample Function Call #include <stdio.h> int main ( ) printf is the name of a predefined { function in the stdio library printf (“Hello World!\n”) ; this statement is return 0 ; is known as a } function call this is a string we are passing as an argument (parameter) to the printf function

Functions (con’t) We have used three predefined functions so far: printf scanf getchar Programmers can write their own functions. Typically, each module in a program’s design hierarchy chart is implemented as a function. C function names follow the same naming rules as C variables.

Sample Programmer-Defined Function #include <stdio.h> void PrintMessage ( void ) ; int main ( ) { PrintMessage ( ) ; return 0 ; } void PrintMessage ( void ) printf (“A message for you:\n\n”) ; printf (“Have a nice day!\n”) ;

Examining printMessage #include <stdio.h> void PrintMessage ( void ) ; function prototype int main ( ) { PrintMessage ( ) ; function call return 0 ; } void PrintMessage ( void ) function header printf (“A message for you:\n\n”) ; function printf (“Have a nice day!\n”) ; body } function definition

The Function Prototype Informs the compiler that there will be a function defined later that: returns this type has this name takes these arguments void PrintMessage (void) ; Needed because the function call is made before the definition -- the compiler uses it to see if the call is made properly

The Function Call Passes program control to the function Must match the prototype in name, number of arguments, and types of arguments void PrintMessage (void) ; int main ( ) same name no arguments { PrintMessage ( ) ; return 0 ; }

The Function Definition Control is passed to the function by the function call. The statements within the function body will then be executed. void PrintMessage ( void ) { printf (“A message for you:\n\n”) ; printf (“Have a nice day!\n”) ; } After the statements in the function have completed, control is passed back to the calling function, in this case main( ) . Note that the calling function does not have to be main( ) .

General Function Definition Syntax type FunctionName ( parameter1, . . . , parametern ) { variable declaration(s) statement(s) } If there are no parameters, either FunctionName( ) OR FunctionName(void) is acceptable. There may be no variable declarations. If the function type (return type) is void, a return statement is not required, but the following are permitted: return ; OR return( ) ;

Using Input Parameters void PrintMessage (int counter) ; int main ( ) { int num; printf (“Enter an integer: “) ; scanf (“%d”, &num) ; PrintMessage (num) ; one argument matches the one formal parameter return 0 ; of type int of type int } void PrintMessage (int counter) int i ; for ( i = 0; i < counter; i++ ) printf (“Have a nice day!\n”) ;

Final “Clean” C Code #include <stdio.h> void PrintMessage (int counter) ; int main ( ) { int num ; /* number of times to print message */ printf (“Enter an integer: “) ; scanf (“%d”, &num) ; PrintMessage (num) ; return 0 ; }

Final “Clean” C Code (con’t) /************************************************************************* ** PrintMessage - prints a message a specified number of times ** Inputs: counter - the number of times the message will be ** printed ** Outputs: None /*************************************************************************/ void PrintMessage ( int counter ) { int i ; /* loop counter */ for ( i = 0; i < counter; i++ ) printf (“Have a nice day!\n”) ; }

Good Programming Practice Notice the function header comment before the definition of function PrintMessage. This is a good practice and is required by the 104 C Coding Standards. Your header comments should be neatly formatted and contain the following information: function name function description (what it does) a list of any input parameters and their meanings a list of any output parameters and their meanings a description of any special conditions

Project 7

Reminder: Academic Integrity From the Syllabus: Project Policy / Academic Integrity All projects must be individual efforts. You should never have a copy of someone else's project either on paper or electronically under any circumstance. Also, you should never give a copy of your project, either on paper or electronically, to another student. This also means that you cannot "work" on the project together. Cases of academic dishonesty will be dealt with severely. If your project is turned in by someone else, both you and the person copying your project will receive a 0 for that project. This includes "substantially similar" projects. Furthermore, all parties concerned will have their prior projects checked for cheating. So, if you cheat on Project 4, you can lose all the points from Projects 1 through 3 as well, even though you may have done all the work and just "let" other people copy from you.

Questions?