✔ 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-Programming Examples: Previous Next: Functions Naming Convention
C-Functions are another advantages provided by the C language. Functions divides the program into smaller parts and solves the problem individually rather worrying about the overall problem. To divide the big problem into smaller ones, called top-down approach.
C-Functions are another advantages provided by the C language. Functions divides the program into smaller parts and solves the problem individually rather worrying about the overall problem. To divide the big problem into smaller ones, called top-down approach.
“Function is small set of instructions to perform a specific task.”
Why they are used/Advantages: -
- Modularity: - it makes maintenance easy because problems are divided into small parts.
- Reusability: - once the function is defined can be used multiple times in a single program.
- It makes program handling easier.
- Less time consuming
- Sub programs are easier to write, understand and debug.
- Reduction in size of program because function written once, can be used again and again by calling it.
- It makes program more readable and easy to understand.
Suppose, you want to calculate the salary of every employee, if you write a program without using a function, then you have to write same code multiple times.
Syntax: [<return data type>]<Function Name>([arguments list])
{
------------;
------------; Body of Function;
------------;
}
Each function name must contain unique name. Function name is also called identifier and must follow the same convention rules.
It is compulsory to use parenthesis [( )]along with function name.
Types of Functions: -
Generally there are 2 types of Functions:
- Pre defined Functions.
- User defined Functions.
- Pre defined Function: -
They are also called built-in or library functions. Many of operations are used by programmers many a times, such operations are stored in C library, so that whenever they are used they need not to be write every time.
Some library functions are:
printf( ) scanf( )
clrscr( ) sqrt( ): stores in <math.h> header file.
sin( ) cos( )
some date and time functions are: (all defined in <time.h> header file)
time( ): displays time of day. clock( ): displays processor time.
Some utility functions are:
exit( ): used for termination of program from any point.
tolower( ): change case in lower case.
toupper( ): change case in upper case.
atoi( ): converts string into numeric value.
- User defined Function: - They are made by the programmers itself according to their requirements. We have 3 concept regarding user defined function:
- Prototype.
- How to define a function.
- How to call a user defined function.
- Function Prototype: - It tells the compiler about the characteristics of defined/called function in advanced, characteristics can be:
- Name of function.
- Return data type.
- Number of arguments passed.
Prototype should be written just after the declaration of a header file and before the main( ). It is non executable statement and a small and simple representation of a Function.
Syntax: <return data type><Function name>(Number of Arguments);
Eg: int sum(int a, int b);
Variable name is not compulsory to give here.
- How to define a Function: - This contains the definition of a function that executed when function is called by the programmer. The function definition contains:
- Function name
- Return data type
- Arguments list
- Body of function
Syntax: [<return data type>]<Function Name>([arguments list])
{
------------;
------------; Body of Function;
------------;
}
- The return data type is type value i.e. going to be return from the function. If the function does not return any value then it is ‘void’.
- In parenthesis, arguments are to be given with their data types this can be left blank.
- By default return data type of a function is ‘int’.
- Function always returns a single value at a time. ‘return’ keyword is used for this. It is also a Jump statement. It has two uses:
o To return value from calling program.
o And to terminate a function.
- A function can be defined once only, but can be used and called many times and instructions of function get executed when it is called otherwise not.
- Calling of a Function: - when a function is called, then only they are in use and their instructions get executed. Functions are called from the main( ) by using name of functions followed by number of arguments in parenthesis. The statement is terminated with semicolon. We can call as many functions as we want from a single program. When complier finds a calling statement of function during the execution of program then compiler jumps to definition section of a function. This jump process makes an execution slow.
main( )
--------------------- |
--------------------- |
{
Function call( );
-------------------;
-------------------;
Statements;
-------------------;
Definition |
Definition |
-------------------;
Function call( );
-------------------;
-------------------;
}
We have multiple syntaxes to call a function:
- Variable = <function name>([argument list]); (when function returning a value)
- <function name>([argument list]);
- <function name>([argument list]);
- {
<function name>([argument list]);
----------------------------;
----------------------------;
----------------------------;
- }
At the time of calling of a function number of arguments must be same as we passed in definition of function. Only name can be different but data type must be same.
Eg: sum( int num1, float num2 );
sum(int a, float b)
{
----------------;
----------------;
----------------;
}
Different types of arguments in c programming are:
The argument or the parameters that are passed in definition call are known as “Actual Arguments” and arguments that are passes at the time of definition of a function, are called “Formal or Dummy Arguments”.
Eg: write a program to calculate sum of two numbers using functions.[without returning a value]
Solution: #include<stdio.h>
#include<conio.h>
void main( )
{
int num1, num2;
printf(“Enter 2 numbers : ”);
scanf(“%d %d”, &num1, &num2);
sum(num1, num2);
getch();
}
void sum(int a, int b)
{
int ans;
ans = a + b;
printf(“Sum is %d”, sum);
}
Eg: write a program to calculate sum of two numbers using functions.[with returning a value]
Solution: #include<stdio.h>
#include
void main( )
{
int num1, num2, ans = 0;
clrscr();
printf(“Enter 2 numbers : ”);
scanf(“%d %d”, &num1, &num2);
ans = sum(num1, num2);
printf(“Sum is %d”, ans);
getch();
}
int sum(int a, int b)
{
int ans = 0;
ans = a + b;
return ans;
}
Eg: Write a program to check whether entered number is even or odd, if the number is even then add 20 in it otherwise add 30 to number.
Solution: #include<stdio.h>
#include<conio.h>
void main( )
{
int num, ans = 0;
clrscr();
printf(“Enter number : ”);
scanf(“%d”, &num);
ans = check(num);
printf(“The new Number is %d”, ans);
getch();
}
int check(int a)
{
int ans = 0;
if(a % 2 = = 0)
{
ans = a + 20;
return ans;
}
else
{
ans = a + 30;
return ans;
}
}
Output: