User Defined Function in C – tccicomputercoaching
- October 5, 2020
- Posted by: Riddhi Dhandha
- Category: User defined function
What is User Defined Function?
A User Defined Function is a block of code developed by user to perform specific task.
Using user Defined Function we can divide a complex problem into smaller modules. So we can decrease complexity and lengthy.
C allows programmer to define functions according to their need. These functions are known as user-defined functions. For example:
Suppose, a program which includes addition, subtraction, division and multiplication of two numbers in same program. You can create user defined functions for each operation individually to solve this problem:
Void add(int, int); // function prototype
Void sub (int, int); // function prototype
Void mul (int, int); // function prototype
Void div (int, int); // function prototype
void main ()
printf(“enter the value of a and b);
Void add (int x,int y)
void sub ()
Void mul ()
printf (“%d”, c)
Void div ()
User Defined Function has 3 sections:
Syntax of function prototype :
returnTypefunctionName(type1 argument1, type2 argument2,…);
A function prototype is simply the declaration of a function that specifies function’s name, parameters and return type. It doesn’t contain function body. Parameters names are optional.
Syntax of Function Call:
Function name (argument1, argument2);
We can use only value also. i. e. add (5, 6);
Compiler starts compilation from main () function, so to jump control from main to user defined function, have to use function call. When this function call executes, control jumps from main to that particular user defined function.
Syntax of Function Definition:
returnTypefunctionName(type1 argument1, type2 argument2,…)
Body of function
When a function is called, the control of the program is transferred to the function definition. And, the compiler starts executing the codes inside the body of a function.
Passing arguments to a function:
In programming, argument refers to the variable passed to the function. In the above example, two variables a and b are passed during function call.
The parameters x and y accepts the passed arguments in the function definition. These arguments are called formal parameters of the function.
The type of arguments passed to a function and the formal parameters must match, otherwise the compiler throws error.
Return Statement :
The return statement terminates the execution of a function and returns a value to the calling function. The program control is transferred to the calling function after return statement.
If you like this post then please share and like this post.
Call us @ 98256 18292.
Visit us @ tccicomputercoaching.com