Java Programming Notes !
Java Programming Notes !
C Functions
In c, we can divide a large program into the basic building blocks known as function.
The function contains the set of programming statements enclosed by {}. A function can
be called multiple times to provide reusability and modularity to the C program. In other
words, we can say that the collection of functions creates a program. The function is also
known as procedure or subroutine in other programming languages.
Advantage of functions in C
There are the following advantages of C functions.
o By using functions, we can avoid rewriting same logic/code again and again in a
program.
o We can call C functions any number of times in a program and from any place in
a program.
o We can track a large C program easily when it is divided into multiple functions.
o Reusability is the main achievement of C functions.
o However, Function calling is always a overhead in a C program.
Function Aspects
There are three aspects of a C function.
o Function declaration A function must be declared globally in a c program to tell
the compiler about the function name, function parameters, and return type.
o Function call Function can be called from anywhere in the program. The
parameter list must not differ in function calling and function declaration. We
must pass the same number of functions as it is declared in the function
declaration.
o Function definition It contains the actual statements which are to be executed. It
is the most important aspect to which the control comes when the function is
called. Here, we must notice that only one value can be returned from the
function.
SN C function Syntax
aspects
Return Value
A C function may or may not return a value from the function. If you don't have to
return any value from the function, use void for the return type.
Let's see a simple example of C function that doesn't return any value from the function.
Example without return value:
1. void hello()
2. {
3. printf("hello c");
4. }
If you want to return any value from the function, you need to use any data type such as
int, long, char, etc. The return type depends on the value to be returned from the
function.
Let's see a simple example of C function that returns int value from the function.
Example with return value:
1. int get()
2. {
3. return 10;
4. }
In the above example, we have to return 10 as a value, so the return type is int. If you
want to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the
return type of the method.
1. float get()
2. {
3. return 10.2;
4. }
Now, you need to call the function, to get the value of the function.
Different aspects of function calling
A function may or may not accept any argument. It may or may not return any value.
Based on these facts, There are four different aspects of function calls.
o function without arguments and without return value
o function without arguments and with return value
o function with arguments and without return value
o function with arguments and with return value
Example for Function without argument and return
value
Example 1
1. #include<stdio.h>
2. void printName();
3. void main ()
4. {
5. printf("Hello ");
6. printName();
7. }
8. void printName()
9. {
10. printf("swastik");
11. }
12. #include<stdio.h>
13. void sum();
14. void main()
15. {
16. printf("\nGoing to calculate the sum of two numbers:");
17. sum();
18. }
19. void sum()
20. {
21. int a,b;
22. printf("\nEnter two numbers");
23. scanf("%d %d",&a,&b);
24. printf("The sum is %d",a+b);
25. }
SN Header Description
file
4 stdlib.h This header file contains all the general library functions like
malloc(), calloc(), exit(), etc.
5 math.h This header file contains all the math operations related
functions like sqrt(), pow(), etc.
9 signal.h All the signal handling functions are defined in this header
file.
Call by value in C
o In call by value method, the value of the actual parameters is copied into the
formal parameters. In other words, we can say that the value of the variable is
used in the function call in the call by value method.
o In call by value method, we cannot modify the value of the actual parameter by
the formal parameter.
o In call by value, different memory is allocated for actual and formal parameters
since the value of the actual parameter is copied into the formal parameter.
o The actual parameter is the argument which is used in the function call whereas
formal parameter is the argument which is used in the function definition.
Let's try to understand the concept of call by value in c language by the example given
below:
1. #include<stdio.h>
2. void change(int num)
3. {
4. printf("Before adding value inside function num=%d \n",num);
5. num=num+100;
6. printf("After adding value inside function num=%d \n", num);
7. }
int main()
8. {
9. int x=100;
10. printf("Before function call x=%d \n", x);
11. change(x);//passing value in function
12. printf("After function call x=%d \n", x);
13. return 0;
14. }
Call by reference in C
o In call by reference, the address of the variable is passed into the function call as
the actual parameter.
o The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
o In call by reference, the memory allocation is similar for both formal parameters
and actual parameters. All the operations in the function are performed on the
value stored at the address of the actual parameters, and the modified value gets
stored at the same address.
Consider the following example for the call by reference.
#include<stdio.h>
1. void change(int *num)
2. {
3. printf("Before adding value inside function num=%d \n",*num);
4. (*num) += 100;
5. printf("After adding value inside function num=%d \n", *num);
6. }
int main()
{
7. int x=100;
8. printf("Before function call x=%d \n", x);
9. change(&x);//passing reference in function
10. printf("After function call x=%d \n", x);
11. return 0;
12. }
2 Changes made inside the function Changes made inside the function
is limited to the function only. The validate outside of the function
values of the actual parameters do also. The values of the actual
not change by changing the formal parameters do change by changing
parameters. the formal parameters.
3 Actual and formal arguments are Actual and formal arguments are
created at the different memory created at the same memory
location location
Recursion in C
Recursion is the process which comes into existence when a function calls a copy of
itself to work on a smaller problem. Any function which calls itself is called recursive
function, and such function calls are called recursive calls. Recursion involves several
numbers of recursive calls. However, it is important to impose a termination condition of
recursion. Recursion code is shorter than iterative code however it is difficult to
understand.
Recursion cannot be applied to all the problem, but it is more useful for the tasks that
can be defined in terms of similar subtasks. For Example, recursion may be applied to
sorting, searching, and traversal problems.
Generally, iterative solutions are more efficient than recursion since function call is
always overhead. Any problem that can be solved recursively, can also be solved
iteratively. However, some problems are best suited to be solved by the recursion, for
example, tower of Hanoi, Fibonacci series, factorial finding, etc.
In the following example, recursion is used to calculate the factorial of a number.
1. #include <stdio.h>
2. int fact (int);
3. int main()
4. {
5. int n,f;
6. printf("Enter the number whose factorial you want to calculate?");
7. scanf("%d",&n);
8. f = fact(n);
9. printf("factorial = %d",f);
10. }
11. int fact(int n)
12. {
13. if (n==0)
14. {
15. return 0;
16. }
17. else if ( n == 1)
18. {
19. return 1;
20. }
21. else
22. {
23. return n*fact(n-1);
24. }
25. }
Recursive Function
A recursive function performs the tasks by dividing it into the subtasks. There is a
termination condition defined in the function which is satisfied by some specific subtask.
After this, the recursion stops and the final result is returned from the function.
The case at which the function doesn't recur is called the base case whereas the
instances where the function keeps calling itself to perform a subtask, is called the
recursive case. All the recursive functions can be written using this format.
Pseudocode for writing any recursive function is given below.
1. if (test_for_base)
2. {
3. return some_value;
4. }
5. else if (test_for_another_base)
6. {
7. return some_another_value;
8. }
9. else
10. {
11. // Statements;
12. recursive call;
13. }
Memory allocation of Recursive method
Each recursive call creates a new copy of that method in the memory. Once some data is
returned by the method, the copy is removed from the memory. Since all the variables
and other stuff declared inside function get stored in the stack, therefore a separate
stack is maintained at each recursive call. Once the value is returned from the
corresponding function, the stack gets destroyed. Recursion involves so much
complexity in resolving and tracking the values at each recursive call. Therefore we need
to maintain the stack and track the values of the variables defined in the stack.
Let us consider the following example to understand the memory allocation of the
recursive functions.
1. int display (int n)
2. {
3. if(n == 0)
4. return 0; // terminating condition
5. else
6. {
7. printf("%d",n);
8. return
9. display(n-1); // recursive call
10. }
11. }
12. Let us examine this recursive function for n = 4. First, all the stacks are maintained
which prints the corresponding value of n until n becomes 0, Once the
termination condition is reached, the stacks get destroyed one by one by
returning 0 to its calling stack. Consider the following image for more
information regarding the stack trace for the recursive functions.
Storage Classes in C
Storage classes in C are used to determine the lifetime, visibility, memory location, and
initial value of a variable. There are four types of storage classes in C
o Automatic
o External
o Static
o Register
Storage Storage Default Scope Lifetime
Classes Place Value
Automatic
o Automatic variables are allocated memory automatically at runtime.
o The visibility of the automatic variables is limited to the block in which they are defined.
The scope of the automatic variables is limited to the block in which they are defined.
o The automatic variables are initialized to garbage by default.
o The memory assigned to automatic variables gets freed upon exiting from the block.
o The keyword used for defining automatic variables is auto.
o Every local variable is automatic in C by default.
Example 1
1. #include <stdio.h>
2. int main()
3. {
4. int a; //auto
5. char b;
6. float c;
7. printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.
8. return 0;
9. }
Static
o The variables defined as static specified can hold their value between the multiple
function calls.
o Static local variables are visible only to the function or the block in which they are
defined.
o A same static variable can be declared many times but can be assigned at only one time.
o Default initial value of the static integral variable is 0 otherwise null.
o The visibility of the static global variable is limited to the file in which it has declared.
o The keyword used to define static variable is static.
Example 1
1. #include<stdio.h>
2. static char c;
3. static int i;
4. static float f;
5. static char s[100];
6. void main ()
7. {
8. printf("%d %d %f %s",c,i,f); // the initial default value of c, i, and f will be printed.
9. }
10. #include<stdio.h>
11. void sum()
12. {
13. static int a = 10;
14. static int b = 24;
15. printf("%d %d \n",a,b);
16. a++;
17. b++;
18. }
19. void main()
20. {
21. int i;
22. for(i = 0; i< 3; i++)
23. {
24. sum(); // The static variables holds their value between multiple function calls.
25. }
26. }
Register
o The variables defined as the register is allocated the memory into the CPU registers
depending upon the size of the memory remaining in the CPU.
o We can not dereference the register variables, i.e., we can not use &operator for the
register variable.
o The access time of the register variables is faster than the automatic variables.
o The initial default value of the register local variables is 0.
o The register keyword is used for the variable which should be stored in the CPU register.
However, it is compilers choice whether or not; the variables can be stored in the
register.
o We can store pointers into the register, i.e., a register can store the address of a variable.
o Static variables can not be stored into the register since we can not use more than one
storage specifier for the same variable.
Example 1
1. #include <stdio.h>
2. int main()
3. {
4. register int a; // variable a is allocated memory in the CPU register. The initial default value of a
is 0.
5. printf("%d",a);
6. }
7. #include <stdio.h>
8. int main()
9. {
10. register int a = 0;
11. printf("%u",&a); // This will give a compile time error since we can not access the
address of a register variable.
12. }
External
o The external storage class is used to tell the compiler that the variable defined as
extern is declared with an external linkage elsewhere in the program.
o The variables declared as extern are not allocated any memory. It is only
declaration and intended to specify that the variable is declared elsewhere in the
program.
o The default initial value of external integral type is 0 otherwise null.
o We can only initialize the extern variable globally, i.e., we can not initialize the
external variable within any block or method.
o An external variable can be declared many times but can be initialized at only
once.
o If a variable is declared as external then the compiler searches for that variable to
be initialized somewhere in the program which may be extern or static. If it is not,
then the compiler will show an error.
o #include <stdio.h>
o int main()
o {
o extern int a;
o printf("%d",a);
o }
#include <stdio.h>
o int a;
o int main()
o {
o extern int a; // variable a is defined globally, the memory will not be allocated to
a
o printf("%d",a);
o }
1. #include <stdio.h>
2. int a;
3. int main()
4. {
5. extern int a = 0; // this will show a compiler error since we can not use extern and initial
izer at same time
6. printf("%d",a);
7. }
8. #include <stdio.h>
9. int main()
10. {
11. extern int a; // Compiler will search here for a variable a defined and initialized s
omewhere in the pogram or not.
12. printf("%d",a);
13. }
14. int a = 20;
1. extern int a;
2. int a = 10;
3. #include <stdio.h>
4. int main()
5. {
6. printf("%d",a);
7. }
8. int a = 20; // compiler will show an error at this line