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