User defined Function
User defined Function
Syntax
C Function Definition
Once the function has been called, the function definition contains
the actual statements that will be executed. All the statements of
the function definition are enclosed within { } braces.
Syntax
C Function Call
In order to transfer control to a user-defined function, we need to
call it. Functions are called using their names followed by round
brackets. Their arguments are passed inside the brackets.
Syntax
function_name(arg1, arg2, ... argN);
Example of User-Defined Function
The following C program illustrates how to use user-defined
functions in our program.
#include <stdio.h>
// Function prototype
// Function definition
int sum;
sum = x + y;
return x + y;
// Driver code
int main()
{
int x = 10, y = 11;
// Function call
return 0;
Output
Sum of 10 and 11 = 21
1. Function Parameters
2. Function Body
The function body is the set of statements that are enclosed within {
} braces. They are the statements that are executed when the
function is called.
Example
int foo (int a, int b) {
int sum = a + b;
return sum;
}
Here, the statements between { and } is function body.
3. Return Value
The return value is the value returned by the function to its caller. A
function can only return a single value and it is optional. If no value
is to be returned, the return type is defined as void.
The return keyword is used to return the value from a function.
Syntax
return (expression);
Example
int foo (int a, int b) {
return a + b;
}
Note: We can use pointers or structures to return multiple values
from a function in C.
1. Call by value
In call by value, a copy of the value is passed to the function and
changes that are made to the function are not reflected back to the
values. Actual and formal arguments are created in different
memory locations.
Example
C
// call by value
#include <stdio.h>
int temp = a;
a = b;
b = temp;
// Driver code
int main()
swap(x, y);
y);
return 0;
Output
Values of x and y before swap are: 10, 20
Values of x and y after swap are: 10, 20
Note: Values aren’t changed in the call by value since they aren’t
passed by reference.
2. Call by Reference
// C program to implement
// Call by Reference
#include <stdio.h>
void swap(int* a, int* b)
*a = *b;
*b = temp;
// Driver code
int main()
y);
swap(&x, &y);
y);
return 0;
Output
Values of x and y before swap are: 10, 20
Values of x and y after swap are: 20, 10
For more details, refer to this article – Difference between Call by
Value and Call by Reference
Advantages of User-Defined Functions
The advantages of using functions in the program are as
follows:
One can avoid duplication of code in the programs by using
functions. Code can be written more quickly and be more
readable as a result.
Code can be divided and conquered using functions. This process
is known as Divide and Conquer. It is difficult to write large
amounts of code within the main function, as well as testing and
debugging. Our one task can be divided into several smaller sub-
tasks by using functions, thus reducing the overall complexity.
For example, when using pow, sqrt, etc. in C without knowing how
it is implemented, one can hide implementation details with
functions.
With little to no modifications, functions developed in one
program can be used in another, reducing the development time.