Learn function and its type in c programming in details

Function

A function is a group of statements that together perform a task. All C programs made up of one or more functions. There must be one and only one main function.
You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task.

A program will be executing statements sequentially inside one function when it encounters a function call.
A function call is an expression that tells the CPU to interrupt the current function and execute another function.
The CPU 'puts a bookmark' at the current point of execution, and then calls (executes) the function named in the function call. When the called function terminates, the CPU goes back to the point it bookmarked, and resumes execution.

Function definition

return_type function_name( parameter list )
{
//statements
}

- The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value.

- function name is the identifier by which the function can be called.

- A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

- statements is the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does.

Types of function in c
There are two types of function in C programming:

1. Standard library functions:
2.User-defined functions:

# Standard library functions:
C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program.

These functions are defined in header files. For example,

1. The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in the stdio.h header file. Hence, to use the printf() function, we need to include the stdio.h header file using #include < stdio.h>.
2. The sqrt() function calculates the square root of a number. The function is defined in the math.h header file.

If you want to use the printf() function, the header file < stdio.h> should be included. For example,


#include<stdio.h>
int main()
{
printf("Catch me if you can.");
}



# User-defined functions:
A function is a block of code that performs a specific task. C allows us to define functions according to your need. These functions are known as user-defined functions. For example:

Programming examples of simple user defined function.

#include < stdio.h>
void functionName()
{
... .. ...
... .. ...
}
int main()
{
... .. ...
... .. ...

functionName();

... .. ...
... .. ...
}

The execution of a C program begins from the main() function. When the compiler encounters functionName();, control of the program jumps to void functionName() And, the compiler starts executing the codes inside functionName(). The control of the program jumps back to the main() function once code inside the function definition is executed.



Function call by Value and Call By Reference

There are two methods to pass the data into the function in C language, i.e., call by value and call by reference.

  • Function call by value
  • Function call by reference


Function call by Value

  • 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.
  • In call by value method, we can not modify the value of the actual parameter by the formal parameter.
  • 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.
  • 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.


The concept of call by value in c language by the example given below:

#include<stdio.h>
#include<conio.h>
void run(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
void main() {
int x=100;
printf("Before function call x=%d \n", x);
run(x);//passing value in function
printf("After function call x=%d \n", x);
getch();
}



Function Call by reference in C

  • In call by reference, the address of the variable is passed into the function call as the actual parameter.
  • The value of the actual parameters can be modified by changing the formal parameters since the address of the actual parameters is passed.
  • 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>
#include<conio.h>
void run(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num)+=100;
printf("After adding value inside function num=%d \n", *num);
}
void main() {
int x=100;
printf("Before function call x=%d \n", x);
run(&x);//passing reference in function
printf("After function call x=%d \n", x);
getch();
}




Return type in C

The return type defines and constrains the data type of the value returned from a subroutine or method.In many programming languages (especially statically-typed programming languages such as C, C++, Java) the return type must be explicitly specified when declaring a function.

example of return type

#include<stdio.h>

int main() {

int message(); /*function prototype declaration*/

message(); /* calling function message() */

}

int message() {

printf("Hello");

return 0; /*Returning an int value from a function with void return type */

}




non-Return type in C

Those function which have no arguments and does not receive any data from the calling function. Similarly when it does not return a value, the calling function does not receive any data from the called function amd known as non-return type.

example of non-Return type

#include<stdio.h>

void main() {

void message(); /*function prototype declaration*/

message(); /* calling function message() */

}

void message() {

printf("Hello");

}




Function prototyping

A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.


Passing arguments to functions

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

Post a Comment

if you have any doubts .Please let me Know

Previous Post Next Post