Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Kernighan/Ritchie: Kelley/Pohl:
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
A First Book of ANSI C Fourth Edition
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
1 CS161 Introduction to Computer Science Topic #9.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
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.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Module 1: Array ITEI222 - Advance Programming Language.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
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.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 6: User-Defined Functions I
School of EECS, Peking University
User-Defined Functions
Introduction to Functions
One-Dimensional Array Introduction Lesson xx
Review for Final Exam.
Computing Fundamentals
Let’s all Repeat Together
Chapter 6: User-Defined Functions I
Review for Final Exam.
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad

Functions  It allows to structure programs in segments of code to perform individual tasks.  In C++, a function is a group of statements that is given a name, which can be called from some point of the program.  The most common syntax for a function is; Type name (parameter1, parameter2,……..) { Statements } 2ITP by Engr. Bilal Ahmad

Explanation  Type is the type of the value returned by this function  Name is the identifier by which the function will be called  Parameters as many as you would like, each parameter must consist of the type followed by an identifier, with each parameter being separated by a comma. For example int x, here int is the type and x is the identifier.  Statements is the functions body. It is the block of the statements surrounding by the braces { } that specify where the function actually does. 3ITP by Engr. Bilal Ahmad

Example  // function example  #include  using namespace std;  int addition (int a, int b)  {  int r;  r=a+b;  return r;  }   int main ()  {  int z;  z = addition (5,3);  cout << "The result is " << z;  }  The result is ? 4ITP by Engr. Bilal Ahmad

Explanation  The program is divided in two functions;  Addition and main  Remember that no matter the order in which they are defined  A C++ program always start by calling the main function  In fact main is the only function that is called automatically  The code in any other function is only executed if its function is called from the main otherwise the function has no significance 5ITP by Engr. Bilal Ahmad

Example’s Explanation  Lets have some discussion about the example that we have done before. What we did we created the addition function right?  Can you identify the type, name and parameters etc. in the example below; 6ITP by Engr. Bilal Ahmad

What is return?  The final statement you all have seen is the return statement do you have any idea why it is used?  This statement is used to send the control back to the main function so that the function can be used for other processing  In the example the return r with have the value of 8 and than will pass on the control to the main function. 7ITP by Engr. Bilal Ahmad

Can a function be called multiple times within a program?  // function example  #include  using namespace std;   int subtraction (int a, int b)  {  int r;  r=a-b;  return r;  }   int main ()  {  int x=5, y=3, z;  z = subtraction (7,2);  cout << "The first result is " << z << '\n';  cout << "The second result is " << subtraction (7,2) << '\n';  cout << "The third result is " << subtraction (x,y) << '\n';  z= 4 + subtraction (x,y);  cout << "The fourth result is " << z << '\n';  } 8ITP by Engr. Bilal Ahmad

Functions with NO types  You will use void in this case  The syntax is type name (argument1, argument2….) {statements}  Requires the declaration to begin with a type because it is the syntax requirement.  If there is a function that doesn’t need to return a value what will you do? Is there any function where there is no need to return a value. remember the first program that we did to start with the C++ Code! 9ITP by Engr. Bilal Ahmad

Example  // void function example  #include  using namespace std;   void printmessage ()  {  cout << "I'm a function!";  }   int main ()  {  printmessage ();  } 10ITP by Engr. Bilal Ahmad

Quiz  You have 10 minutes maximum to attempt this quiz.  On a blank piece of paper just write your name and roll number and without any other information just write the output of the code.  No copying No cheating  After 10 minutes there will be no marking of your quiz, I am sorry.  The functions will be ended with this quiz, if you need any further information you can read ITP by Engr. Bilal Ahmad

Quiz  #include  using namespace std;  int divide (int a, int b=2)  {  int r;  r=a/b;  return (r);  }  int main ()  {  cout << divide (12) << '\n';  cout << divide (20,4) << '\n';  return 0;  } 12ITP by Engr. Bilal Ahmad

Surprise At the End of the Lecture there will be a surprise for you all. Stay tuned 13ITP by Engr. Bilal Ahmad

Arrays  So now we have started writing functions, which will become a part of our every program.  As C language is an Function Oriented Language so we will be dealing with too many functions, not in this ITP course but OOP and DSA.  Our program tool kit is almost complete but still a very important component is missing and it is called Arrays. This Lecture will be based on Arrays. I will strongly advise you after this Lecture go and read Lecture 11 of CS201-ITP can be found on ITP by Engr. Bilal Ahmad

Why Arrays?  Let us consider an Example to calculate the average of 10 students. At first we will declare 10 variables to store the age of 10 students each and then sum up all the ages and divide them with 10. complicated process isn’t it. If yes than can you suggest any other way?  No because you don’t Know  I know ‘Yes’ but why shall I tell you all?  Acha koi nahi batata hun next slide dekho ap sab 15ITP by Engr. Bilal Ahmad

Arrays Explanation  Array is a special data type. Suppose if you have 100 students and you want to calculate the average instead of declaring 100 variables you use the simple and straight forward arrays.  Declaration is simple Data_type array_name [size]; for example Int ages [10]; 16ITP by Engr. Bilal Ahmad

Structure of Array 17ITP by Engr. Bilal Ahmad

Sample Problem 1  Problem Statement: Write a Program which reads positive integers from the user and store these ones in an array. User can enter a maximum of 100 numbers. Stop taking inputs when user enters -1 18ITP by Engr. Bilal Ahmad

Solution  We have to declare an integer array of size 100  We need to use a loop to get the input from the users  There are two conditions to terminate the loop and they are either user has entered 100 numbers or user entered -1  For and while loop can execute zero or more times whereas do while may execute one or more times  We take an integer z to get the input from the user and small I as a counter so the condition will be (z!-1 && i< 100) 19ITP by Engr. Bilal Ahmad

Program Code  # include  using namespace std;  main ()  {  int c[100];  int i,z;  do  {  int z, i = 0;  cout<<"please enter the number (-1 to endup the input)"<<endl;  cin >> z;  if (z!= -1)  {  c[i]=z;  }  i++;  }  while  (z!=-1 && i< 100);  cout <<"the total number of positive integers entered by user is"<<i - 1;  }  20ITP by Engr. Bilal Ahmad

Surprise  Well it is not any prize for the surprise but it was the Last Lecture to cover the course content, in case if we get some time we will try to have some labs but your course is completed. Read till chapter no 12 of the Book uploaded on  It was a pleasure to have all you in my class and I think so I have done my level best to give you a nice experience in the University. I would like you to write a 1 page document and share all your experiences with me. I will mark it for your assignment as well, it will be a treat for you. Be positive and write whatever you want related to me as a person, course etc. 21ITP by Engr. Bilal Ahmad

 You all are very dear and highly respectful for me and I wish you all the very best of Luck for the future.  You are not going to have any courses with me in the next semester but if I am in the University you will face me in semester 6 to high onwards.  May Allah bless you all “AMEEN”. If you need any help/ advise let me know and I will be there for you, even if I am not in the University you can do so.  Good Luck again and all the best for the examination. I believe that you all have potential and you all will get good grades.  At the end we all are human beings if you are hurt because of me I am sorry for that.  If you want to say any thing for me feel free and I am listening. 22ITP by Engr. Bilal Ahmad