Explain built in function and user define function...
Explain built in function and user define function...
in mind our current context (Tuesday, April 15, 2025, 11:47 AM IST, Bhubaneswar, Odisha,
India). Functions are fundamental building blocks of modular and reusable code in C. They
allow you to break down complex tasks into smaller, manageable units.
1. Built-in Functions (Library Functions):
● Definition: Built-in functions are functions that are pre-defined and provided by the C
standard library. These functions perform common and essential tasks that are frequently
needed in programming.
● Characteristics:
○ Part of the Standard Library: They are part of the C standard library, which is a
collection of header files containing declarations and definitions of various
functions.
○ Ready to Use: You don't need to write the code for these functions yourself. You
can directly call them in your program after including the appropriate header file.
○ Wide Range of Functionality: The standard library provides functions for
input/output operations, string manipulation, mathematical calculations, memory
management, and much more.
○ Portability: Because they are part of the standard, these functions generally
behave consistently across different C compilers and operating systems,
contributing to the portability of C programs.
● How to Use Built-in Functions:
1. Include the Header File: You need to include the header file that declares the
function you want to use. Header files typically have a .h extension and contain
function prototypes (declarations) and other necessary definitions. You use the
#include preprocessor directive for this.
2. Call the Function: Once the header file is included, you can call the function by its
name, followed by parentheses (). If the function expects arguments (input values),
you provide them inside the parentheses, separated by commas. If the function
returns a value, you can store it in a variable or use it directly in an expression.
● Examples of Built-in Functions and Their Header Files:
○ Input/Output (stdio.h):
■ printf(): Prints formatted output to the standard output (usually the console).
■ scanf(): Reads formatted input from the standard input (usually the
keyboard).
■ fopen(), fclose(), fprintf(), fscanf(): Functions for file input and output.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
2. User-Defined Functions:
● Definition: User-defined functions are functions that are written by the programmer to
perform specific tasks that are not directly provided by the standard library. They allow
you to modularize your code, make it more organized, and reuse blocks of code as
needed.
● Characteristics:
○ Programmer Created: You define the function's name, parameters (inputs), return
type (output), and the code that it executes.
○ Tailored to Specific Needs: They are designed to solve particular problems or
perform specific operations within your program.
○ Promote Code Reusability: Once defined, a user-defined function can be called
multiple times from different parts of your program, reducing code duplication.
○ Improve Code Organization: Breaking down a large program into smaller
functions makes it easier to understand, debug, and maintain.
● Syntax for Defining a User-Defined Function:
return_type function_name(parameter_list) {
// Function body: code to be executed
// ...
return value; // If return_type is not void
}
○ return_type: The data type of the value the function will return. If the function
doesn't return a value, the return type is void.
○ function_name: The name you choose for your function (must follow identifier
naming rules).
○ parameter_list: A comma-separated list of parameters (input values) that the
function accepts. Each parameter has a data type and a name (e.g., int x, float y). If
the function takes no parameters, you can leave the parentheses empty or use
void.
○ function body: The block of code enclosed in curly braces {} that contains the
statements to be executed when the function is called.
○ return value;: A return statement is used to send a value back from the function to
the caller (if the return type is not void).
● Example of a User-Defined Function:
#include <stdio.h>
// Function to add two integers
int add(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
// Function to print a greeting message
void greet(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
int a = 5, b = 10;
int result = add(a, b); // Calling the 'add' function
printf("The sum is: %d\n", result); // Output: The sum is: 15
greet("Alice"); // Calling the 'greet' function
return 0;
}