Functions in C-Language
Functions in C-Language
Explanation:
Functions are used to organize code into reusable modules, making it
easier to write, test, and maintain programs.
Diagram
What is a Function?
A function is a self-contained block of code that performs a specific task.
Explanation:
Functions are used to organize code into reusable modules, making it
easier to write, test, and maintain programs.
Diagram
What is a Function?
A function is a piece of code that performs some specific task.
or
A function definition consists of block of code which is capable of
performing some specific task.
Example:-
7 a = 10; Division = 2
8 b = 5;
9 int z = a * b; === Code Execution Successful ===
10 int y = a / b;
11 printf("Product = %d\n",z);
12 printf("Division = %d",y);
13 return 0;
14 }
// Online C compiler to run C program online
#include <stdio.h> int main()
int multi(int a, int b) {
{ int x,y;
int c;
x = 10;
c = a * b;
y = 5;
return (c);
int z = multi (x , y);
}
int div(int a, int b) int k = div (x , y);
{ printf("Product is %d\n",z);
int c; printf("Division is %d\n",k);
c = a / b; }
return (c);
}
Structure of a Function
There are 6 basic structures of a function in C
programming:
1. Function Name
- The name of the function, which is used to call the function.
2. Return Type:
- The data type of the value that the function returns.
3. Parameter List:
- The list of variables that are passed to the function when it is called.
4.Function Body:
- The block of code that is executed when the function is called.
5. Function Prototype:
- The declaration of the function, which specifies the function name, return type, and parameter
list.
6. Function Call:
- The statement that calls the function, passing the required arguments.
Here is a simple example of a function structure:
// Function to add two numbers
int add(int num1, int num2) {
- add is the function name.
int sum = num1 + num2;
- num1 and num2 are the
return sum;
}
parameters.
int main() {
- The function body calculates the
int result = add(5, 10); sum and returns it.
printf("The sum is: %d\n", result); - The main function calls the add
return 0; function and prints the result.
}
Types of Functions
There are several types of functions in
programming, including:
1. Library Functions
- Library functions are Pre-defined functions that are part of the programming language's
standard library.
- Examples: printf(), scanf(), sqrt()
2. User-Defined Functions
- Functions defined by the programmer to perform a specific task.
- Examples: add(), multiply(), calculate_area()
3. Recursive Functions
- Functions that call themselves repeatedly until a base case is reached.
- Examples: factorial(), fibonacci()
4. Inline Functions:
- Functions that are expanded in-line by the compiler, eliminating the overhead of a function call.
- Examples: inline int add(int x, int y) { return x + y; }
5. Pure Functions:
- Functions that always return the same output given the same inputs, without any side effects.
- Examples: int add(int x, int y) { return x + y; }
6. Impure Functions:
- Functions that may return different outputs given the same inputs, or have side effects.
- Examples: int getRandomNumber() { return rand(); }