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

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
C Language.
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
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.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C Programming Lecture 7 Functions. Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing.
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
Guide To UNIX Using Linux Third Edition
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 
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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)
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
User defined functions
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
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.
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.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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 Dilshad M. Shahid New York
Programming Fundamentals Enumerations and Functions.
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.
Chapter 6: User-Defined Functions I
Functions in C Mrs. Chitra M. Gaikwad.
Chapter 5 - Functions Outline 5.1 Introduction
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
User Defined Functions
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
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
Department of Statistics St. Joseph’s College (Autonomous)
Functions Imran Rashid CTO at ManiWeber Technologies.
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 42 Topics Functions That Return a Value
CPS125.
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

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

Review of Structured Programming  Structured programming is a problem solving strategy and a programming methodology that includes the following guidelines: The program uses only the sequence, selection, and repetition control structures. The flow of control in the program should be as simple as possible. The construction of a program embodies top-down design.

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.

#include 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 Sample Function Call

Functions (cont.)  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 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 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 ( parameter 1,..., parameter n ) { 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 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 (cont). /************************************************************************* ** 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