![]() |
A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no header file is required for their usage. In this article, we will learn about user-defined function, function prototype, function definition, function call, and different ways in which we can pass parameters to a function. How to use User-Defined Functions in C?To use a user-defined function, we first have to understand the different parts of its syntax. The user-defined function in C can be divided into three parts:
C Function PrototypeA function prototype is also known as a function declaration which specifies the function’s name, function parameters, and return type. The function prototype does not contain the body of the function. It is basically used to inform the compiler about the existence of the user-defined function which can be used in the later part of the program. Syntaxreturn_type function_name (type1 arg1, type2 arg2, ... typeN argN); We can also skip the name of the arguments in the function prototype. So, return_type function_name (type1 , type2 , ... typeN); ![]()
C Function DefinitionOnce 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. Syntaxreturn_type function_name (type1 arg1, type2 arg2 .... typeN argN) { // actual statements to be executed // return value if any }
C Function CallIn 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. Syntaxfunction_name(arg1, arg2, ... argN); Example of User-Defined FunctionThe following C program illustrates how to use user-defined functions in our program. C
Output
Sum of 10 and 11 = 21 Components of Function DefinitionThere are three components of the function definition:
1. Function ParametersFunction parameters (also known as arguments) are the values that are passed to the called function by the caller. We can pass none or any number of function parameters to the function. We have to define the function name and its type in the function definition and we can only pass the same number and type of parameters in the function call. Example int foo (int a, int b); Here, a and b are function parameters.
2. Function BodyThe 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 ValueThe 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; }
Passing Parameters to User-Defined FunctionsWe can pass parameters to a function in C using two methods:
1. Call by valueIn 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
Output
Values of x and y before swap are: 10, 20 Values of x and y after swap are: 10, 20
2. Call by ReferenceIn a call by Reference, the address of the argument is passed to the function, and changes that are made to the function are reflected back to the values. We use the pointers of the required type to receive the address in the function. Example C
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 FunctionsThe advantages of using functions in the program are as follows:
|
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |