If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 न करें।
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 करता है।
#include <iostream>
using namespace std;
// define the function.
void myfun(){
cout << "Funtion called";
}
int main() {
// call the function.
myfun();
return 0;
}
Funtion called
C++ में Functions Case Sensitive होते हैं , means myfun() और Myfun() दोनों function different होंगे।
आगर आप same name के function define करते हो तो syntax error आएगी।
#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;
}
Funtion called : myfun Funtion called : Myfun
Code Re-usability : functions use करने का सबसे बड़ा advantage यही है कि , हम code को reuse कर सकते हैं। same processing के लिए एक बार function define करने के बाद उसे हम कही भी और कितनी बार भी use कर सकते हैं।
Less Code : चूंकि हम same code के लिए functions use करते हैं जिससे Program की length कम जाती है।
Reduce Coding Time : Function use करने से coding time reduce होता है , जो कि किसी भी developer के लिए important है।
Easy To Understand : Code को समझना आसान हो जाता है।
ऊपर जो function examples दिए गए हैं यह simple and regular functions थे , इनके अलावा C++ में functions कुछ इस प्रकार हैं।
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