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

Function declaration,definition,calling

User-defined functions are custom functions that enhance code modularity, reusability, and maintainability. The document outlines the syntax for function declaration, definition, and calling, along with examples of a complete program demonstrating these concepts. It also highlights the advantages of using functions, such as code reusability and better readability.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Function declaration,definition,calling

User-defined functions are custom functions that enhance code modularity, reusability, and maintainability. The document outlines the syntax for function declaration, definition, and calling, along with examples of a complete program demonstrating these concepts. It also highlights the advantages of using functions, such as code reusability and better readability.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Definition:

User-defined functions are custom functions created by the programmer to perform


specific tasks. They help modularize the program, improve code reusability, and
make programs easy to understand and maintain.

Syntax of a Function

return_type function_name(parameter_list);

1. Function Declaration (Prototype)

 Tells the compiler about the function name, return type, and parameters
before it is used.
 Usually written before the main() function.

Example:

int add(int a, int b);

2. Function Definition

 Contains the actual code (body) of the function.


 Written outside main().

Example:

int add(int a, int b) {


return a + b;
}

3. Function Call

 Used to invoke the function and execute its code.


 Can be done inside main() or another function.

Example:

sum = add(5, 10);

✅ Complete Example Program

#include <stdio.h>

// Function Declaration
int add(int a, int b);

int main() {
int x = 5, y = 10, result;

// Function Call
result = add(x, y);

printf("Sum = %d\n", result);


return 0;
}

// Function Definition
int add(int a, int b) {
return a + b;
}

💡 Advantages of Using Functions:

 ✅ Code Reusability
 ✅ Better Readability
 ✅ Easy Debugging
 ✅ Modular Programming
Example Program:

#include <stdio.h>

// Function Declarations

int add(int a, int b);

int subtract(int a, int b);

int multiply(int a, int b);

float divide(int a, int b);

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

// Function Calls

printf("Addition = %d\n", add(num1, num2));

printf("Subtraction = %d\n", subtract(num1, num2));

printf("Multiplication = %d\n", multiply(num1, num2));

if (num2 != 0)

printf("Division = %.2f\n", divide(num1, num2));

else

printf("Division by zero is not allowed.\n");

return 0;
}

// Function Definitions

int add(int a, int b) {

return a + b;

int subtract(int a, int b) {

return a - b;

int multiply(int a, int b) {

return a * b;

float divide(int a, int b) {

return (float)a / b;

You might also like