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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…