5 Functions functions C functions are basic building

5. Functions

functions: C functions are basic building blocks in a program. All C programs are written using functions to improve re-usability, understandability. A function is a set of instructions, which performs a specific task. Uses of functions: ØC functions are used to avoid rewriting same logic/code again and again in a program. ØThere is no limit in calling C functions to make use of same functionality wherever required. ØWe can call functions any number of times in a program and from any place in a program. ØA large C program can easily be tracked when it is divided into functions. ØThe core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.

Function Declaration: Function declaration or prototype – This informs compiler about the function name, function parameters and return value’s data type. return_type function_name(Argument_list); Example: int add(int a, int b); Function Definition: This contains all the statements to be executed. Return_type function_name (arguments list) { //Body of function; } Function Call: This calls the actual function_name (arguments list);

/* A c program to find the square of number using function*/ #include<stdio. h> // function prototype, also called function declaration float square( float x ); // main function, program starts from here int main( ) { float m, n ; printf ( "n. Enter some number for finding square n"); scanf ( "%f", &m ) ; // function call n = square ( m ) ; printf ( "n. Square of the given number %f is %f”, m, n ); } float square ( float x ) // function definition { float p ; p=x*x; return p ; }

How to call c functions in a program? 1. CALL BY VALUE: ØIn call by value method, the value of the variable is passed to the function as parameter. ØThe value of the actual parameter can not be modified by formal parameter. ØDifferent Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter. Actual parameter – This is the argument which is used in function call. Formal parameter – This is the argument which is used in function definition

/* call-by-value */ #include<stdio. h> // function prototype, also called function declaration void swap(int a, int b); // or void swap(int, int); int main() { int m = 22, n = 44; // calling swap function by value printf(" values before swap m = %d nand n = %d", m, n); swap(m, n); } void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf(" nvalues after swap m = %dn and n = %d", a, b); }

CALL BY REFERENCE: ØIn call by reference method, the address of the variable is passed to the function as parameter. ØThe value of the actual parameter can be modified by formal parameter. ØSame memory is used for both actual and formal parameters since only address is used by both parameters.

//call by reference #include<stdio. h> // function prototype, also called function declaration void swap(int *a, int *b); Address : 100 int main() 22 { m int m = 22, n = 44; // calling swap function by reference printf("values before swap m = %d n and n = %d", m, n); swap(&m, &n); } void swap(int *a, int *b) { int tmp; 100 102 tmp = *a; *a = *b; a b *b = tmp; printf("n values after swap a = %d nand b = %d", *a, *b); } 102 44 n

C – Argument, return value All C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Now, we will see simple example C programs for each one of the below. ØC function with arguments (parameters) and with return value. ØC function with arguments (parameters) and without return value. ØC function without arguments (parameters) and with return value. ØIf the return data type of a function is “void”, then, it can’t return any values to the calling function. ØIf the return data type of the function is other than void such as “int, float, double etc”, then, it can return values to the calling function.

How many values can be return from c functions? ØAlways, Only one value can be returned from a function. ØIf you try to return more than one values from a function, only one value will be returned that appears at the right most place of the return statement. ØFor example, if you use “return a, b, c” in your function, value for c only will be returned and values a, b won’t be returned to the program. ØIn case, if you want to return more than one values, pointers can be used to directly change the values in address instead of returning those values to the function.

C – Library Functions ØLibrary functions in C language are inbuilt functions which are grouped together and placed in a common place called library. ØEach library function in C performs specific operation. ØWe can make use of these library functions to get the pre-defined output instead of writing our own code to get those outputs. ØThese library functions are created by the persons who designed and created C compilers. ØAll C standard library functions are declared in many header files which are saved as file_name. h.

conio. h clrscr() This function is used to clear the output screen. getch() It reads character from keyboard getche() It reads character from keyboard and echoes to o/p screen textcolor() This function is used to change the text color textbackground() This function is used to change text background

Stdlib. h Function malloc() calloc() realloc() free() abs() div() abort() exit() system() Description This function is used to allocate space in memory during the execution of the program. This function is also like malloc () function. But calloc () initializes the allocated memory to zero. But, malloc() doesn’t This function modifies the allocated memory size by malloc () and calloc () functions to new size This function frees the allocated memory by malloc (), calloc (), realloc () functions and returns the memory to the system. This function returns the absolute value of an integer. The absolute value of a number is always positive. Only integer values are supported in C. This function performs division operation It terminates the C program This function terminates the program and does not return any value This function is used to execute commands outside the C program.

stdlib. h atoi() atol() atof() strtod() strtol() Converts string to int Converts string to long Converts string to float Converts string to double Converts string to long getenv() This function gets the current value of the environment variable setenv() This function sets the value for environment variable putenv() This function modifies the value for environment variable perror() This function displays most recent error that happened during library function call. rand() This function returns the random integer numbers delay() This function Suspends the execution of the program for particular time

time. h Functions Description setdate() This function used to modify the system date getdate() This function is used to get the CPU time clock() This function is used to get current system time() This function is used to get current system time as structure difftime() This function is used to get the difference between two given times strftime() This function is used to modify the actual time format

User Defined Functions: User defined functions are the functions which are written by us for our own requirement. ØIt is possible to add, delete, modify and access our own user defined function to or from C library. ØThe advantage of adding user defined function in C library is, this function will be available for all C programs once added to the C library. ØWe can use this function in any C program as we use other C library functions.

STEPS FOR ADDING OUR OWN FUNCTIONS IN C LIBRARY: STEP 1: For example, below is a sample function that is going to be added in the C library. Write the below function in a file and save it as “addition. c” addition(int i, int j) { int total; total = i + j; return total; } STEP 2: Compile “addition. c” file by using Alt + F 9 keys (in turbo C). STEP 3: “addition. obj” file would be created which is the compiled form of “addition. c” file.

STEP 4: Use the below command to add this function to library (in turbo C). c: > tlib math. lib + c: addition. obj + means adding c: addition. obj file in the math library. We can delete this file using – (minus). STEP 5: Create a file “addition. h” & declare prototype of addition() function like below. int addition (int i, int j); Now, addition. h file contains prototype of the function “addition”. STEP 6: Let us see how to use our newly added library function in a C program.

# include <stdio. h> // Including our user defined function. # include “c: \addition. h” int main () { int total; // calling function from library total = addition (10, 20); printf ("Total = %d n", total); }

Recursion: The process of calling a function by itself is called recursion and the function which calls itself is called recursive function. Recursion is used to solve various mathematical problems by dividing it into smaller problems A recursive function performs the tasks by dividing it into the subtasks. There is a termination condition defined in the function which is satisfied by some specific subtask. After this, the recursion stops and the final result is returned from the function.

- Slides: 21