Functions
Functions
A function is a block of code that performs a specific task. The idea is to put some
commonly or repeatedly done tasks together to make a function so that instead of
writing the same code again and again for different inputs, we can call this function.
return-type
The return type of a function is the data type of the variable that that function returns.
For e.g.
if we write a function that adds 2 integers and returns their sum then the return type
of this function will be ‘int’ as we will returning sum that is an integer value.
When a function does not return any value, in that case the return type of the function
is ‘void’.
function_name
It is the unique name of that function.
It is always recommended to declare a function before it is used.
Parameters
A function can take some parameters as inputs. These parameters are specified along
with their data types.
For e.g.
if we are writing a function to add 2 integers, the parameters would be passed like –
Function Prototype
In C++, the code of function declaration should be before the function call. However,
if we want to define a function after the function call, we need to use the function
prototype.
The syntax of a function prototype is:
Example:
#include<bits/stdc++.h>
using namespace std;
// using function definition after main() function
// function prototype is declared before main()
// function prototype
int add(int, int);
int main() {
int sum;
return 0;
}
// function definition
int add(int a, int b) {
return (a + b);
}
Main function
The main function is a special function as the computer starts running the code from
the beginning of the main function. Main function serves as the entry point for the
program.
Types of Function: -
Library Functions:
Library functions are the built-in functions in C++ programming.
Programmers can use library functions by invoking the functions directly; they don't
need to write the functions themselves.
Some common library functions in C++ are sqrt(), abs(), isdigit(), etc.
In order to use library functions, we usually need to include the header file in which
these library functions are defined.
For instance, in order to use mathematical functions such as sqrt() and abs(), we need
to include the header file math.h.
User-Defined Functions:
These functions are created by the C programmer to perform specific tasks. By
defining custom functions, programmers can modularize their code, making it more
organized and easier to manage. User-defined functions contribute to code
optimization and reduce the complexity of large programs. They are designed to be
reusable, allowing the programmer to invoke them multiple times as needed.
Parameters :-
Formal Parameter
Formal Parameter refers to a variable and its type as they are declared in the
function or method prototype.
Actual Parameter
Actual Parameter, on the other hand, is the variable or expression associated
with a formal parameter during a function or method call in the calling
environment.
int main() {
int a = 40;
int b = 50;
cout << "Before swap: a = " << a << " b = " << b << endl;
swap(a, b);
cout << "After swap: a = " << a << " b = " << b << endl;
return 0;
}
2. Call by Reference
In simpler terms, when we use call by reference, it means that if you pass a
variable to a function, the function gets to directly interact with the original
variable. Any changes made to the variable inside the function affect the original
variable in the main part of the program. It's like sharing the same piece of paper
between the function and the main program, so changes made by one are
immediately visible to the other. This method is called "call by reference," and it's
efficient in both time and space.
#include <bits/stdc++.h>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
cout<<"In swap function : a = "<<*a<<" b = "<<*b<<endl;;
}
int main() {
int x = 5;
int y = 10;
cout << "Before swap: x = " << x << " , y = " << y << endl;
swap(&x, &y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}