Function reusable piece of code या block of code होता है जो कि कोई specific task perform करता है। एक बार define करने के बाद हम इन्हें script में कितनी ही बार use / call कर सकते हैं। इसके अलावा C++ में 1000+ predefined useful function हैं जिन्हें हम Built In Functions कहते हैं।


हालाँकि C++ हमें ये facility provide करती है कि user खुद के function define कर सके जिन्हें User Defines Functions कहते हैं । Web page load होते समय कोई भी function automatically run नहीं होता है जब तक कि हम उसे manually call / invoke न करें।

C++ Function Declaration

function declaration को function definition या function statement भी कहते हैं , बाकी languages like PHP, JavaScript की तरह C++ में function define करने के लिए आपको function keyword use नहीं करना पड़ता है।

type function_name()
{
  //perform task here
  return value according to type.
}
  • function define करते समय हमें उसका return type define करना होता है , जो define करता है कि function किस type की value return करेगा। अगर function कोई value return नहीं करता तो , आप void type declare कर सकते हैं।

  • function_name , कोई भी valid name हो सकता है , जो कि string या underscore के साथ start हो और C++ में predefined keywords से match नहीं करता हो।

  • C++ में function name number से start नहीं होता है , numbers को function name के बीच में या last में दे सकते हैं। But कहीं भी floating point numbers नहीं दे सकते हैं।

  • return statement, function run होने के बाद function declaration के type के according value return करता है।

C++ Function Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define the function.
void myfun(){
  cout << "Funtion called"; 
}
int main() { 
  // call the function.
  myfun();  
  return 0;
}
Output
Funtion called

C++ Important

  • C++ में Functions Case Sensitive होते हैं , means myfun() और Myfun() दोनों function different होंगे।

  • आगर आप same name के function define करते हो तो syntax error आएगी।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define the function.
void myfun(){
  cout << "Funtion called : myfun"; 
}

// define function with same name but in different letters.
void Myfun(){
  cout << "Funtion called : Myfun"; 
}

int main() { 
  // call the function.
  myfun(); 
  cout << endl;
  Myfun();
  return 0;
}
Output
Funtion called : myfun
Funtion called : Myfun

C++ Advantages Of Functions

  1. Code Re-usability : functions use करने का सबसे बड़ा advantage यही है कि , हम code को reuse कर सकते हैं। same processing के लिए एक बार function define  करने के बाद उसे हम कही भी और कितनी बार भी use कर सकते हैं।

  2. Less Code : चूंकि हम same code के लिए functions use करते हैं जिससे Program की length कम जाती है।

  3. Reduce Coding Time : Function use करने से  coding time reduce होता है , जो कि किसी भी developer के लिए important है।

  4. Easy To Understand : Code को समझना आसान हो जाता है।

C++ Function Types

ऊपर जो function examples दिए गए हैं यह simple and regular functions थे , इनके अलावा C++ में functions कुछ इस प्रकार हैं।

  1. Parameterized Function
  2. Recursive Function

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers