✔ C-LANGUAGE CHAPTERS:-
1. C-Introduction
2. C-Constants
3. C-Basic Programs
4. C-Instructors
5. C-Operators
6. C-If Else Statement
7. C-Loop Contructs
8. C-Nesting Of Loops
9. C-Programming Examples
10. C-Functions
11. C-Functions Naming Convention
12. C-Pointers
13. C-Arrays
14. C-Arrays & Functions
15. C-Strings
16. C-Structures
17. C-Union
18. C-Storage Classes
19. C-PreProcessor
20. C-Header Files
21. C-R/W one Line
22. C-Accessing Random File
23. C-Important Point
24. C-Interview Questions
1. C-Introduction
2. C-Constants
3. C-Basic Programs
4. C-Instructors
5. C-Operators
6. C-If Else Statement
7. C-Loop Contructs
8. C-Nesting Of Loops
9. C-Programming Examples
10. C-Functions
11. C-Functions Naming Convention
12. C-Pointers
13. C-Arrays
14. C-Arrays & Functions
15. C-Strings
16. C-Structures
17. C-Union
18. C-Storage Classes
19. C-PreProcessor
20. C-Header Files
21. C-R/W one Line
22. C-Accessing Random File
23. C-Important Point
24. C-Interview Questions
C-Functions: Previous Next: C-Pointers
Naming convention for writing a function name in C Programming:
Function name follows the same convention which is followed for variable names:
- First character must be an alphabet not a digit.
- Spaces are not allowed.
- Keywords can`t be taken as a function name.
- Length of name is <= 40 characters.
- No special characters are allowed expect underscore.
Recursion:
In recursion, a function calls itself until the condition will not become false. The function which is called again and again is called recursive function and this calling process is known as Recursion.
Recursion is used in game “Tower of Hanoi”.
Eg: Write a program to calculate Factorial of given number using Recursion.
Solution: #include<stdio.h>
#include<conio.h>
void main( )
{
int num, fact = 0;
clrscr();
printf(“Enter number : ”);
scanf(“%d”, &num);
fact = factorial(num);
printf(“The Factorial is %d”, fact);
getch();
}
int factorial(int a)
{
if(a==1)
return 1;
return a * factorial(a-1);
}
Output:
Passing Arguments to a Function in C Programming: -
Parameter passing is a process for communication of data between the function.
Parameter passing is a process for communication of data between the function.
Parameter can be passed by two ways:
- Call by Value.
- Call by Reference.
- Call by Value: -
When arguments are passed by value that passing of method is called “Call by Value”. When we passed argument by call by value it means we passing a copy of arguments not the actual value of arguments. The value of copied argument can be changed. In other way we can say that the formal arguments are copy of actual arguments.
Eg: Write a program to swap two number using function [Call by Value]
Solution: #include<stdio.h>
#include<conio.h>
void main( )
{
int num1, num2;
clrscr();
printf(“Enter 2 numbers : ”);
scanf(“%d %d”, &num1, &num2);
swap(num1,num2);
printf(“After swapping Number1 is %d Number2 is %d”, num1, num2);
getch();
}
void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf(“After swapping Number1 is %d Number2 is %d”, a, b); }
Output:
(B) Call by Reference:-
When we pass arguments by address, the method is called “Call by Reference”. The both (actual and formal arguments) share the same memory location. The address of(&) operator is used with variable name at the time of passing arguments.
Eg: Write a program to swap two number using function [Call by Reference]
Solution: #include<stdio.h>
#include<conio.h>
void main( )
{
int num1, num2;
clrscr();
printf(“Enter 2 numbers : ”);
scanf(“%d %d”, &num1, &num2);
swap(&num1, &num2);
printf(“After swapping Number1 is %d \n Number2 is %d”, num1, num2);
getch();
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf(“After swapping Number1 is %d \n Number2 is %d”, *a, *b);
}
Output: