About Me

Dr. Ajay Dutta

Introduction to Functions

A function is a group of statements that together perform a task.  

The use of functions in a program allows

- a program to be broken into small tasks.
- a program to be written and debugged one small part at a time.
- several programmers to work on the same program.
-It makes a program much easier to read, test and debug.

There are two types of functions 

• Built in Functions
• User Defined functions

The Functions those are provided by C Language are refers to the Predefined  Functions For example. cin and cout, getch, Clrscr are the Examples of predefined  functions. Predefined functions are stored in the form of header Files so that a user doesn’t need to Know how this Function has developed and a user just use that Function.


Functions those are developed by the user for their Programs are known as User Defined Functions. 

• Function Declaration or Prototyping
• Function Defining
• Calling a Function 


Function Declaration - a function declaration that does not include the body of the function. It contains the Name of Function, Return type of the Function and also the Number of Arguments that a User will take for performing the Operation.

Return Type of a function: The Return Function determines whether a Function will return any value to the Function. If a Function is declared with the void Keyword or if a Function Contains a void then that’s means a Function Never Returns a value. Means a Function will Executes his statements one by one. And if a Function Contain any other data type means if a Function Contains int or Float then the Function must return a value to the user.

 Name of Function : The Name of Function must be valid and the name of function must be Start from any Alphabet and the Name of Function doesn’t Contains any Spaces and the Doesn’t Contains any Special Character For Example Space , * sign etc.

Argument List: A Function may have zero or More Arguments. The Arguments those are used by the function calling are known as the actual Arguments and the arguments those are used in the Function declaration are known as the formal arguments, When calling any function then the actual arguments will match the formal arguments and if a proper match is found, then this will execute the statements of the function otherwise this will give an error Message.


Function Call:
In order to use this function, we need to invoke it at a required place in the program. This is known as the function call.


Function definition – a function definition that includes the body of the function. It contains two parts – the function heading and the
function body.




// function prototype
int CalcSum(int n1, int n2);     
void main(void)
{
int firstNum;
int secondNum;
int theSum;
cout << "Enter two integers: ";
cin >> firstNum >> secondNum;
// call the function in an assignment expression
theSum = CalcSum(firstNum, secondNum);
// call the function in a cout sending constants as arguments
cout << CalcSum(5,8);
// call the function in a cout sending expressions as arguments
cout << CalcSum(2 * 3 + 4, 8 - 12);
}
// function definition
int CalcSum(int n1, int n2)
{
return n1 + n2;
}


Call by value and call by reference in C++

There are two ways to pass value or data to function in C++ language: call by value and call by reference. Original value is not modified in call by value but it is modified in call by reference.







Call by value in C++

In call by value, original value is not modified.

In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main().

Let's try to understand the concept of call by value in C++ language by the example given below:



#include "iostream"
#include "stdafx.h"
#include <stdlib.h>
using namespace std;
void swap(int a, int b); //Swap function declaration

int main()
{
system("cls");
   int x=10, y=20;



   cout << "Before swapping" << endl;
    cout << "x= " << x << endl;
    cout << "y = " << y << endl;

   swap(x, y);

   cout << "\nAfter swapping" << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
system("pause"); 

   return 0;


}


//Swap function definition
void swap(int a, int b)
{
   int t;

   t  = b;
   b = a;
   a = t;



}

Output:

Before swapping
x= 10
y = 20

After swapping
x = 10
y = 20

Press any key to continue . . .




Call by reference in C++


In call by reference, original value is modified because we pass reference (address).


Here, address of the value is passed in the function, so actual and formal arguments share the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.



Let's try to understand the concept of call by reference in C++ language by the example given below:


#include "iostream"
#include "stdafx.h"
using namespace std;
void swap(int &a, int &b); //Swap function declaration

int main()
{

   int x=10, y=20;

// cout<<"Enter the value of x and y\n";
//cin>>x>>y;

   cout << "Before swapping" << endl;
    cout << "x= " << x << endl;
    cout << "y = " << y << endl;

   swap(x, y);

   cout << "\nAfter swapping" << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
system("pause"); 

   return 0;

}


//Swap function definition
void swap(int &a, int &b)
{
   int t;

   t  = b;
   b = a;
   a = t;


}


Output:

Before swapping
x= 10
y = 20

After swapping
x = 20
y = 10

Press any key to continue . . .






Post a Comment

0 Comments