A computer program cannot handle all the tasks by itself, instead, it requests other programs like entities called, functions in C, to get its task done. A function is a self-contained block of statements that perform a coherent task of some kind. Every C program can be thought of as a collection of these functions. A function can also be referred to as a method or a sub-routine or a procedure, etc.
There are two types of functions in C programming:
return-type functionName( parameters)//function definition
{
//Body of function
return statement;
}
A function in C may or may not return a value, it depends on the function declaration that whether a return type is mentioned or not. for instance, void type does not return any value, whereas, int, float, etc returns some value on function call.
Example: here we are only printing a value but there is no return type.
void hello()
{
printf("hello coderzpy");
}
Example: here we are calculating sum of two integers and returning the sum.
int sum_of_integers()
{
int a=9,b=1;
sum=a+b;
return sum;
}
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
// main function that doesn't receive any parameter and
// returns integer.
int main()
{
int a = 10, b = 20;
// Calling above function to find max of 'a' and 'b'
int m = max(a, b);
printf("m is %d", m);
return 0;
}
m is 20
In the above code, we have created a function to calculate the max value among two integers and return it. The max function is called inside the main function and the returned value gets printed.
Note: also read about the Jump statements in C.
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…