Function in c

Function Name − It denotes the actual name of any function that we are using in the given code.
 The name and parameter of a function constitute a function signature in the code together.
Function Body 
− It consists of a collection of all the statements.
 These provide a definition of what the function will do in the code.
Parameters 
− It is just like a placeholder. Whenever we invoke a function, we pass a value to the available parameter. 
We refer to this value as an argument or an actual parameter. 
We refer to the parameter list as the number, order, and type of the function in the code. 
The parameters may be optional. It means that any function in a code may consist of no parameters at all.
Return Type
 − We may get a value in return for a function. The term return type refers to the data type of the returns that we get from the functions (function returns). Some of the functions are also capable of performing any desired operation without getting a value in return for it. In any such case, the keyword used for the return_type will be void.
Syntax of Function Aspects of Function
return_type name_of_function (argument list) {function body;} Function definition
name_of_function (argument_list) Function call
return_type name_of_function (argument list); Function declaration

Function Syntax


return_type name_of_function(data_type parameter…){

// executable code in c

}


Example
Here, we have given a source code below for the function max(). The max() function takes two of the parameters- namely val1 and val2. It then returns the maximum value that is present between both of them. Let us take a look:

/* the function returns the max in between two values */

int max(int val1, int val2) {

/* declaration of local variable */

int result;

if (val1 > val2)

result = val1;

else

result = val2;

return result;

}


Types of Functions
The C programming language has functions that are of the following types:

User-defined Functions – These are the types of functions that we can create using the C programmer so that we can make use of it multiple times. This function reduces the complexity of any big program- and thus, optimizes the given code.
Library Functions – These are the functions whose declaration occurs in the header files of C, such as floor(), ceil(), outs(), gets(), printf(), scanf(), etc.

Return Value
Any function in the C programming may not return its value from any function. In case we don’t need to return the value that is available in any function, we can make use of the void in the form of a return type. Let us look at an example of the C function that does not perform the return of a value from the available function.

Let us look at an example of a C function that has no return value:

void chocolate(){

printf(“chocolate c”);

}

In case we want to return a value from an available function, we must make use of any of the data types, such as char, long, int, etc. Thus, the return type depends totally on the value that needs to return from the available function. We will look at an example of a C function that will return an int value from the given function.

Example of a C function with int return value,

int get(){

return 20;

}

In the example mentioned above, we are trying to return the value 20, and the data type, in this case, is int. If we are willing to return to the value of the floating-point (example, 64.5, 7,9, 27.61, etc.), then we must make use of the float in the form of the method’s return type.

float get(){

return 83.7;

}

Here, we have to call a function so that we get the function’s value.


Function Calling – Different Aspects
The functions available in a code may accept an argument or may not accept it at all. Thus, it may return any of the values or may not do so. On the basis of these facts, the function calls have the following four aspects:

A function that has no arguments and has no return value.
A function that has no arguments but has a return value.
A function that has arguments but has no return value.
A function that has arguments and also has a return value.
Example of a function that has no arguments and has no return value,

#include<stdio.h>

void printName();

void main ()

{

printf(“Bye “);

printName();

}

void printName()

{

printf(“Kiddo”);

}

The output generated from this program would be:

Bye Kiddo

Example of a function that has no arguments but has a return value,

#include<stdio.h>

int sum();

void main()

{

printf(“By calculating the area of the square given\n”);

float area = square();

printf(“The area of the given square will be: %f\n”,area);

}

int square()

{

float side;

printf(“The length of the side of square in meters will be: “);

scanf(“%f”,&side);return side * side;

}

The output generated from this program would be:

By calculating the area of the square given

The length of the side of the square in meters will be: 10

The area of the given square will be: 100.000000

Example of a function that has arguments but has no return value,

#include<stdio.h>

void sum(int, int);

void main()

{printf(“\nThe sum of the two values is %d”,x+y);

}
Posted on by