Learn C Programming
Functions
In C programming, a function is a self-contained block of code that performs a specific task. Moreover, functions break down a program into smaller, manageable pieces, making the code more organized, modular, and easier to understand.
Suppose, you are writing a C program and you need to perform the same task in that program more than once. In such case you have two options:
- Use the same set of statements every time you want to perform the task.
- Otherwise, create a function to perform that task, and just call it every time you need to perform that task.
A function is a section of code that only executes when called. Thus, functions promote code reuse, as you can call a function multiple times from different parts of your program.
Function Aspects
There are mainly three aspects of a C function.
- Function declarationย
- Function definition
- Function call
Function declarationย
The function declaration provides information about the function to the compiler without providing its actual implementation. It includes the functionโs name, return type, and a list of parameters (if any). The function declaration typically appears at the beginning of a C program, either in a header file (.h) or directly within the source code, before the function is used or called. The purpose of the declaration is to inform the compiler about the functionโs existence and its signature, allowing it to check for correctness during compilation.
Syntax of a function declaration:
return_type function_name(parameter_list);
Example of a function declaration:
int add(int a, int b);
Function definition
The function definition contains the actual implementation of the functionโs code. It defines what the function does and how it performs its task. However, the function definition typically appears after the main function or any other function that uses it. A function can have only one definition in a program, though it can be declared multiple times.
Syntax of a function definition:
return_type function_name(parameter_list) {
// Function code goes here
// Statements to perform the desired task
}
Example of a function definition:
int add(int a, int b) {
int sum = a + b;
return sum;
}
Function call
A function call is a statement that invokes a specific function to execute its code and perform its designated task. Whenever, to call a function, you use its name followed by a pair of parentheses. If the function takes parameters, you provide them within the parentheses. When the function is called, the program execution jumps to the function definition, and after executing the function code, it returns to the point of the call.
Syntax of a function call:
function_name(arguments);
Example of a function call:
int sum = add(5, 3);
// Calls the function "add" with arguments 5 and 3
In this example, the function add takes two integer parameters, a and b, and returns their sum. The function can be declared elsewhere in the program or in a header file and defined later in the source code. The function call example mentioned earlier (int sum = add(5, 3);) will execute the add function, passing arguments 5 and 3, and store the result (8) in the variable sum.
Types of functions:
In C programming, functions can be categorized into two types:
- Standard library functions
- User Defined functions
Standard library functions
Standard library functions in C are the built-in functions provided by the C standard library. Functions such as malloc(), fopen(), printf(), scanf() etc are standard library functions. These functions are already defined in header files (files with .h extensions are called header files such as stdio.h), so we just call them whenever there is a need to use them.
Additionally, letโs some Examples of standard library header files and their functions:
- stdio.h: For input/output operations like printf(), scanf(), fgets(), fopen(), etc.
- stdlib.h: For memory allocation and other utility functions like malloc(), free(), rand(), etc.
- string.h: For string manipulation functions like strcpy(), strcat(), strlen(), etc.
- math.h: For mathematical functions like sqrt(), sin(), cos(), etc.
- time.h: For time and date functions like time(), ctime(), strftime(), etc.
Therefore, by including the appropriate header files, you can access and use these standard library functions to perform various tasks in your C programs.
User-defined function
In C, a user-defined function is a function that you create to perform a specific task in your C program. Additionally, it allows you to break down your program into smaller, manageable parts.
As a result, user-defined functions enable you to divide a large program into smaller, more manageable modules, making the code significantly easier to read, understand, and maintain.
Advantages of user-defined function
- Modularity: Breaking down complex programs into smaller, manageable modules.
- Code Reusability: Using the same function in multiple parts of the program to reduce duplication.
- Abstraction: Hiding implementation details behind function names for simplicity.
- Debugging and Testing: Easier debugging and isolated testing of individual functions.
- Encapsulation: Grouping related operations together and hiding internal workings.
- Scalability: Allowing easy addition of new functions without changing existing code.
In this overview, we will briefly introduce user-defined functions. However, we will delve deeper into the topic of User-defined Functions in C programming in the next tutorial.
ย
ย
ย