🟡 Control Flow  ·  Lesson 23

Functions in C++

What is a function?

A function is a named block of code that does one job. You write it once and call it whenever you need that job done. Functions keep programs organised, avoid repetition, and make code easier to read and test.

Anatomy of a function

C++
int add(int a, int b) {   // return type, name, parameters
    return a + b;         // body: does the work, returns a value
}
PartMeaning
intReturn type — the kind of value it gives back
addThe function's name
(int a, int b)Parameters — inputs it receives
return a + b;Sends a value back to the caller

A working example

C++
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(3, 5);   // call the function
    cout << "Sum = " << result << "\n";
    return 0;
}
Output:
Sum = 8

Function prototypes

If you want to define a function after main, you tell the compiler about it first with a prototype — the header plus a semicolon:

C++
int add(int, int);        // prototype

int main() {
    cout << add(2, 4);     // works, thanks to the prototype
}

int add(int a, int b) {   // definition, later
    return a + b;
}

Pass by value vs reference

By default a function gets a copy of each argument, so it cannot change the caller's variable. Add & to pass by reference and work on the original:

C++
void doubleIt(int &n) {   // reference parameter
    n = n * 2;
}
int main() {
    int x = 5;
    doubleIt(x);
    cout << x;            // 10 — the original changed
}
Output:
10

References are also efficient for large data, since nothing is copied. See references for more.

Functions that return nothing

Use void when the function just performs an action:

C++
void greet(string name) {
    cout << "Hello, " << name << "!\n";
}

Common mistakes

  • Forgetting the return in a non-void function.
  • Calling a function before declaring or prototyping it.
  • Mismatching argument types or count against the parameters.
  • Expecting a pass-by-value function to change the caller's variable.
🏋️ Practice

Write a function maxOf(int, int) that returns the larger of two numbers, and a void function that prints a line of stars of a given length. Call both from main.

Summary

  • A function is a reusable named block that does one job.
  • It has a return type, name, parameters and a body.
  • Prototypes let you define a function after it is called.
  • Pass by value copies; pass by reference (&) shares the original.
  • void functions perform an action without returning a value.

Frequently Asked Questions

What is a function in C++?
A function is a named block of code that performs a specific task and can be called whenever needed. It helps break a program into smaller, reusable pieces, so instead of repeating code you write it once in a function and call it by name with any arguments it needs.
What are the parts of a function in C++?
A function has a return type, a name, a parameter list in parentheses, and a body in braces. For example in int add(int a, int b), int is the return type, add is the name, and a and b are parameters that receive values when the function is called.
What is a function prototype in C++?
A prototype declares a function's name, return type and parameters before it is used, so the compiler knows how to call it even if the full definition appears later in the file. It is written like the function header followed by a semicolon, such as int add(int, int);.
What is the difference between pass by value and pass by reference in C++?
Pass by value gives the function a copy of the argument, so changes inside the function do not affect the caller's variable. Pass by reference, written with an & in the parameter, lets the function work on the original variable, so changes are visible to the caller.
What does a void function do in C++?
A void function performs an action but does not return a value. It is used when the job is to do something — like printing a message or updating data through references — rather than to compute and hand back a result.

Function क्या है?

Function एक नामित code block है जो एक काम करता है। आप इसे एक बार लिखते हैं और जब भी वह काम चाहिए तब call करते हैं। Functions programs को व्यवस्थित रखते हैं, दोहराव से बचाते हैं, और code को पढ़ना तथा test करना आसान बनाते हैं।

Function की बनावट

C++
int add(int a, int b) {   // return type, naam, parameters
    return a + b;         // body: kaam karti hai, value lautati hai
}
हिस्साअर्थ
intReturn type — किस तरह की value लौटाता है
addFunction का नाम
(int a, int b)Parameters — जो inputs यह पाता है
return a + b;Caller को एक value वापस भेजता है

एक चलता उदाहरण

C++
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(3, 5);   // function ko call karein
    cout << "Sum = " << result << "\n";
    return 0;
}
Output:
Sum = 8

Function prototypes

अगर आप function को main के बाद define करना चाहें, तो पहले compiler को उसके बारे में prototype से बताएँ — header और एक semicolon:

C++
int add(int, int);        // prototype

int main() {
    cout << add(2, 4);     // prototype ki wajah se kaam karta hai
}

int add(int a, int b) {   // definition, baad mein
    return a + b;
}

Pass by value बनाम reference

Default रूप से function हर argument की copy पाता है, तो वह caller का variable नहीं बदल सकता। मूल पर काम करने को & जोड़कर reference से भेजें:

C++
void doubleIt(int &n) {   // reference parameter
    n = n * 2;
}
int main() {
    int x = 5;
    doubleIt(x);
    cout << x;            // 10 — mool badal gaya
}
Output:
10

References बड़े data के लिए कुशल भी हैं, क्योंकि कुछ copy नहीं होता। अधिक के लिए references देखें।

कुछ न लौटाने वाले functions

void तब इस्तेमाल करें जब function बस एक क्रिया करता है:

C++
void greet(string name) {
    cout << "Hello, " << name << "!\n";
}

आम गलतियाँ

  • Non-void function में return भूलना।
  • Function को declare या prototype करने से पहले call करना।
  • Arguments के types या संख्या को parameters से बेमेल करना।
  • Pass-by-value function से caller का variable बदलने की उम्मीद करना।
🏋️ अभ्यास

एक function maxOf(int, int) लिखें जो दो numbers में से बड़ा लौटाए, और एक void function जो दी गई लंबाई के stars की एक line print करे। दोनों को main से call करें।

सारांश

  • Function एक पुनः-प्रयोग योग्य नामित block है जो एक काम करता है।
  • इसमें एक return type, नाम, parameters और body होती है।
  • Prototypes आपको function को call के बाद define करने देते हैं।
  • Pass by value copy करता है; pass by reference (&) मूल साझा करता है।
  • void functions बिना value लौटाए एक क्रिया करते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.