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,

  1. Return Type: It is the type of optional value returned by the function. Only one value can be returned.
  2. 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;
}


Output

Sum is: 40

Type of Function

A function can be of 4 types based on return value and parameters:

  1. Function with no return value and no parameters.
  2. Function with no return value and parameters.
  3. Function with return value and no parameters.
  4. Function with return value and parameters.

There is another classification of function in which there are 2 types of functions:

  1. Library Functions
  2. 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().

1. malloc()

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

2. calloc()

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

3. realloc()

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

4. free()

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


Output

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.

Similar Reads

Basic Syntax

Consider the below Hello World program:...

Variables

...

Data Types

A variable is the name given to the memory location that stores some data....

Identifiers

The data type is the type of data that a given variable can store. Different data types have different sizes. There are 3 types of data types in C:...

Keywords

Identifiers is the name given to the variables, functions, structure, etc. Identifiers should follow the following set of rules:...

Basic Input and Output

Keywords are the reserved words that have predefined meanings in the C compiler. They cannot be used as identifiers....

Format Specifiers

The basic input and output in C are done using two functions namely scanf() and print() respectively....

Escape Sequence

...

Operators

Format specifiers are used to describe the format of input and output in formatted string. It is different for different data types. It always starts with %...

Conditional Statements

Escape sequences are the characters that are used to represent those characters that cannot by represented normally. They start with ( \ ) backslash and can be used inside string literals....

Loops

Operators are the symbols that are used to perform some kind of operation. Operators can be classified based on the type of operation they perform....

Jump Statements

Conditional statements are used to execute some block of code based on whether the given condition is true. There are the following conditional statements in C:...

Example of Loops and Jump Statements

...

Arrays

Loops are the control statements that are used to repeat some block of code till the specified condition is false. There are 3 loops in C:...

Strings

Jump statements are used to override the normal control flow of the program. There are 3 jump statements in C:...

Pointers

C // C program to illustrate loops #include    // Driver code int main() {     int i = 0;        // for loop with continue     for (i = 1; i <= 10; i++) {         if (i == 5 || i == 7) {             continue;         }         printf("%d ", i);     }     printf("\n");        // while loop with break     i = 1;     while (i <= 10) {         if (i == 7) {             break;         }         printf("%d ", i++);     }     printf("\n");        // do_while loop     i = 1;     do {         printf("%d ", i++);     } while (i <= 10);     printf("\n");        // goto statement     i = 1; any_label:     printf("%d ", i++);     if (i <= 10) {         goto any_label;     }        return 0; }...

Functions

...

Structures

An array is a fixed-size homogeneous collection of items stored at a contiguous memory location. It can contain elements from type int, char, float, structure, etc. to even other arrays....

Union

...

Enumeration (enum)

Strings are the sequence of characters terminated by a ‘\0’ NULL character. It is stored as the array of characters in C....

File Handling

...

Preprocessor Directives

Pointers are the variables that store the address of another variable. They can point to any data type in C...

Common Library Functions

...

Conclusion

...