Functions in C and Strings

Unformatted text preview:

Functions in C A function in C is a set of statements that when called perform some specific task It is the basic building block of a C program that provides code reusability The programming statements of a function are enclosed within braces having certain meanings and performing certain operations They are also called subroutines or procedures in other languages Syntax of Functions in C The syntax of function can be divided into 3 aspects 1 Function Declaration 2 Function Definition 3 Function CallsFunction Declarations In a function declaration provide the function name its return type and the number and type of its parameters parameter 1 name of the function par Type A function declaration tells the compiler that there is a function with the given name defined somewhere else in the program Syntax return type par Type parameter 2 The parameter name is not mandatory while declaring functions We can also declare the function without using the name of the data variables Example int sum int a int b Function Definition A C function is generally defined and declared in a single step because the function definition always starts with the function declaration The below example serves as both a function definition and a declaration return type para1 name function name para1 type in the function call include body of the function Function Call A function call is a statement that instructs the compiler to execute the function We use the function name and parameters stdio h void swap int var1 int var2 int temp var1 var1 var2 var2 temp int main int var1 3 var2 2 printf Before swap Value of var1 and var2 is d d n var1 var2 swap var1 var2 printf After swap Value of var1 and var2 is d d var1 var2 return 0 Write a program in C to find the square of any number using the function Write a program in C to ADD and Subtract two numbers using function include stdio h double square double num return num num int main int num double n printf Input any number for square scanf d num n square num printf The square of d is 2f n num n return 0 this problem dynamic arrays Dynamic arrays Array in C is static in nature so its size should be known at compile time and we can t change the size of the array after its declaration Due to this we may encounter situations where our array doesn t have enough space left for required elements or we allotted more than the required memory leading to memory wastage To solve raised Dynamic arrays Dynamic arrays are a powerful data structure in programming that allows for creating and manipulating arrays of varying sizes during runtime A dynamic array is an array whose size can be changed during runtime Unlike static arrays which have a fixed size that is determined at compile time dynamic arrays can be resized as needed It allows for more flexibility and better memory management as the size of the array can be adjusted to fit the amount of data being stored Dynamic arrays are implemented using pointers and memory allocation functions In C the most commonly used memory allocation functions are malloc calloc and realloc Memory Allocation Contiguous Allocation Reallocation These functions allow for the allocation and deallocation of memory during runtime which is necessary for creating and manipulating dynamic arrays malloc initialization Allocates memory without Resizes allocated previously calloc Allocates and initializes memory to zero realloc memory Syntax void malloc size t size size Number of bytes to allocate Returns A pointer to the allocated memory or NULL if the allocation fails void calloc size t num size t size num Number of elements size Size of each element in bytes Returns A pointer to the allocated memory with all bytes initialized to zero or NULL if the allocation fails void realloc void ptr size t size ptr Pointer to a previously allocated memory block size New size in bytes for the memory block Returns A pointer to the resized memory block or NULL if the reallocation fails The original memory block is freed if reallocation is successful or remains valid if not int arr arr int malloc 10 sizeof int Allocate memory for 10 integers if arr NULL Handle allocation failure 10 sizeof int calculates the amount of memory needed for 10 integers malloc returns a pointer to this memory which you can use to store integers Always check if malloc returns NULL to handle cases where memory allocation fails malloc Memory Allocation Purpose Allocates a block of memory of a specified size How it works You specify the size of memory you need malloc gives you a pointer to this memory block The contents of this memory are not initialized so they could contain any data Syntax void malloc size t size int arr arr int calloc 10 sizeof int Allocate and zero initialize memory for 10 integers if arr NULL Handle allocation failure calloc 10 sizeof int allocates memory for 10 integers and initializes them all to zero int arr int malloc 10 sizeof int Initially allocate memory for 10 integers if arr NULL Handle allocation failure arr int realloc arr 20 sizeof int Resize memory to hold 20 integers if arr NULL Handle reallocation failure if realloc fails the original realloc is used to resize the memory block Here we re increasing the size to hold 20 integers Note that memory block remains unchanged and you need to handle the error properly In C programming pointers are used extensively for managing and manipulating memory particularly when dealing with dynamic memory allocation Pointers are essential for dynamic memory management because they hold the address of dynamically allocated memory They provide a flexible and efficient way to manage memory allowing programs to allocate access and modify memory at runtime Using pointers with functions like calloc malloc and realloc ensures that you can handle varying amounts of data and manage memory efficiently Function A function function is s self contained block or subprogram in C is a set of statements that when called perform some specific task It is the basic building block of a C program that provides code reusability They are also called subroutines or procedures in other languageSyntax of Functions in C The syntax of function can be divided into 3 aspects 1 Function Declaration 2 Function Definition 3 Function Calls A function declaration tells the compiler that there is a function with the given name defined somewhere else in the program The function definition consists of actual statements which are executed when the function is called


View Full Document

Functions in C and Strings

Download Functions in C and Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions in C and Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions in C and Strings and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?