Functions | #Tut-06
So, today we are going to cover very important concept in C programming language which is nothing but the "functions" and "pointers".
Sometimes our program gets bigger in size and it's not possible for a programmer to track which piece of code is doing what. Function is a way to break our code into chunks so that it is possible for a programmer to reuse them.
Function's :
What is function ?
--> A function is a block of code which performs a particular task. A function can be reused by the programmer in a given program for any number of times.
C functions can be classified into two categories
1. library functions : commonly required functions grouped together in a library file on a disk.
2. user defined function : these are the functions declared and defined by the user.
Why use functions..?
--> to avoid rewriting of same logic again and again
--> to keep track of what we are doing in program
--> to test and check logic independently
--> separating the code into modular functions also makes the program easier to design and understand
some advantages..
- It saves time and energy as it can be reused
- It helps in faster development
- Debugging and testing is easier
- Functions are helpful in generalizing the program
let us see one program..
output :
Passing values to functions..
we can pass value to a function and can get value in return from a function.
int sum(int a, int b){ }
The above prototype means that sum is a function which takes values a (int type) and b (int type)
and return a vlues of the type interger.
Now as I have declared the function sum, let us see how will be the definition of sum...
Let us see one program related to library function...
Output :
Here #include<math.h> is a library function and the expression pow() is defined under this library function. It always takes a floating value or cannot be run without a "%f".
Recursion..
There is simple defination of recursion i.e. "a function call itself" . Any function which calls itself is called recursive function, and such function calls are called recursive calls. Recursion involves several numbers of recursive calls. There are very good examples of recursion
e.g. factorial of number, fibonacci series, etc.
So at the end our recursion should stop at the one stage i.e we need to specify the base condition and as we know factorial(1) or factorial(0) is always 1.
Code Snippet :
Output :
Pointers..
A pointer is a variable which stores the address of another variable.
e.g int i = 3;
int *j = &i;
when i want to access the address of i then i will use --> j
and when i want to access value of j then i will use --> *j
Code Snippet :
Output :
Types of function call..
This won't swap the values as we are passing the values only and not the address, so only the copy of that variables will be passed to function.
2. Call by reference..
This will definitely swap the values as we are passing the address of the variables so, the changes will affect the original values also and this is nothing but our call by reference.
Comments
Post a Comment