Functions I Creating a programming with small logical units of code.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

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.
Spring Semester 2013 Lecture 5
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 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.
1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Chapter 6: User-Defined Functions I
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
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 
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.
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.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 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.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
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.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
 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, 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.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
User-Written Functions
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
C Language By Sra Sontisirikit
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
User Defined Functions
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
1) C program development 2) Selection structure
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
Introduction to Problem Solving and Programming
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, Part 2 of 42 Topics Functions That Return a Value
CPS125.
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

Functions I Creating a programming with small logical units of code

Structured Programming Structured programming is a problem solving strategy and a programming methodology that includes the following guidelines The flow of control in the program should be as simple as possible The construction of a program should embody top-down design

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

Function Invocation A program is made up of one or more functions, one of which is main( ) Execution always begins with main( ) When program control encounters a function name, the function is called, or invoked Program control passes to the function The function is executed Control is passed back to the calling environment

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

Functions Programmers can write their own functions We have used 3 functions so far printf ( ) scanf ( ) getchar ( ) Programmers can write their own functions

PrintMessage function #include <stdio.h> void PrintMessage (void); main ( ) { PrintMessage ( ); } 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 main ( ) { PrintMessage ( ); function call } void PrintMessage (void) function header printf (“A message for you:\n\n”); function printf (“Have a Nice Day!\n”); body

The function prototype Informs the compiler that there will 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 and number and types of arguments void PrintMessage (void); main ( ) same name no arguments { PrintMessage ( ); }

The Function Definition Control was passed to the function by the function call, so the statements within the function body will 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 ( )

Syntax of a Function Definition type FunctionName (list of formal parameters) { variable declarations statements } Everything before the first brace is the header Everything between the braces is the body

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

The Function Header Comment /********************************************** ** PrintMessage ** prints out “Have a Nice Day” the number ** of times specified by the parameter ‘counter’ *************************************************/ void PrintMessage (int counter) { int i; for (i = 0; i < counter; i++) printf (“Have a nice day\n\n”); }