Functions
Functions are the block of statements enclosed within { } braces that perform some specific task. They provide code reusability and modularity to the program.
Function Syntax is divided into three parts:
1. Function Prototype
It tells the compiler about the existence of the function.
return_type function_name ( parameter_type_list... );
where,
- Return Type: It is the type of optional value returned by the function. Only one value can be returned.
- Parameters: It is the data passed to the function by the caller.
2. Function Definition
It contains the actual statements to be executed when the function is called.
return_type function_name ( parameter_type_name_list... ) { // block of statements . . }
3. Function Call
Calls the function by providing arguments. A function call must always be after either function definition or function prototype.
function_name (arguments);
Example of Function
C
// C program to show function // call and definition #include <stdio.h> // Function that takes two parameters // a and b as inputs and returns // their sum int sum( int a, int b) { return a + b; } // Driver code int main() { // Calling sum function and // storing its value in add variable int add = sum(10, 30); printf ( "Sum is: %d" , add); return 0; } |
Sum is: 40
Type of Function
A function can be of 4 types based on return value and parameters:
- Function with no return value and no parameters.
- Function with no return value and parameters.
- Function with return value and no parameters.
- Function with return value and parameters.
There is another classification of function in which there are 2 types of functions:
- Library Functions
- User-Defined Functions
Dynamic Memory Management
Dynamic memory management allows the programmer to allocate the memory at the program’s runtime. The C language provides four <stdlib.h> functions for dynamic memory management which are malloc(), calloc(), realloc() and free().
The malloc() function allocates the block of a specific size in the memory. It returns the void pointer to the memory block. If the allocation is failed, it returns the null pointer.
Syntax
malloc (size_t size);
The calloc() function allocates the number of blocks of the specified size in the memory. It returns the void pointer to the memory block. If the allocation is failed, it returns the null pointer.
Syntax
calloc (size_t num, size_t size);
The realloc() function is used to change the size of the already allocated memory. It also returns the void pointer to the allocated memory.
Syntax
realloc (void *ptr, size_t new_size);
The free function is used to deallocate the already allocated memory.
Syntax
free (ptr);
Example of Dynamic Memory Allocation
C
// C program to illustrate the dynamic memory allocation #include <stdio.h> #include <stdlib.h> int main() { // using malloc to allocate the int array of size 10 int * ptr = ( int *) malloc ( sizeof ( int ) * 10); // allocating same array using calloc int * ptr2 = ( int *) calloc (10, sizeof ( int )); printf ( "malloc Array Size: %d\n" , 10); printf ( "calloc Array Size: %d\n" , 10); // reallocating the size of first array ptr = realloc (ptr, sizeof ( int ) * 5); printf ( "malloc Array Size after using realloc: %d" , 5); // freeing all memory free (ptr); return 0; } |
malloc Array Size: 10 calloc Array Size: 10 malloc Array Size after using realloc: 5
C Cheat Sheet
This C Cheat Sheet provides an overview of both basic and advanced concepts of the C language. Whether you’re a beginner or an experienced programmer, this cheat sheet will help you revise and quickly go through the core principles of the C language.
In this Cheat Sheet, we will delve into the basics of the C language, exploring its fundamental concepts that lay the groundwork for programming. We will cover topics such as variables, data types, and operators, providing you with a solid understanding of the building blocks of C programming.
Contact Us