C Functions
In C programming, functions are essential building blocks for organizing and reusing code.
- Rule: In C, you cannot define a function inside another function.
- Rule: But you can call a function inside another function.
- Rule: Functions cannot return more than one value at a time because, after returning a value, control is passed back to the calling function.
- Any function, including main(), can call itself recursively.
- C has two kinds of functions User Defined Functions, Pr-Defined Libarary Functions.
User – defined functions
What is user-defined function?:
Answer
Users can define custom functions as needed at the user level. User-defined functions should follow certain rules.
Syntax
- User defined function declaration syntax:
returntype function_name(type_1, type_2,....);Refer below code
returntype function_name(type_1, type_2,....)
{
Statement_1;
Statement_2;
}
- Function call syntax :
function_name (); - Function passing arguments(Two Types – Refer below table ) Syntax:
function_name (arg_1, arg_2, …); - If you have a question like what are the function passing arguments types?, Whats is actual arguments? What is Formal arguments? Here the solution check the below table
| Function passing arguments (Two types) | Explanation |
|---|---|
| Actual arguments | Actual arguments are comes with arguments when function call. |
| Formal arguments | Formal arguments or dummy arguments are comes in function definition. |
Function Name
- Function name should declare at only one time, You can follow the same variable name declaration for function name declaration also.
- If two functions are declared with the same name, it results in an error: “Error: Multiple declaration of function_name().”
Return value
A function can return floating point value, integer, decimal, Boolean,
Return statement:
- A function may have any number of return statements each returning different values and each return statements will not occur successively.
- If a function contains two return statements successively, the compiler will generate “Unreachable code” warnings. Check the below code
#include<stdio.h>
int mul(int, int); /* Function prototype */
int main()
{
int a = 4, b = 3, c;
c = mul(a, b);
printf("c = %d\n", c);
return 0;
}
int mul(int a, int b)
{
return (a * b);
return (a - b); /* Warning: Unreachable code */
}
/* Output
c = 12 */
Return Type
- If a function return type is declared as void it cannot return any value.
- If return type for a function is not specified, The default return type for a function is int(Integer) .
Type of User Defined functions
- function with no arguments and no return value.
- function with arguments and no return value.
- function with arguments and return value.
- function with no arguments and return value.
Functions Arguments
- C can accept upto 127 maximum number of arguments in a function.
Function Call
You call a function either by value or by reference.
| Calling Function | Descriptions |
|---|---|
| call by value | Actual argument value is copied to formal arguments. |
| call by reference | Actual argument address is copied to formal arguments. |
Advantage and Disadvantage of user-defined function
| Advantage of functions | Disadvantage of functions |
|---|---|
| Using top-down modular programming. | Difficult to understand function structure. |
| User can understand easily when separate many function. | Users can’t understand easily some like recursive function. |
| Reduce the code repetition. | Program execution control sequence is not in run time |
| Can change function into program. | Can’t return more than one value. |
| Program testing is very simple. | can’t see separate function use. |
Recursive function
- When a recursive call occurs, the function or process duplicates itself and then executes that function, resulting in time and space constraints.
- A loop involves no recursive calls, saving significant time and space.
- The stack may lead to a stack overflow due to excessive recursive calls as it stores the return address of each function call when invoked.
- After some time, the stack memory becomes fully occupied, triggering a stack overflow error.
Previous & Next
- Previous : C Pointers
- Next : C Memory Allocation