Functions are elaborated & more examples are added (#24798)
* Functions are elaborated & more examples are added * Cleaned up code and added formatting
This commit is contained in:
committed by
Manish Giri
parent
24acfb420a
commit
faeb8454e0
@ -68,7 +68,7 @@ void increment_by_reference (int & a) {
|
|||||||
### Function body:
|
### Function body:
|
||||||
The function body contains a collection of statements that define what the function does.
|
The function body contains a collection of statements that define what the function does.
|
||||||
|
|
||||||
##Example:
|
#### Example:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
int max(int num1, int num2)
|
int max(int num1, int num2)
|
||||||
@ -83,12 +83,49 @@ int max(int num1, int num2)
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
## calling a function:
|
```
|
||||||
|
#### Calling the function:
|
||||||
|
```cpp
|
||||||
int res;
|
int res;
|
||||||
res=max(5,10);
|
res = max(5,10);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Functions with void return type
|
||||||
|
|
||||||
|
If a function returns a value, assign it to a variable, else just invoke the function like so:
|
||||||
|
```cpp
|
||||||
|
void printMax(int num1, int num2)
|
||||||
|
{
|
||||||
|
// local variable declaration
|
||||||
|
int result;
|
||||||
|
|
||||||
|
if (num1 > num2)
|
||||||
|
result = num1;
|
||||||
|
else
|
||||||
|
result = num2;
|
||||||
|
|
||||||
|
cout << result;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
#### calling the function:
|
||||||
|
```cpp
|
||||||
|
printMax(5,10);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Functions without parameters
|
||||||
|
|
||||||
|
Also note that it is not necessary to create a function with parameters, we can also make functions without parameters like so:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// Function without parameter
|
||||||
|
int test()
|
||||||
|
{
|
||||||
|
// code goes here
|
||||||
|
}
|
||||||
|
|
||||||
|
// In main you can call it like:
|
||||||
|
int val = test();
|
||||||
|
|
||||||
// if the function returns a value, assign it to that variable, else just call it like function(arg1,arg2);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Why are functions important?
|
## Why are functions important?
|
||||||
|
Reference in New Issue
Block a user