What is a function and built-in function?

28 Dec 2022 Balmiki Mandal 0 C Programming

In C programming, a function is a self-contained block of code that performs a specific task or set of tasks. Functions are a fundamental concept in C and are used to break down a program into smaller, manageable pieces. Each function has a name, a list of parameters (if any), and a body that contains the code to be executed when the function is called.

Here's a basic syntax for defining a function in C:

C- Programming
return_type function_name(parameters) {
    // Function body
    // Code to perform a specific task
}
  • return_type: It specifies the data type of the value that the function returns, or it can be void if the function doesn't return any value.
  • function_name: It is the name of the function and should be a valid C identifier.
  • parameters: These are optional and represent the input values that the function can accept. Parameters are enclosed in parentheses.
  • function body: This is the block of code enclosed in curly braces {} that defines what the function does when called.

Here's an example of a simple C function:

C Programming
int add(int a, int b) {
    return a + b;
}

 

What is Built in function in c 

A built-in function is a function that is already available in the programming language. It does not need to be defined by the user. Some examples of built-in functions in Python are:

  • print(): Prints the given object to the console.
  • len(): Returns the length of the given object.
  • sum(): Sums the elements of the given sequence.
  • abs(): Returns the absolute value of the given number.

The code you provided defines a function called factorial(). This function calculates the factorial of a number. The factorial of a number is the product of all the positive numbers less than or equal to that number. For example, the factorial of 5 is 120 (1 * 2 * 3 * 4 * 5).

The print() function is used to print the output of the factorial() function. The output of the code is 120, which is the factorial of 5.

Here are some more examples of built-in functions in Python:

  • max(): Returns the maximum value of the given sequence.
  • min(): Returns the minimum value of the given sequence.
  • round(): Rounds the given number to the specified number of decimal places.
  • str(): Converts the given object to a string.
  • list(): Converts the given object to a list.

I hope this helps! Let me know if you have any other questions.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.