C Functions

What are Functions in C?

A function is a block of code that performs a specific task.

Functions help divide a large program into smaller and manageable parts.

They improve code reusability, readability, and maintenance.

Instead of writing the same code multiple times, a function can be called whenever needed.

Example

void greet()
{
printf(“Welcome to C Programming”);
}

Explanation

• greet() is the function name.

• void indicates that the function does not return any value.

• The function displays a welcome message.

• The function can be called whenever required.

Why Functions are Important?

Functions are important because they:

• Reduce code repetition.

• Improve code readability.

• Make programs easier to maintain.

• Help divide large programs into smaller modules.

• Increase code reusability.

Types of Functions in C

C provides two types of functions:

• Library Functions

• User-Defined Functions

Library functions are predefined functions provided by C.

User-defined functions are created by the programmer according to requirements.

Library Functions

Library functions are predefined functions provided by the C Standard Library.

These functions are ready to use and help perform common tasks.

Examples of library functions:

• printf()

• scanf()

• strlen()

• sqrt()

• pow()

Example

#include <stdio.h>

int main()
{
printf(“Hello World”);

return 0;
}

Explanation

• printf() is a library function.

• It is used to display output on the screen.

• The function is available through the stdio.h header file.

• No need to create the function because it is already provided by C.

User-Defined Functions

User-defined functions are functions created by the programmer.

They are used when a specific task needs to be performed repeatedly.

These functions improve code reusability and organization.

Example

#include <stdio.h>

void greet()
{
printf(“Welcome to C Programming”);
}

int main()
{
greet();

return 0;
}

Explanation

• greet() is a user-defined function.

• The function contains code to display a welcome message.

• greet(); is used to call the function.

• When the function is called, the statements inside the function are executed.

• User-defined functions help avoid writing the same code repeatedly.

Advantages of Functions

• Reduce code duplication.

• Improve code readability.

• Make debugging easier.

• Increase code reusability.

• Help organize large programs into smaller modules.

Example Program

Program

#include <stdio.h>

void greet()
{
printf(“Welcome to C Programming”);
}

int main()
{
greet();

return 0;
}

Output

Welcome to C Programming

Explanation

• #include <stdio.h> includes the standard input/output library.

• greet() is a user-defined function.

• printf() displays the message on the screen.

• greet(); calls the function.

• When the function is called, the statements inside the function are executed.

• return 0; ends the program successfully.

Key Points

• Functions are blocks of code that perform specific tasks.

• Functions improve code reusability and readability.

• C provides Library Functions and User-Defined Functions.

• Library functions are predefined by C.

• User-defined functions are created by programmers.

• Functions help organize large programs into smaller modules.