WHAT ARE FUNCTIONS?
Afunction is a block of code that performs a specific task.
Functions are used to call a formula in the main program
It is not included in the main body
Can be called number of times
STANDARD FUNCTIONS
These standardfunctions are groups in different libraries
which can be included in the C++ program, e.g math.h,
iostream.h
5.
USERDEFINEDFUNCTIONS
Although C++ isshipped with a lot of standard functions,
these functions are not enough for all users, therefore, C++
provides its users with a way to define their own functions
(or user-defined function)
6.
DEFINING FUNCTIONS
A C++function consists of two parts
The function header, and
The function body
The function header has the following syntax
<return type> <name> (<parameter list>)
The function body is simply a C++ code enclosed between { }
EXAMPLE- (CALLING AFUNCTION)
int main(){
int num1, num2;
cout<<"Enter first number"<<endl;
cin>>num1;
cout<<"Enter second number"<<endl;
cin>>num2;
cout<<"The sum is "<<sum(num1, num2);
return 0;
}
9.
FUNCTION PROTOTYPE
A functionprototype is simply the declaration of a function that
specifies function's name, parameters and return type. It doesn't
contain function body.
A function prototype gives information to the compiler that the
function may later be used in the program.
10.
SYNTAX OF FUNCTIONPROTOTYPE
returnType functionName(type1 argument1, type2 argument2, ...);
Int sum (int a, int b)
Declaration: int sum(int, int);
11.
PASSING ARGUMENTS TOA FUNCTION
In programming, argument refers to the variable passed to the function. In the above example,
two variables n1 and n2 are passed during the function call.
The parameters a and b accepts the passed arguments in the function definition. These arguments
are called formal parameters of the function.
12.
RETURNSTATEMENT
The return statementterminates the execution of a function and returns a value to the calling
function.
The void function is used when no return value is needed
When a functionis called, the values that are passed in the call are called arguments or actual parameters
( The actual parameters are x and y in this case)
Formal parameters are local variables which are assigned values from the arguments when the function is
called. ( The formal parameters are a and b in this case which are assigned the values of x and y, to perform
the operation)
int add( int a, int b);
int main()
{ int x=3; y=4;
add(x,y)}
ACTUALANDFORMALPARAMETERS
When the functionis called in the main function, the values given to actual parameters are
copied to formal parameters of the function and the operation is performed
This means that the values assigned to actual parameters in the main function remain unchanged
no matter whatever the operations are performed in the function
POINTSTOREMEMEBER (CALLBYVALUE)
21.
In call ofcall by reference, the address is copied to the function using pointers
This means that the value stored at the address in the main function can be changed using
functions
POINTSTOREMEMEBER (CALLBYREFERENCE)
22.
int add(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
(code in the notes below)
CALLBYREFERENCE(SWAPEXAMPLE)
23.
CLASSTASK (MAHDEPT)
Make amathematical calculator using functions ( addition, subtraction,
multiplication, division)
24.
1. Get twovalues a and b, and change the values of a to a=a+b; and b=a-b; using call by
reference
2. Define two functions to print the maximum and the minimum number respectively among
three numbers entered by user.
3. Write a program to print the circumference and area of a circle of radius entered by user by
defining your own function.
HOMETASKS(MATHDEPT)
Editor's Notes
#16 #include<iostream>
using namespace std;
void find(int x, int arr[])
{
for(int i=0; i<=4; i++)
{
if(arr[i]==x)
cout<<"Number found at postion " << i+1;
}
}
int main()
{
int array[4], num;
cout<<"Enter the numbers in the array ";
for ( int i=0; i<4; i++)
{
cin>>array[i];
}
cout<<"Enter the number to find ";
cin>>num;
find(num, array);
}
#22 #include<iostream>
using namespace std;
int add(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int x=4;
int y= 5;
cout<<"The first values of x is =" <<x<<endl;
cout<<"The first values of y is =" <<y<<endl;
swap(&x,&y);
cout<<"The second values of x is =" <<x<<endl;
cout<<"The second values of y is =" <<y<<endl;
}