420 likes | 536 Views
Data Structure and C Part-6. Naming a Function. Any valid variable-name can be given to the user-defined function. The main program itself is considered as a function in C. Whenever a C program is executed, execution starts from the function. So there should be only one main function.
E N D
Naming a Function • Any valid variable-name can be given to the user-defined function. • The main program itself is considered as a function in C. • Whenever a C program is executed, execution starts from the function. • So there should be only one main function.
Example of Code Program # include <stdio.h> main( ) { ------ Calling ------ Function ------ } ------ } calculation() int…….; { ------ Called ------ Function ------ }
Structure of a Function The general structure is return type function name (list of parameters) Syntax: parameter declaration. { Statements : }
Body of the Function • The body of the function contains a variable declaration part, processing and a return statement. • The variables declared within the body of a function are called ‘local variables’. • These variables can be used or referred to only within the body of that function and hence the name.
These variables have no meaning outside the body of that function. • The body of the function is given within braces. Example: cube ( int x ) { int cubes; cubes = x*x*x; return (cubes); }
The Return-Statement • The calculation part can contain either assignment statement or any valid arithmetic logic expressions. • If there is a value that has to be returned to the calling function • The return statement is used.
Example of Code Program retfun( ) { return(1); } The output is a = 1 The general syntax is : return [value]; Example: # include <stdio.h> main( ) { int a ; a = retfun( ); printf(“a = %d”,a) getch( ); }
Function Declaration • Any C function by default returns an int value. • If a function should return a value other than integer. • then it is necessary to explicitly mention so in the calling function declaration or function prototype.
The general format is: • data-type function-name (list of parameters);
# include <stdio.h> main ( ) { float x, y ; float div (float a, float b); printf (“enter 2 number”); scanf (“%f%f”,&x,&y); printf (“The div value is %f” ,div (x,y); getch ( ); } Float div (a,b) Float a,b; { Return(a/b); }
Void Function Type • If the function returns no value then it is called void function. • Such function must be declared as a void function. • In general void is an empty data type. General form • Void function name ( )
Example-1 #include<Stdio.h> #include<Conio.h> void welcome (char name [ ]); { Printf (“ Welcome % s” name); }
Example-2 # include <stdio.h> void fun1 (void); void fun2 (viod); main ( ) { clrscr ( ) ; fun1 ( ) ; getch ( ) ; } void fun2(void) { Printf (“C Programming”); }
Parameter in Functions A function is called by specifying its name followed by a list of arguments. When a function is invoked, the formal parameters are initialized to the actual parameters used in the function call operator . The corresponding arguments in the function Program are called actual parameter.
Declaration main ( ) { a = area (l,b); } { area (x,y) …… } main ( ) { int a,b; result = mul (a,b): …… } mul(i,j); int i,j; { …… }
LIBRARY FUNCTIONS • C offers a rich collection of library functions. • Any of these functions can be called from any C program. • To invoke these functions we have to include the appropriate header file in the program.
Utility Functions • Utility Functions is symbol by <stdlib.h>. • It is an header file. • Utility Functions has servral types. Following are given below
atof ( ) • This is used to convert a string into a floating-point value. • The general format is atof (string); Example: main ( ) { char a [20] = “222.33”; float c; C = atof (a); } The output will be C = 222.33
atoi ( ) This function is used to convert a string into an integer value..The general form is: atoi (string); Example: main ( ) { char a[20] = “2233”; int c; c = atof(a) + 5; } The output is C= 2238
Mathematical Functions • Mathematical Functions Symbol <math.h> • It has several types. Following are given below;- floor ( ) • This function is used to find the floor of a value x.
The floor of x is the first smaller integer. The general format is: • Floor (x); • Example:- • floor (2,9) = 2 • floor (-2.1) = 3
ceil ( ) This function returns the round value of the nearest highest integer. The general format is: ceil (x); Example: ceil(2.6) = 3 ceil(5.2) = 6 ceil(-4.3) = -4
pow ( ) This function is used to find xy. The general format is: Pow (x,y); Example: pow (2.3); The output is 8.
exp ( ) • This function is used to find the exponential value. • The general format is: • Exp(x);
String Function • String Function is symbol <string.h> • It also using character and words. • These functions are for string manipulations. These functions can be classified according to their performance as follows:-
Joining of two strings 1) strcat 2) strncat • strcat: This function is used to add two strings. The result is stored in the destination string that is the first string. The general form is: strcat(string 1, string 2);
ii. strncat: This function is similar to ‘strcat()’ except. That it binds only n characters of string 2 with string 1. The general form is: strncat(string 1, string 2, n);
Conversion of Strings 1) strlur: This function is used to convert a string containing uppercase to lowercase. The general Format is: strlwr(string);
2) strupr: This function is used to convert a string containing lowercase to uppercase. The general format is: strupr (string); 3) strrev: This function is used to reverse the string. The general format is strrev(string);
4) strset: This function is used to replace all the characters in a string with a particular given character. The general format is: strset(string, char); where the string will be replaced by the character.
5) strnset: This function is similar to strset ( ). The only difference is that we can specify the number of characters to be replaced in the given string. The general format is: strnset(string, char , n);
Comparison of string • strcmp This function is used to compare the two strings. The function is case sensitive. The general format is: • strcmp (string 1, string2); • Example: • string 1 = “Canada” • string 2 = “Africa”
2) strncmp : This function will compare just n characters of string 2 with string 1. This is case sensitive. The general format is: stricmp(string 1, string 2, n);
3) stricmp : This function is similar to strcmp(). It is an except that this is non-case sensitive. The general format is: stricmp(string 1, string 2);
Other String Functions • Strlen This function is used to find the length of the string. This function will not count null-terminating character. The general format is: strlen(string);
2) strcpy This function is used to copy string 2 to string 1 including the null terminating character of string. The general format is: strcpy (string1, string2); Here string 1 is the destination where the string 2 will be copied. After the execution the old characters in string 1 will be completely erased.
3) Strncpy This function is similar to strcpy(), except that it copies n characters on string 2 to string 1. The general format is: strncpy(string1, string, n);
Character Class Test Functions Character class test functions is symbol by <ctype.h>. • isalnum( ) Tests whether a character is an alphabet or a number. Isalnum(intc);
2. islower ( ) • Test whether a charcter is a lower case or not. • Islower (int c); • 3. isupper ( ) • Tests whether the charcter is a uppercase. • Isupper(int c);
2. islower ( ) Test whether a character is a lower case or not. Islower (int c); 3. isupper ( ) Tests whether the charcter is a uppercase. Isupper(int c);
4) tolower( ) This function translates character to lowercase. Tolower(intc); 5. toupper ( ) This function translates character to uppercase. Toupper(int c);