Fucntions in C++ Malik Jahan Khan

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

Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Object Oriented Programming Spring COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
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.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
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.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
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.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Programming Principles II Lecture Notes 3.1 Void Functions Andreas Savva.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Lecture 36 OOP The STL Standard Template Library
Pointers What is the data type of pointer variables?
Chapter Topics The Basics of a C++ Program Data Types
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Overloaded Constructors and Multiple Classes
C++ Programming: CS150 For.
Basic Elements of C++.
School of EECS, Peking University
Arrays Part-1 Armen Keshishian.
Multi-dimensional Array
User-Defined Functions
Programming fundamentals 2 Chapter 2:Function
Basic Elements of C++ Chapter 2.
Lecture 8 – 9 Arrays with in a class
User-defined Functions
Introduction to Functions
One-Dimensional Array Introduction Lesson xx
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
FUNCTIONS& FUNCTIONS OVERLOADING
Value returning Functions
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
User-defined Functions
CS150 Introduction to Computer Science 1
Lec 14 Oct 23, 02.
A function with one argument
Chapter 6: User-Defined Functions I
The Function Prototype
CS150 Introduction to Computer Science 1
CHAPTER 2 Arrays and Vectors.
CS150 Introduction to Computer Science 1
CHAPTER 2 Arrays and Vectors.
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
CS1201: Programming Language 2
CS-161 Computer Programming Lecture 15 & 16: Arrays II
TOPIC: FUNCTION OVERLOADING
Introduction to Algorithms and Programming COMP151
Introduction to Functions
Introduction to Algorithms and Programming COMP151
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Functions Chapter No. 5.
Presentation transcript:

Fucntions in C++ Malik Jahan Khan Functions in C++ Malik Jahan Khan Malik Jahan Khan

Fucntions in C++ Functions A function groups a number of program statements into a unit and gives it a name Then this unit may be called from other program parts The most important reason to use functions is to organize the program conceptually Another reason is to reduce program size by putting repeated parts of program into functions Dividing the program into functions is the key principle of structured programming OOP has better principles to organize the program conceptually [We will learn them] Malik Jahan Khan

Functions (Cont.) Function Prototype void main() { … Function call } Function Declarator Function Body Function Definition

Functions (Cont.) - Example #include<iostream.h> void starline(); //Prototype void main() { starline(); //Function Call cout<<endl<<“Object Oriented Programming”<<endl; cout<<“OOP is an inspiration from “<<endl<< “real world objects. “<<endl<< “World is made up of objects”<<endl; } void starline() //Function Definition for(int x=1;x<=20;x++) cout<<“*”; Output: ******************** Object Oriented Programming OOP is an inspiration from real world objects. World is made up of objects

Passing Arguments to Functions Argument is some data passed from the caller to a function which acts as an input to a function Each argument is specified by its data type void FunctionName(DataType, DataType); //Prototype FunctionName(Value, Value); //Call to function void FunctionName(DataType Var1, DataType Var2) //Function Declarator

#include<iostream #include<iostream.h> void chararcterline(char, int); //Prototype void main() { char a=‘#’; int b=20; characterline(a,b); //Function Call cout<<endl<<“Object Oriented Programming”<<endl; a=‘*’; cout<<“OOP is an inspiration from “<<endl<< “real world objects. “<<endl<< “World is made up of objects”<<endl; a=‘&’; } void characterline(char y,int z) //Function Definition for(int x=1;x<=z;x++) cout<<y; Example Output: #################### Object Oriented Programming ******************** OOP is an inspiration from real world objects. World is made up of objects &&&&&&&&&&&&&&&&&&&&

Returning Values from Functions Arguments are passed from caller to the function It may also be a scenario when we need the result to be sent back to the caller from the function It can be accomplished using return statement The value to be returned must have the same data type as given in prototype and definition of the function When a fucntion completes its execution, it can return a single value to the calling program Usually, this returned value is the answer calculated by the fucntion

#include<iostream> using namespace std; float lbsToKg(float); //Declaration int main() { float lbs; cout<<“\n Enter your weight in pounds:”; cin>>lbs; cout<<“\n Your weight in Kg is:”<<lbsToKg(lbs); //Call return 0; } float lbsToKg(float pounds) //Definition return 0.453592*pounds; Example

Multiple Functions We may have multiple functions in a program Any function can be called from any function as we call our own declared function from main()

#include<iostream #include<iostream.h> int add(int,int); //Declaration int multiply(int,int); //Declaration int main() { int a,b; cout<<“\n Enter first number:”; cin>>a; cout<<“\n Enter 2nd number:”; cin>>b; cout<<“\n Product is:”<<multiply(a,b); //Call return 0; } int multiply(int x, int y) //Definition int i,result=0; for(i=1;i<=y;i++) result=add(result, x); //Call return result; int add(int x, int y) //Definition return x+y; Example

Passing Arrays to a Function Array name acts as a base address of the whole array If we only use name of array, then it means we are starting from the first element of the array We can increment towards next locations just like we increment towards next consecutive houses of friends in a street

#include<iostream> using namespace std; int add(int[]); void main() { int i[]={4,6,8,9,10}; int j; cout<<"Sum of array elements is "<<add(i); } int add(int arr[]) int sum=0; for(int x=0;x<5;x++) sum=sum+arr[x]; return sum; Example Output Sum of array elements = 37

#include<iostream> using namespace std; int add(int[]); void main() { int i[]={4,6,8,9,10}; int j; cout<<"\n First element before addition ="<<i[0]; cout<<“\n Sum of array elements = "<<add(i); cout<<"\n First element after addition ="<<i[0]; } int add(int ptr[]) int sum=0; for(int x=0;x<5;x++) sum=sum+ptr[x]; ptr[0]=100; return sum; Example Output First element before addition =4 Sum of array elements = 37 First element after addition =100

What’s going on! Arrays are passed by reference, not by value If they would have been passed by value then in the previous example first element of original array would not have been altered But ordinary variables are passed by value, not by reference

#include<iostream #include<iostream.h> int add(int[],int); void main() { int i[]={4,6,8,9,10}; int j,k=50; cout<<"\n First element before addition ="<<i[0]; cout<<"\n Value of k before addition ="<<k; cout<<"\n Sum of array elements = "<<add(i,k); cout<<"\n First element after addition ="<<i[0]; cout<<"\n Value of k after addition ="<<k; } int add(int ptr[],int k) int sum=0; for(int x=0;x<5;x++) sum=sum+ptr[x]; ptr[0]=100; k++; return sum; Example Output First element before addition =4 Value of k before addition =50 Sum of array elements = 37 First element after addition =100 Value of k after addition =50

Homework (ungraded) Problem: Build a program which takes 8 integers as input array. It also asks for an option: “i” means Insertion Sort “s” means Selection Sort It simulates the corresponding sorting algorithm as selected by user By simulation, we mean showing all intermediate steps while trying to sort

Homework (Cont.) You MUST write separate function for each algorithm and on selection of appropriate choice, only corresponding function will be executed Insertion sort MUST properly use INSERT function Selection sort MUST properly use FIND_MIN function