1 / 42

Data Structure and C Part-6

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.

tulia
Télécharger la présentation

Data Structure and C Part-6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure and CPart-6

  2. 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.

  3. Example of Code Program # include <stdio.h> main( ) { ------  Calling ------ Function ------ } ------ } calculation() int…….; { ------  Called ------ Function ------ }

  4. Structure of a Function The general structure is return type function name (list of parameters) Syntax: parameter declaration. { Statements : }

  5. 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.

  6. 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); }

  7. 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.

  8. 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( ); }

  9. 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.

  10. The general format is: • data-type function-name (list of parameters);

  11. # 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); }

  12. 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 ( )

  13. Example-1 #include<Stdio.h> #include<Conio.h> void welcome (char name [ ]); { Printf (“ Welcome % s” name); }

  14. Example-2 # include <stdio.h> void fun1 (void); void fun2 (viod); main ( ) { clrscr ( ) ; fun1 ( ) ; getch ( ) ; } void fun2(void) { Printf (“C Programming”); }

  15. 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.

  16. Declaration main ( ) { a = area (l,b); } { area (x,y) …… } main ( ) { int a,b; result = mul (a,b): …… } mul(i,j); int i,j; { …… }

  17. 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.

  18. Utility Functions • Utility Functions is symbol by <stdlib.h>. • It is an header file. • Utility Functions has servral types. Following are given below

  19. 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

  20. 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

  21. 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.

  22. The floor of x is the first smaller integer. The general format is: • Floor (x); • Example:- • floor (2,9) = 2 • floor (-2.1) = 3

  23. 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

  24. pow ( ) This function is used to find xy. The general format is: Pow (x,y); Example: pow (2.3); The output is 8.

  25. exp ( ) • This function is used to find the exponential value. • The general format is: • Exp(x);

  26. 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:-

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

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

  29. Conversion of Strings 1) strlur: This function is used to convert a string containing uppercase to lowercase. The general Format is: strlwr(string);

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

  31. 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.

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

  33. 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”

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

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

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

  37. 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.

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

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

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

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

  42. 4) tolower( ) This function translates character to lowercase. Tolower(intc); 5. toupper ( ) This function translates character to uppercase. Toupper(int c);

More Related