CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Cyber Safety and Ethics · 7 min · free

Functions in C++: Overloading, Default Arguments and Inline

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

A C++ function is a named block you define once and call many times; C++ adds references, default arguments, inline and overloading.

Follows the syllabus of: GSEB Std 12 Computer Studies, NIOS Senior Secondary Computer Science (330)

On screen in this lesson

What is a function?

A named block of code that does one job
Write it once, call it many times
Makes programs shorter and easier to fix
main() itself is a function

Parts of a function

PartExampleMeaning
Return typeintType sent back
NameaddWhat we call it
Parameters(int a, int b)Inputs it takes
Body{ return a + b; }The work it does
Calladd(3, 4)Runs the function

Function prototype

The compiler reads a file from top to bottom
Function below main? Declare it first
Prototype: int add(int, int);
It ends with a semicolon and has no body

Value vs reference

PointBy valueBy reference
ReceivesA copyThe original
Symbolint mint &m
Changes main?NoYes
Best forSmall inputsChanging values

Rules for default arguments

Defaults are given from right to left
int fee(int days, int rate = 50) is correct
int fee(int days = 1, int rate) is an error
Give the default once, usually in the prototype

How overloading is chosen

The compiler matches the number of arguments
It also matches the types: int vs double
Return type alone cannot tell them apart
Too many close matches: 'ambiguous' error

Quick answers

Can two C++ functions share a name?

Yes, if their parameter lists differ. That is overloading.

Is int fee(int days = 1, int rate) valid?

No. Defaults must be given from the right.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Can two functions in one program have the same name? In C, no. In C plus plus, yes! Today we learn how to write and call functions, pass values in two ways, and use default arguments, inline functions and overloading.

First, what is a function? A function is a named block of code that does one job, like a calculator button. You write it once and call it whenever you need it. This keeps programs shorter, and a mistake is fixed in one place only. In fact, main is also a function, and the program starts there.

Every function has a few parts. The return type is the type of value it sends back, and void means it sends nothing back. The name is what we use to call it. Parameters are the inputs it receives, written in brackets. The body, inside curly braces, does the work, and return sends the answer back. A call runs the function, and the values we send are called arguments.

Here is a function called add for a canteen bill. It takes two int parameters and returns their sum. Main calls add with fifteen and ten, the prices of a samosa and tea. Control jumps into add, calculates twenty five, and returns it to main. Then see out prints twenty five.

What if you write the function below main? The compiler reads the file from top to bottom. So it must know about a function before it is called. We declare it first with a prototype, which is just the first line followed by a semicolon. A prototype has no body, and the parameter names are optional.

Now, two ways to pass values. In call by value, the function receives a copy of the value. Here marks is sixty, and add bonus gets a copy called m. It changes its copy to sixty five. But the original marks in main is untouched. So the output is still sixty.

Now look at the one small change, an ampersand before the parameter. This makes it a reference, which is another name for the same variable. So call by reference works on the original, not a copy. Adding five changes marks in main itself. The output is sixty five.

Exams often ask to swap two numbers with a function. We use references so that the swap changes the real variables. A temporary variable holds one value while we move the other. P was one and q was two. After the call, the program prints two, then one. Pause and predict. What prints if we remove both ampersands? One two, because only copies are swapped.

Let us compare the two methods side by side. By value receives a copy, while by reference receives the original variable. A reference parameter is written with an ampersand. Changes by value never reach main, but changes by reference do. Use by value for simple inputs, and by reference when the function must change the caller's variables.

A default argument is a value a parameter uses when the caller leaves it out. Think of a library fine. The rate is fifty rupees a day unless we say otherwise. Fee of four uses the default, so it returns two hundred. Fee of four and eighty replaces the default, so it returns three hundred twenty.

Default arguments follow strict rules. Defaults must be given from the right end of the list. So giving a default to rate, the last parameter, is correct. Giving a default to days but not to rate is a compile error. And write the default only once, usually in the prototype.

Every function call takes a little time, because control jumps away and comes back. For a tiny function, the keyword inline asks the compiler to paste the body at the place of the call. So square of nine becomes nine times nine, and it prints eighty one. Inline is only a request, and the compiler may ignore it for big functions or loops.

Function overloading means two or more functions share one name but have different parameter lists. Here, one area function takes one side and finds a square's area. The other takes length and breadth and finds a rectangle's area. Area of five calls the first, giving twenty five. Area of four and six calls the second, giving twenty four.

How does the compiler pick the right function? First, it looks at how many arguments you pass. It also looks at their types, such as int or double. The return type alone is not enough, so two functions that differ only in return type give an error. And if two versions match equally well, the compiler says the call is ambiguous.

Let us revise what we learned today. A function is a named block that you define once and call many times. Call by value sends a copy, and call by reference with an ampersand sends the original. Default arguments are given from the right. Inline asks the compiler to paste a small function where it is called. And overloading means the same name with different parameter lists. Try writing an overloaded function for the perimeter of a square and a rectangle.

Courses that teach this

CourseUnit
CBSE Class 9 Information Technology (402)Electronic Spreadsheet
GSEB Std 12 Computer StudiesCascading Style Sheets and JavaScript
NIOS Secondary Data Entry Operations (229)8. Formulas, Functions and Charts
NIOS Senior Secondary Computer Science (330)Module 3: Programming in C++
Programming All levels CStructures, Unions, and Enums
Programming All levels C++Control Flow and Functions

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →