0% found this document useful (0 votes)
3 views

Functions in C-Language

A function is a self-contained block of code that performs a specific task, used to organize code into reusable modules. The document outlines the structure of a function, types of functions, and provides examples of user-defined and library functions. It also explains key components such as function name, return type, parameter list, function body, function prototype, and function call.

Uploaded by

mzainsafdarjutt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functions in C-Language

A function is a self-contained block of code that performs a specific task, used to organize code into reusable modules. The document outlines the structure of a function, types of functions, and provides examples of user-defined and library functions. It also explains key components such as function name, return type, parameter list, function body, function prototype, and function call.

Uploaded by

mzainsafdarjutt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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 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:-

int add( int a, int b )


{
int sum;
sum = a + b;
return sum;
}
1 // Online C compiler to run C program online
2 #include <stdio.h> Output
3 int main()
4 { Enter marks : 873
Percentage: 72.75%
5 float marks;
6 === Code Execution Successful ===

7 printf("Enter marks : ");


8 scanf("%f", &marks );
9
10 printf("Percentage: %.2f%%\n", (marks/1200)*100);
11
12 return 0;
13 }
1 // Online C compiler to run C program online
2 #include <stdio.h> Output
3 int per()
4 {
Enter marks : 873
5 float marks;
Percentage: 72.75%
6
7 printf("Enter marks : "); === Code Execution Successful ===
8 scanf("%f", &marks );
9
10 printf("Percentage: %.2f%%\n", (marks/1200)*100);
11 }
12 int main()
13 {
14 per();
15 return 0;
16 }
1 // Online C compiler to run C program online
2 #include <stdio.h>
3
4 int main() Output
5 {
6 int a,b; Product = 50

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(); }

You might also like