Function-A function is a group of instruction that performs a specific task. Every C++ program has at least one function, which is main(), all the most trivial programs can define additional functions. We can divide our code into several functions.
Advantages of using the function in a program:
- You can divide your program into logical blocks. ...
- Use of function avoids typing same pieces of code multiple times. ...
- Individual functions can be easily tested.
- In case of any modification in the code, you can modify only the function without changing the structure of the program.
1. User-defined Function-A user-defined function is a programmed routine that has its parameters set by the user of the system. User-defined functions often are seen as programming shortcuts as they define functions that perform specific tasks within a larger system.
2.Built-in function- A function is a group of statements that together perform a task. ... A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C++ standard library provides numerous built-in functions that your program can call.
Example code of how to work with functions.
#include<iostream>
using namespace std;
void abc(); //function declaration
int main()
{
abc(); //function calling
return 0;
}
void abc() //function definition
{
cout<<"welcome to c++";
}
The output of the above program will be as follows:
In this case, we have not passed any argument in the function, We can also pass arguments inside the function. Which can be done as follows:
//program to add two numbers using function
#include<iostream>
using namespace std;
int add(int,int); //function declaration, two intvariable is passed as an arument.
int main()
{
int x,y,sum;
sum=add(4,5); //function calling
cout<<"sum of the two numbers is = "<<sum;
return 0;
}
int add(int a ,int b) //function definition
{
return(a+b)
}
Output of the above code is as follows.
in the function call, we have passed two integer type values. After the call, the definition of the function will get invoked in which 4 will be assigned to variable a and 5 will be assigned to variable b. Then the function will return the sum of these two values and it will get stored in variable sum.
In C++ programming, you can provide default values for function parameters. The idea behind default argument is simple. If a function is called by passing arguments, those arguments are used by the function. But if the argument/s are not passed while invoking a function then, the default values are used.
Default Argument-A default argument is a value provided in function declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument with default value.
The following code will make the concept of default argument more clear.
#include<iostream>
using namespace std;
// A function with default arguments, it can be called with
// 2 arguments or 3 arguments or 4 arguments.
int sum(int a, int b, int c=0, int d=0) //function declaration & definition
{
return (a + b + c + d); //it will return the sum
}
int main()
{
cout <<sum(20, 35) << endl; //function call
cout <<sum(20, 35, 55) << endl; //function call
cout << sum(20, 35, 55, 60) << endl; //function call
return 0;
}
The output of the Code will be
~important Points:
- Default arguments are different from constant arguments as constant arguments can’t be changed whereas default arguments can be overwritten if required.
- Default arguments are overwritten when calling function provides values for them. For example, calling of function sum(20,35,55,60) overwrites the value of z and w to 55 and 60 respectively.
- During calling of function, arguments from calling function to called function are copied from left to right. Therefore, sum(20,35,55) will assign 20, 35 and 55 to x, y, and z. Therefore, default value is used for w only.
- Once default value is used for an argument in function definition, all subsequent arguments to it must have default value. It can also be stated as default arguments are assigned from right to left. For example, the following function definition is invalid as subsequent argument of default variable z is not default.
- Default values are assigned to the variables in a fixed order from Right to left. We are not allowed to assign the values in any other way like from left to right or any random order.
In Next Blog we'll see the different way to call a function.
0 Comments