CHapter 2 function
CHapter 2 function
CHAPTER-Two
Working on Functions
CONTENTS
Introduction
Defining and Declaring Functions
Function passing
Scope of variables on functions
Inline function
Recursive functions
Tuesday, December 24, 2024 Abdela A. 2
Modularity
• Modular programming
– Breaking down the design of a program into individual
components (modules) that can be programmed and tested
independently
– A program divided into several smaller parts which can interact
• Modules
– Can be written and tested separately
– Testing is easier (smaller)
– Doesn't need to be retested
– Reduces length of program
– Hides details (abstraction)
duplicate (x , y , z);
Tuesday, December 24, 2024 Abdela A. 44
#include<iostream.h>
#include<math.h>
void sqr (float &num);
void main()
{
float first;
cout<<“Enter the first number”;
cin>>first;
cout<<“Before num is: “<<first;
cout<<“\n In function ”;
sqr(first); // calls function
cout<<“After num is :”<<first;
}
void sqr (float &num)
{
num = sqrt(num);
cout<<“the square root is “ <<num<<“\n”;
}
Tuesday, December 24, 2024 Abdela A. 45
Write a program to swap two integers using call by value and
call by reference.