Functions
- A function is a block of code which we separate from the main function.
- Functions are used to perform specific tasks.
- When we need to do that task, we call it in main function because we know that in our C++ code, execution starts from main function and ends when main function ends.
- If we do not call function in main function, it will never be executed.
Why Functions?
As we can achieve the same functionality with our code without using the functions, but we still use functions because;
- They make out code more readable.
- Reduce redundancy of your code.
- Easy to find errors.
Real Life Example
If you make a calculator in your main function and you need calculator at 10 places. You will have to write code for it 10 times. If you declare a function and write code for calculator there. And then if you need calculator at 10 places, then you just need to call calculator function, which will be a single line of code.
Steps to write a function
- Declare the function. Declaration of function is also called prototype of the function.
- Define the function. Function definition is the body of the function which defines its behavior.
Now, we will declare a function which will print "Hi, there!" on screen. Then we will call it in main function.
Description
- void print() is the declaration of the function. Return type of function is same as the value it is returning. As our function is returning nothing therefore its type is void.
- The code inside the curly braces is the body of the function.
- We have to declare function before the main function and we can define/write code in body of the function anywhere outside the main function.
- Rules for naming functions are same as rules for naming variables and you can not name your user defined function "main" as we can have only one main function in our code.
Parameters vs Arguments
When there is a variable in the main function and we need it in the other function then we pass it to other function when we call it(inside parentheses). We can pass as many arguments as we want.
Parameters: variables in parenthesis when we declare function.
Arguments: variables in parenthesis when we call function.
Functions which return some value
When functions are returning some value then we can assign it to a variable.
Passing values to functions
We have studied that how can we pass data value to a function. This is called pass by value method. We are actually passing the address of our variable, where our data is stored.
Pass by reference
When you pass a parameter by reference, you are essentially passing a reference to the original data. If you change the data value of your variable in the function, it will also be reflected in the main function.
Pass by Pointer
When you pass a parameter by pointer, you are passing a pointer to the original data. This allows you to access and modify the original data indirectly through the pointer.
Functions and arrays
Functions can accept arrays as arguments, allowing you to perform operations on the array's elements.
Recursion
Recursive functions are functions that call themselves directly or indirectly to solve a problem.
Function Overloading
Function overloading allows us to define different functions with the same name but,
- Overloading functions must have parameters with different data types, OR
- Number of parameters must be different, OR
- If number of parameters is same, their sequence must be different.
- Return type of overloading functions may or may not be different.
Functions with at least one feature given above or having all the three features are said to be overloaded. When function is called, compiler determines which version of function to invoke.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP