Function in c
What is function in c programming?
A function is a piece of code which performs a particular task given by the programmer.
or
C Function is a group of statements. There is a function in every program. for e.g. The main () function is also written in the program. Function calls are made where the function is required.
Why we use function in c programming?
There are so many reasons to use the function in c programming which is mentioned below:
Sometimes our program gets bigger in size and it is not possible for a programmer to track which piece of code is doing what.
The function is a way to break our code into chunks so that it is possible for a programmer to reuse them.
The code written in the function does not have to be written again and again.
The function Programmer saves time and space for the program.
Larger programs can be divided into smaller functions.
If a said error in the program, then it can be easily removed.
The function can be called again and again where needed.
How we can write any function in c programming
Function has a specific name. At the beginning of the function, its name is followed by two parenthesis () and function statements are two curly braces {}
Syntax of a function
#include <stdio.h>
Void vs(); = Function prototype
Int main()
{
Int a;
vs(); = Function call
Getch();
}
Void vs()
{
Printf(“hi I am vs”) = Function definition
}
What is Function prototype?
Function prototype is a way to tell the compiler about the function we are going to define in the program. Here void indicates that the function returns nothing
Function call
A function call is a way to tell the compiler to execute the function body at the time the call is made
Note that the program execution starts from the main function in the sequence, the instructions are written
Function definition
This part contains the exact set of instructions which are executed during the function call. When a function is called from main (), the main function falls asleep and gets temporarily suspended. During this time the control goes to the function being called. When the function body is done executing main () resumes.
I hope, you understand what is prototype function, function call and function definition
Types of function.
There are two types of functions.
- In-built / Predefined Function
- User-defined Function
What is In-built / Predefined Function?
- In-built Functions are also called predefined or Library Functions.
- In-built Functions, separate header file or preprocessor is created for each of the functions.
- The declaration of a function occurs in the definition header files.
- C has a lot of header files and has different functions grouped in them.
- If the programmer wishes, he can also create his own header files.
For example
printf (): This function stdio.h comes under this header file. If the programmer #include <stdio.h> does not include it in the preprocessor program, then printf cannot be used.
strlen (): This function string.h comes under this header file. If the programmer #include <string.h> does not include it in the preprocessor program, then strlen cannot be used.
What is User-defined Function?
A user-defined function has three divisions.
- Function Declaration
- Function calling
- Function Definition
In Function Declaration
While creating a function, the compiler has to tell what kind of function you want to create, this process is called Function Declaration.
Syntax for Function Declaration
return_type function_name (parameter (s));
Function Declaration has four departments.
- return_type
- function_name
- parameter (s) / argument (s)
- semicolon
What is return_type?
Each function returns a value, be it null (void) or numeric (int, float, double) or character (char). The return_type of the function is void default. The return_type of the function is in the requirement of the program.
What is function_name?
The name of the function should be according to its code. Even if there is no, the compiler runs, but it is not called Good Programming. Function name does not have any keyword of C. There are also parameters inside the parenthesis () of the function of C. The function name is case-sensitive. for eg. hello () and hello () are both different functions.
What is parameter (s) / argument (s)?
Function arguments are data_types or names of their variables with the data type. What type of value a user wants to receive through function calling, it is declared in the argument of the function.
What is a semicolon?
After the Declaration of every function, a semicolon is given. This is also part of the function declaration.
Example
Function Declaration with two parameters
int add (int a, int b); // function declaration
Function Declaration without parameter (s)
int add ();
Function Declaration with one parameter
int add (int a);
What is Function Calling?
Function calling only has the function name, arguments, and semicolon of the function. Below given example function’s return type is ‘integer’. The name of the function is ‘add’ and the function is passed with two parameters meaning a and b. The declaration and definition of the function is of no importance until the function is called.
The syntax for Function Calling
function name (Parameter1, Parameter2. Parameter n);
Example
Function calling with two parameters
add (a, b); // function calling
Function calling without parameter (s)
add ();
Function calling with one parameter
add (a);
Function Definition
The syntax for Function Definition
return_type function_name (Parameter (s)) {
function_body;
}
There are four parts to a function definition.
1. return_type
Each function returns a value, be it null (void) or numeric (int, float, double) or character (char). The return_type of the function is void default. The return_type of the function is in the requirement of the program.
2. function_name
The name of the function should be according to its code. Even if there is no, the compiler runs, but it is not called Good Programming. The function name does not have any keyword of C. There are also parameters inside the parenthesis () of the function of C. The function name is case-sensitive. for eg. hello () and hello () are both different functions.
3. parameter (s) / argument (s)
Function arguments are data_types or names of their variables with the data type. What type of value a user wants to receive through function calling, it is declared in the argument of the function.
4. function_body
Function body has variables, but due to being inside the function, their scope is Local. There are some statement (s) inside the body and value returns.
Example for Function Definition
int add (int x, int y)
{
// function definition
int z;
z = x + y;
return z;
}
Full Example for Function
#include <stdio.h>
int add (int a, int b); // function declaration with argument
int main () {
int a, b, c;
printf (“Enter value of a and b:”);
scanf (“% d% d”, & a, & b);
c = add (a, b); // funtion calling
printf (“Addition of a and b:% d”, c);
return 0;
}
int add (int x, int y) {// function definition
int z;
z = x + y;
return z;
}
Before learning Call By Value and Call By Reference, understand Parameters.
There are two types of parameters in a function.
- Formal parameter
- Actual parameter
Formal parameter
The parameters that are written in the declaration of the function and in the definition are called Formal Parameters.
For example
void swap (int x, int y); // Formal Parameters
int main () {
int a = 2; b = 10
swap (a, b)
}
void swap (int x, int y) {// Formal Parameters
— — — — — —
— — — — — —
}
Actual parameter
The parameters that are written in a function call are called Actual Parameter.
For example:
void swap (int x, int y);
int main () {
int a = 2; b = 10
swap (a, b) // Actual Parameter
}
void swap (int x, int y) {
— — — — — —
— — — — — —
}
There are two types of Function Calling.
- Call by value
- Call by reference
1. Call By Value
In Call By Value, the value of Variable is passed to the function as a parameter.
In the Call By Value, the value of the actual parameter is copied to the Formal Parameter.
Here variable ‘a’ and ‘b’ function ‘add’ have been passed in the program.
Here ‘a’ and ‘b’ are copied to the values ’x’ and ‘y’ variable.
For example:
#include <stdio.h>
void swap (int x, int y);
int main () {
int a = 2, b = 10;
printf (“Before Swapping a =% d and b =% d \ n”, a, b);
swap (a, b);
}
void swap (int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
printf (“After Swapping a =% d and b =% d”, x, y);
}
2. Call By Reference
In Call By Reference, the address of Variable is passed to the function as a parameter.
In Call By Value, the value of the actual parameter is not copied to the Formal Parameter.
Here the variable ‘a’ and ‘b’ address are passed to function ‘add’ in the program.
Here ‘a’ and ‘b’ are not copied to the values ’x’ and ‘y’ variable.
It only holds the address hold of the variables.
For example
#include <stdio.h>
void swap (int * x, int * y);
int main () {
int a = 2, b = 10;
printf (“Before Swapping a =% d and b =% d \ n”, a, b);
swap (& a, & b);
}
void swap (int * x, int * y)
{
int temp;
temp = * x;
* x = * y;
* y = temp;
printf (“After Swapping a =% d and b =% d”, * x, * y);
}
In this article, we looked at the introduction of function. Now we will see its best examples in the next article. Please, the next article is going to be very important, so I will definitely name it Function Part 2.
please read my next article
Thanks for reading this article
Also, read
(https://grapsvs.blogspot.com/2020/08/switch-case-in-c-programming-questions--switch-case-c.html)
(https://grapsvs.blogspot.com/2020/08/for-loop-in-c-do-while-loop-in-c.html)