What is function in C-programming language?
A Function in c programming language is a block of code which only runs when it is called. Every C-Program contains at-least one function which is main().
We can pass inputs as parameters, into a function.
Functions are made to perform some actions whenever it is called.
We can call a function several times in a program.
There are two types of functions available in C-Programming Language:
Functions which are predefined in C-Library. Like printf(), scanf(), main()...etc.
Functions which are defined by user are called user-defined functions.
How to Declare A Function in C-Programming Language?
Return_type function_Name(parameters)
{
block of code...
}
Return Type : A function may return a value. The return-type is the data type of the value, the function returns. Some functions perform the desired operations without returning a value. In this case, the return-type is the keyword void.
Function-Name :This is the actual name of the function. The function name and the parameter list together constitute the function signature.
Parameters :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.
Block Of Code :The block of code contains a collection of statements that define what the function does.
example :
void myfun()
{
printf("I am a function");
}
How to call a function in C-Programming Language?
Function-Name(parameters);
example :
myfun();
Example with a program :
#include<stdio.h>
int main()
{
int a = 5, b = 6;
int c = add(a,b);
printf("The sum of %d and %d is %d\n",a,b,c);
return 0;
}
int add(int x, int y)
{
return x+y;
}
Output :
The sum of 5 and 6 is 11
Function with void return value :
void hello()
{
printf("Hello i'm a function");
}
Function With int return value :
int hello()
{
printf("Hello i'm a function");
return 0;
}
Function with double return value :
double hello(double a)
{
a = 9.9;
return a;
}
Function with parameters :
int add(int a, int b)
{
return a+b;
}
Read also : Types of function in c-programming language.
0 Comments
Post a Comment