2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang

Slides:



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

User Defined Functions
C Language.
Chapter Five Functions
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
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
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
Functions in C Computer Programming(1)- 1090CS Manesh T
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of 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.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
User defined functions
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
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.
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.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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
Chapter 6: User-Defined Functions I
Lecture 7: Repeating a Known Number of Times
Functions, Part 2 of 2 Topics Functions That Return a Value
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.
CSCI 161: Introduction to Programming Function
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
The while Looping Structure
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
User Defined Functions
Functions I Creating a programming with small logical units of code.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
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
Fundamental Programming
Department of Statistics St. Joseph’s College (Autonomous)
Functions Imran Rashid CTO at ManiWeber Technologies.
More Loops Topics Counter-Controlled (Definite) Repetition
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
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang Functions 1 2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang [Based on dblock spr’06lecture, slightly modified]

Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments Reading Sections 5.1 - 5.8

Structured Programming uses only the sequence, selection, and repetition control structures. keeps flow of control in the program as simple as possible. No goto statements

Top-Down Design repeatedly decompose problems into smaller problems produces a collection of small tasks that can be easily coded can use functions to implement each task.

Other Design Paradigms bottom-up design modular design object-oriented design modern software engineering uses a mixture of design paradigms

Functions A C program has one or more functions main() is a function, so is printf() Execution always begins with main( ) When function is called (invoked) program control passes to the function, the function is executed, control is returns 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) some predefined functions we have used: printf() scanf() sqrt() Programmers can write their own functions. Typically, each module in a program’s design hierarchy chart is implemented as a function.

A 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 (or elsewhere) 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 returns to the calling function. Note that the calling function does not have to be main( ) .

Function Definition Syntax type functionName ( parameter1, . . . , parametern ) { variable declaration(s) statement(s) } If there are no parameters, either functionName( ) OR functionName(void) 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 function header comments are very useful. header comments should contain: function name function description (what it does) list of input parameters and their meanings list of output parameters and their meanings description of any special conditions