Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Introduction to C Programming
An Introduction to Programming with C++ Fifth Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Overview creating your own functions calling your own functions.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Introduction to Methods
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Chapter 6: Functions.
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
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 3: User-Defined Functions I
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
C# Programming Methods.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
User-Defined Functions (cont’d) - Reference Parameters.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Program Development and Design Using C++, Third Edition
User-Written Functions
Suppose we want to print out the word MISSISSIPPI in big letters.
Variables A piece of memory set aside to store data
CSCI 161: Introduction to Programming Function
User-Defined Functions
Chapter 2 - Introduction to C Programming
Value returning Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
CS150 Introduction to Computer Science 1
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
CS150 Introduction to Computer Science 1
Presentation transcript:

Functions Structured Programming

Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and formal parameters Global Variables 2

Introduction Functions break large computing tasks into smaller ones. You have used functions in every program that you have encountered so far. ◦ The getch( ) and cout routines are examples of functions. Also you wrote a special function in each and every program ◦ main function 3

Introduction contd... Functions performs a specific task and is relatively independent of the remaining code ◦ Example :  cout – Write output to the console  cin– Read an user input 4

Defining a Function When defining a function we have to mention : ◦ Function name ◦ The type of value it returns ◦ The arguments it takes void main( ) { } 5 Return type Name Argument List

Function Components ComponentPurposeExample Declaration (prototype) Specifies function name,argument types and return value. Alert compiler that function is coming up later Void func( ); callCauses the function to be executed Func( ); DefinitionThe function itself. Contains the lines of code that constitute the function. Void func( ) { ……. }

Defining a Function – Function Name Every function has a name, in this example it is main ◦ Function names are very similar to variable names. ◦ You have to follow the same rules  Can have alphabetical characters, digits or _  The name must start with a alphabetical character or _  Name has to be short but meaningful etc,. ◦ Ex.  int Sum()  float Average() 7

Defining a Function - Return Type Return type is the data type of the result that the function will send to its caller. If there is no return type we use the keyword void. ◦ Ex.  int main()  void main() 8

Defining a Function - Return Type Contd,. ◦ When we complete the return value; statement we pass the control back to the main method and resume the main method from where we stopped. ◦ When calling a function, you must get the return type from the function to a variable of correct type. ◦ Examples: 1.numbers[i] = readValue(i); 2.float avg; avg = calcAvg(numbers); 9

Defining a Function – Argument List Argument list are the values you pass into the function. Each item in the argument list is called a formal parameter. ◦ Each item in the argument list have a name and a data type. Function will perform some task with these values such as: ◦ Displaying the value ◦ Calculating a value etc,.  Ex.  int Sum(int Num1, int Num2)  float Average(int Num1, int Num2) 10

Function Calling and Execution When calling a function, the values we pass are copied into the formal parameter. Then during the execution time, the function will work with its formal parameter as shown bellow; Calling the Function : int Sum = sum(4,5); Function : int sum(int Num1, Num2) { return (Num1 + Num2); } 11

Function : Example 1 #include void printMessage () { cout<<“I Love C Programming !!\n”; } void main ( ) { printMessage (); } 12 I Love C Programming !! _

Arguments Arguments are usually inputs to a function. When you are calling a function with an argument list you must prepare the set of arguments before you call the function. Example: int numbers[5]; //Read the values using for loop for ( i = 0; i < 5; ++i ) { numbers[i] = readValue(i); } min = calcMin(numbers); 13 Preparing the arguments

Local Variables Look at the following function. 14 int calcMin(int numbers[]) { int i; int min = numbers[0]; for ( i = 1 ; i < 5; ++i ) { if (min > numbers[i]) min = numbers[i]; } return min; }

Local Variables contd,. Inside the function we have declared variables such as int i, int min These are called local variables of a function. They are also called as automatic local variables because they are automatically “created” each time the function is called. Their values are local to the function. 15

Local Variables contd,. The value of a local variable can only be accessed by the function in which the variable is defined. Its value cannot be accessed by any other function. If an initial value is given to a variable inside a function, that initial value is assigned to the variable each time the function is called. 16

Functions - Exercise 1 Write down a function called calcSum which will calculate the Sum of two user given values. 17

Function : Example 2 In a program we need to read 5 integers from the user. The integers must be greater than or equal zero and less than or equal to100. If the inputs are valid we must store them to an array and display the minimum, maximum and the average values. 18

Execution of Example1 Always execution starts with the main method As a result first part of the program will declare variables Next is the for loop. In every iteration we call int readValue(int index) function. ◦ When readValue is called we stop execution of the main function and control is sent to the readValue. ◦ As a result its statements will get executed 19

Formal Parameters in the Argument List When you pass values in an argument list, they are copied into the formal parameters of the function. The function will work with the formal parameters. 20

Exercise 2 Implement a program that will read 5 numbers from the user and store the absolute value of each number into an array. 21