C – Functions

A function cannot be defined inside the another function, but a function can be called inside a another function. Functions cannot return more than one value at a time. because after returning a value the control is given back to calling function. Any function including main() can be called recursively. C has two kind of functions User Defined Functions,  Pr-Defined Functions.


User – defined functions

  • Function declaration syntax: returntype function_name(type_1, type_2,....);
  • Function definition syntax:
  • Function call syntax : function_name ();
  • Function passing arguments syntax: function_name (arg_1, arg_2, ...);
    • Arguments (Two types)
      • Actual arguments : Actual arguments are comes with arguments when function call.
      • formal arguments : Formal arguments or dummy arguments are comes in function definition.
  • Function Name : If two function are declared in a same name, it gives "Error: Multiple declaration of function_name())" .
    • Return value : A function can return floating point value, integer, decimal, Boolean,
    • Return statement :
      • A function may have any number of return statements each returning different values and each return statements will not occur successively.
      • Yes. If a function contains two return statements successively, the compiler will generate “Unreachable code” warnings

         
    • Return Type :
      • If a function return type is declared as void it cannot return any value
      • If return type for a function is not specified, The default return type for a function is int .
    • Type of User Defined functions
      • function with no arguments and no return value.
      • function with arguments and no return value.
      • function with arguments and return value.
      • function with no arguments and return value.
  • Functions Arguments : C can accept upto 127 maximum number of arguments in a function.
  • Calling functions ( A function can be called either call by value or call by reference)
    • call by value : Actual argument value is copied to formal arguments.
    • call by reference : Actual argument address is copied to formal arguments.
  • Advantage of functions
    • Using top-down modular programming.
    • User can understand easily when separate many function.
    • Reduce the code repetition.
    • Can change function into program.
    • Program testing is very simple.
  • Disadvantage of functions
    • Difficult to understand function structure.
    • Users can’t understand easily some like recursive function.
    • Program execution control sequence is not in run time
    • Can’t return more than one value.
    • can’t see separate function use.

 

Recursive function

When a recursive call is made, the function/process clones itself and then process that funtion. This leads to time and space constrains. In a loop, there is no recursive call involved that saves a lot of time and space too. Too many recursive calls may result into stack overflow . because when a function is called its return address is stored in stack. After sometime the stack memory will be filled completely. Hence stack overflow error will occur

 



 

Pre – defined functions

Pre – defined Library functions are  stdio.h , math.h ,  string.h  ,   conio.h  , ctype.h  , graphics.h

 

stdio.h (Input output statements)

  • These statements are used to get and put a data to computers
  • Ex:
    • Can put a data to computer via keyboard.
    • Can get a data and see that in Monitor.
  • Input output statements are used as a functions in c language
  • stdio.h file contains that statements statements
  • So if we use these functions should include stdio.h , functions are   scanf()  , printf()   , getchar() , gets() , putchar()   ,   puts()  .

 


 

scanf() function

  • Syntax :
    • int scanf(char *control_string, argument_list);
    • scanf("control strings", &variable_1, &variable_2....&variable_n);
    • control sting : %w -( % – conversion specification indicator, W -width of input data)
    • & – address of the variable
  • Rules
    • Should use comma , to separate the different variables
    • Same data type variable and data specifier should use.
  • Returns
    • scanf()  returns the number of data items successfully assigned a value. if an error occurs, EOF  is return.
  • Examples
    • scanf("%d", &a);
    • scanf("%f", &a);
    • scanf("%d %f %d", &a, &b, &c);
    • scanf("%c", &sex);
    • scanf("%s%c", &name, &sex);
    • scanf("%4d", &a);
    • scanf("%5f", &a);
    • scanf("%20s", &name);

printf() function

  • syntax : int printf(char *control strings, argument_list);
  • Data format : %w.p data-type
  • % -conversion specification indicator
  • w – width of the output data (optional)
  • p – point (dot)
  • Examples ;
    • printf("%d",x);
    • printf("%3d",x);
    • printf("%5d",x);
    • printf("%-5d",x);
    • printf("%07d",x);
    • printf("%4d",-x);
    • Print the \n  using printf function
      • Ans : printf("\\n");

 

 


getchar() function

  • Syntax: var = getchar();

gets() Function

  • Syntax : var = gets()

putchar() function

  • Syntax : putchar(var);

puts() Function

  • Syntax : puts("ArunEworld"


 

ctype.h (characters statements)

Use of this functions is know which type of characters  isdigit() , isalpha()  ,  isupper()  , islower()  ,  spunct()  ,

isdigit() function

  • Used to check is character is numeric digit or not.
  • Syntax : int isdigit(c);
    • here c is a variable or constant.
  • Returns : True – Non-zero, false – Zero.

isalpha() function

  • Used to check is character is alphabet or not.
  • Syntax : int isalpha(c);
    • here c is a variable or constant.
  • Returns : True – Non-zero, false – Zero.

isupper() function

  • Used to check is character is upper or not.
  • Syntax : int isupper(c);
    • here c is a variable or constant.
  • Returns : True – Non-zero, false – Zero.

islower() function

  • Used to check is character is lower or not.
  • Syntax : int lower(c);
    • here c is a variable or constant.
  • Returns : True – Non-zero, false – Zero.

ispunct() function

  • Used to check is character is punctuation or not.
  • Syntax : int ispunct(c);
    • here c is a variable or constant.
  • Returns : True – Non-zero, false – Zero.

 



string.h functions

String is a collection of characters in between the double quotation ". In C language does not have a data type like string, so we can get this from array of characters. Last character of sting should be null characters '/0' . Generally compiler will add the null characters when read a string from computer. Dose not appear null characters '/0' when print the string in computer display. Data length is number of characters in the string with null characters. string handling functions  strlen()strcpy()  ,  strcat()  ,  strcmp()  ,  strrev()  ,  strupr()   ,  strlwr()  ,  strrev()  ,

 

  • Declaring string :Syntax – char string_name[size]; //size-maximum number of characters including ‘0/’.
    • Example : char city [10];
  • Initializing string :Syntax – static char variable_name [size] = str;
    • Example
      • static char city[] = "ArunEworld";
      • static char city[10] = "ArunEworld";
      • static char city[10] = {'A','r','u','n','E','w','o','r','l','d'};
    • Reading string
      • scanf()
        • Example : char name[50]; scanf(%s", name);
      • getchar()
      • gets()
    • Printing String
      • printf()
        • printf("%s", var_name);
      • puts()
        • puts(var_name);

 

strlen() functions

  • Syntax ; int strlen(char *string);

Exerciser :  find the length of the string using for loop.

 

strcpy(destination, source); functions

  • Syntax : void strcpy(char *string_!, char *string);

strcmp() functions

  • Syntax : int strcmp(char *string_1, char *String_2); // strcmp return (Zero) if both the strings are equal , else return -1
  • Example :

strrev() functions

  • syntax: char *strrev(char *string_1);

strupr() functions

  • syntax : char* strupr(char *string_1);

strlwr()

  • syntax : string *strlwr(char *string_1);

strcat()

  • Syntax : char *strcat(char *dest, const char *src);
  • syntax ; void strcat( char *string_1, char *string_2);
  • The strcat() function is defined in <string.h> header file.

Exercise :

 

 

getch()

The getch() function reads a single character from keyboard. It doesn’t uses any buffer, so entered data is not displayed on the output screen.

getche()

  • The getche() function reads a single character from keyword but data is displayed on the output screen. Press Alt+f5 to see the entered character.

sprintf function

  • sprintf() // Prints the formatted output onto the character array.

scanf function

  • syntax : scanf("<format string>", <Address of variables>);

fputc() Function :

  • Syntax : fputc(character, file_pointer);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

if you have any queries call us +918300026060 or WhatsApp Dismiss

0

Your Cart

%d bloggers like this: