Function A function is a block of code which only runs when it is called . Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times . You can pass data, known as parameters, into a function.
The programming statements of a function are enclosed within { } braces , having certain meanings and performing certain operations . Functions are broadly classified into two types, which are as follows Predefined functions User defined functions
Predefined (or) library functions These functions are already defined in the system libraries. Programmer will reuse the already present code in the system libraries to write error free code. But to use the library functions, user must be aware of syntax of the function.
Example sqrt () function is available in math.h library and its usage is − y= sqrt (x) x number must be positive eg : y = sqrt (25) then ‘y’ = 5printf ( ) present in stdio.h library. clrscr ( ) present in conio.h library.
User-Defined Function in C 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.
The user-defined function in C can be divided into three parts: Function Prototype Function Definition Function Call
C Function Prototype A 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.
Syntax return_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 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.
return_type function_name (type1 arg1 , type2 arg2 .... typeN argN ) { // actual statements to be executed // return value if any }
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 . function_name (arg1, arg2, ... argN );
Example of User-Defined Function # include < stdio.h > int sum( int , int ); int sum( int x, int y) { int sum; sum = x + y; return x + y; } int main() { int x = 10, y = 11; int result = sum(x, y); printf ("Sum of %d and %d = %d ", x, y, result); return 0; } OutPut Sum of 10 and 11 = 21
Components of Function Definition Function Parameters Function Body Return Value
1. Function Parameters Function 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.
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; }
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; }
Example 1: Function to calculate the factorial of a number. #include < stdio.h > int factorial( int num ) { int fact = 1; for ( int i = 1; i<= num ; i++) { fact = fact * i; } return fact; } int main() { int num = 5; int result = factorial( num ); printf ("The factorial of %d is %d\n", num , result); return 0; }
Benefits of User-Defined Functions Reusability: One of the main benefits of user-defined functions is that they can be reused in different parts of a program. Instead of writing the same code multiple times, you can define a function and call it whenever you need to perform a specific task. It makes the code more efficient and easier to maintain.
Modularity: User-defined functions promote modularity in a program by breaking it down into smaller, manageable parts . Each function can perform a specific task, and the program as a whole can be built by combining these functions. It makes it easier to understand the code and debug any issues that may arise.
Simplified code: User-defined functions can simplify the code by abstracting away complex logic into a single function. It makes the code easier to read and understand, reducing the likelihood of errors and improving the overall quality of the program.
Better testing: By breaking down a program into smaller functions, it becomes easier to test each function individually. It makes it easier to identify and isolate issues within the code, making it easier to fix any bugs and improve the overall quality of the program.
Improved collaboration: User-defined functions can improve collaboration by making it easier for multiple developers to work on different parts of a program simultaneously. By breaking the program down into smaller functions, each developer can focus on their specific task and work independently, making it easier to combine their work later.
Types of User Defined Functions in C There are four types of user-defined functions divided on the basis of arguments they accept and the value they return: Function with no arguments and no return value Function with no arguments and a return value Function with arguments and no return value Function with arguments and with return value
1. Function with No Arguments and No Return Value Functions that have no arguments and no return values. Such functions can either be used to display information or to perform any task on global variables.
#include < stdio.h > void sum() { int x, y; printf ("Enter x and y\n"); scanf ("%d %d", &x, &y); printf ("Sum of %d and %d is: %d", x, y, x + y); } int main() { sum(); return 0; }
2. Function with No Arguments and With Return Value Functions that have no arguments but have some return values. Such functions are used to perform specific operations and return their value.
#include < stdio.h > int sum() { int x, y, s = 0; printf ("Enter x and y\n "); scanf ("%d %d", &x, &y); s = x + y; return s; } int main() { printf ("Sum of x and y is %d", sum()); return 0; }
3. Function With Arguments and No Return Value Functions that have arguments but no return values. Such functions are used to display or perform some operations on given arguments.
#include < stdio.h > void sum( int x, int y) { printf ("Sum of %d and %d is: %d", x, y, x + y); } int main() { int x, y; printf ("Enter x and y\n "); scanf ("%d %d", &x, &y); sum(x, y); return 0; }
4. Function With Arguments and With Return Value Functions that have arguments and some return value. These functions are used to perform specific operations on the given arguments and return their values to the user.
#include < stdio.h > int sum( int x, int y ) { return x + y; } int main() { int x, y; printf ("Enter x and y\n"); scanf ("%d %d", &x, &y); printf ("Sum of %d and %d is: %d", x, y, sum(x, y)); return 0; }
Recursion Recursion is the technique of making a function call itself . In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function . while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
Example calculates the factorial of a given number using a recursive function
#include < stdio.h > int factorial( int i ) { if(i <= 1 ) { return 1; } return i * factorial(i - 1 ); } int main() { int i = 3 ; printf ("Factorial of %d is %d\n", i, factorial(i )); return 0 ; }
Ex-2 int sum( int k); int main () { int result = sum(10); printf ("%d", result); return 0; } int sum( int k) { if (k > 0) { return k + sum(k - 1); } else { return 0; } }
Fibonacci Series #include < stdio.h > int fibonacci ( int i ) { if(i == 0) { return 0; } if(i == 1) { return 1; } return fibonacci (i-1) + fibonacci (i-2); } int main() { int i; for (i = 0; i < 10; i++) { printf ("%d\t\n", fibonacci (i)); } return 0 ; }
Call by value and Call by reference in C There are two methods to pass the data into the function in C language, i.e., call by value and call by reference .
Call by value in C In call by value method, the value of the actual parameters is copied into the formal parameters. In other words, we can say that the value of the variable is used in the function call in the call by value method . In call by value method, we can not modify the value of the actual parameter by the formal parameter.
In call by value, different memory is allocated for actual and formal parameters since the value of the actual parameter is copied into the formal parameter. The actual parameter is the argument which is used in the function call whereas formal parameter is the argument which is used in the function definition.
#include< stdio.h > void change( int num ) { printf ("Before adding value inside function num =%d \n", num ); num =num+100; printf ("After adding value inside function num =%d \n", num ); } int main() { int x=100; printf ("Before function call x=%d \n", x); change(x);//passing value in function printf ("After function call x=%d \n", x); return 0; }
output Before function call x=100 Before adding value inside function num =100 After adding value inside function num =200 After function call x=100
Call by reference in C In call by reference, the address of the variable is passed into the function call as the actual parameter. The value of the actual parameters can be modified by changing the formal parameters since the address of the actual parameters is passed. In call by reference, the memory allocation is similar for both formal parameters and actual parameters. All the operations in the function are performed on the value stored at the address of the actual parameters, and the modified value gets stored at the same address.
#include< stdio.h > void change( int * num ) { printf ("Before adding value inside function num =%d \n",* num ); (* num ) =* num + 100; printf ("After adding value inside function num =%d \n", * num ); } int main () { int x=100; printf ("Before function call x=%d \n", x); change(&x);//passing reference in function printf ("After function call x=%d \n", x); return 0; }
Before function call x=100 Before adding value inside function num =100 After adding value inside function num =200 After function call x=200
Difference between call by value and call by reference in c No. Call by value Call by reference 1 A copy of the value is passed into the function An address of value is passed into the function 2 Changes made inside the function is limited to the function only. The values of the actual parameters do not change by changing the formal parameters. Changes made inside the function validate outside of the function also. The values of the actual parameters do change by changing the formal parameters. 3 Actual and formal arguments are created at the different memory location Actual and formal arguments are created at the same memory location