96: ***(Syntax)*** Functions and Global Variables
functions
2: Enter just variable name that you want to pass to function in main(){} like happybday(name); but also pass datatype in function declaration e.g. void happybday(string name) :
3: Function Prototype:
You can also write the body of the function below the int main(){} but you just have to declare that function above main like void happybday(string name); so that compiler knows that specific function exists in program.
4: If you have to pass more than one variable to function, separate each with a , . Also, write its datatype in the function declaration.
- int sum(int a, int b); is acceptable.
- int sum(int a, b); is not acceptable ie you have to enter datatype also.
- int sum(int, int); is also acceptable but only in the function prototype. You have to declare variables while writing the id and body of the function int sum(int a, int b){}.
In this program, the variables used in the function ie a and b are called formal parameters while the values that we pass to the functions ie num1 and num2 are called actual parameters.
Formal parameters take value from the actual parameters.
5: You can choose which type of value you need from the function and change void to that data type like float .
6: You should always use different variables in the function declarations from int main( ) so that you don't get confused while passing values to the functions because function variables are destroyed once used:
For example:
In int sum(int a, int b); int a and int b have been used but you can pass any variable when you call this function like here in int main( ) we have passed num1 and num2: sum(num1, num2);
7: Pass by value:
When you pass a value to a function, the value passed is the copy of the actual argument. This can be confirmed if we try to swap the actual values by using a function.
The value will not be swapped from the original variables num1 and num2 because only the copies of actual arguments were passed to the user-defined function. No matter what happens to variables in the function, they will not be affected in the main function.
Thus, for swapping you will need to pass value by reference.
8: Call by reference using C++ reference variable:
& is known as ' address of ' and is used to pass the address of a variable to the function. If we do not use & in the function definition and declaration and see the memory address of the same variables in the main function and user-defined function, it will be different showing that the same memory is not used in the function instead the value is just copied to the function.
Now if we pass the addresses of a and b, the memory address in the main and user-defined function will be the same.
Swap:
You can swap actual arguments by using C++ reference variable. For this just use & symbol before variables in the function id.
The values will be swapped because, in this case, copies of values are not passed to function. Rather, the address of the variable is passed to the function, what happens to the variable in the user-defined function also happens to it in the main function.
9: Call by reference using pointers:
You can do the same as in point 8 by calling by reference using pointers.
For this purpose, just add * before the variables in the function.
10: Inline function:
To write the inline function, you just have to add an inline argument before function id.
If you have very small code in the function like x * y and you have to call it like 10 times, every time, its value will be copied in the function and recalled again. So, if you used the inline function, the compiler will paste code x * y during compile time saving more time.
You should only use the inline function when the function code is very small and is called again and again. If the code is large and you make it inline, it will take so much cache memory making our program code inefficient.
Don't use the Inline function with static variables.
11: Static Variables:
A static variable is initialized once when the function is called for the first time, afterwards, it retains its value and is not initialized again.
In the following example, the prod( ) function is called various times but the value of z is initialized from 0 only when the prod( ) function was called for the first time.
It is advised, you should not use Inline function while using static variables.
12: Const Parameters in functions:
const parameter is used where you are sending a value by its reference and you don't want that value to be even accidently changed within the function due to any condition and to make other developers aware that these arguments are read-only.
In the following program, if const argument is not passed, the values of name and age will be changed, but due to const argument, compiler will show error if these arguments gets changed within the function.
13: Recursion:
Recursion is calling a function within itself. It is a programming technique where a function invokes itself from within. In it, a complex problem is broken into repeatable single steps.
Advantages:
1. Less code
2. Cleaner
3. Useful for sorting and search algorithms.
Disadvantages:
1. Uses more memory
2. Slower
3. Same function is invoked again and again
4. Very difficult to understand most of the times.
Fibonacci series using recursion:
14: Overloaded functions:
Functions that have the same name but different parameters are called Overloaded Functions.
for example:
i. void bake_pizza();
ii. void bake_pizza(string topping1);
iii. void bake_pizza(string topping1, string topping2);
A function name + its parameters ie bake_pizza(string topping1) are together called Function Signature.
15: Boolean Functions:
You use Boolean functions when there is a yes or no questions. If you have to list perfect numbers within a range you will check if the number is a perfect number using a boolean function and if it is true you will print the number otherwise you will leave that number. This will save you a large amount of code.
- The return type will be bool
- The values you return will be
- true
- false
- You should not use 0 and 1 because it is also int datatype whereas true and false are only boolean datatypes.
- You can also write if-statement as: if (isPositive(num) == 1) and it will work the same.
- You can also declare a bool variable before and check it into the if statement and it will work the same
- You can also in the function store your result in a variable and return that variable, it will work the same.
- You can also return the condition that will give 1 and 0 as the answer and it will work the same.
This is the shortest way of writing a boolean function.
16: Generic functions:
Sometimes we need functions with different datatypes. In this case, you can either copy that same function just with a different datatype for every datatype or you can use a single generic function and pass the datatype to this function. This saves a large amount of code.
Now if you want to get a floating value, you will have to make the same function with float datatype:
Or
You can make a generic function by:
- using template <typename T> before function and using T at places where you want to add that data type.
- #include <iostream>using namespace std;template <typename T>T quotient(T x, T y);int main(){float num1 = 5, num2 = 3;cout << "Qoutient of " << num1 << " with " << num2 << " is: "<< quotient<float>(num1, num2);return 0;}template <typename T>T quotient(T x, T y){return x / y;}
- Add <float> or any other datatype you want while calling the function.
- If you don't write <float> the datatypes of variables that you are passing will be passed to T
18: Global variables:
Local variables are located inside a function or a block like int main(){} or void bake_pizza(){} etc.
Functions cannot look into one another ie cannot take its variable unless we pass it as a parameter.
So, in this case, you can make a global variable also. A global variable is written outside every function and block, usually at the top of the program.
But if the same variable is present in a block or function also ie as a local variable, the compiler will prefer the local variable.
=> If you want to use a global variable in a function or a block just use :: before the variable name.
19: fflush:
Use:
cin.clear();
fflush(stdin);
on top of do while loop to stop infinity loop when user enteres some invalid input.
Comments
Post a Comment