User defined functions


Function:
  • A function is a set of instructions that are used to perform specified tasks that repeatedly occur in the main program.
  • It helps to avoid duplication of work and also break down a large program into a number of small functions.
  • It is used to divide the complex task into manageable task.
  • Functions are classified into two types as shown below,

              * User-defined functions
              * Pre-defined functions
1.User defined function:
  • The users have full scope to implement their own ideas in the function.
  • It defined by the users according to their requirements are called user-defined functions.
  • Elements of user-defined function:

               * Function declaration
               * Function definition
               * Function call

Syntax of user-defined function:

<Header_files>
Main_function()
{
variable_declaration;
//function declaration part
return_type or data_type function_name(parameters list);
// function calling part
function_name();//no parameter passed
or
function_name(parameter);//parameter passed
or
return value=function_name(parameter);
//printing statements
}
//function definition part
Data_type function_name(parameters list)
{
Local variable declaration;
......
Body of the function;
return (expression);
}

Parameters:
It provides data communication between the calling function and called function.
There are two types of parameters,
(i). Actual parameters:
Used in data transferred from the main function to the user defined function.
(ii). Formal parameters:
Used in data transferred from a user-defined function into the main function.
For example:


Where
          a, b are the actual parameters.
          x,y are the formal parameters.
Local and global variables:
Local variable:
The local variables are defined within the body of the function.
This variable used within the function only, other functions can not access these variables.
Example:

fun1(int a, int b)
{
          int c,d;
}

Global variable:
The global variables are defined outside the main () function, multiple functions can use these variables.
Example:

int x,y=10;
main()
{
          int a,b;
}

Example program for local and global variables.

#include<stdio.h>
#include<conio.h>
int i; //Global variable
void main()
{
          int j; //Local variable
void fun1();//Function declaration
i=0;
clrscr();
printf("Value of i in main function: %d\n",i);
fun1();//Function calling
printf("Value of i in after call: %d\n",i);
getch();
}
//Function definition
void fun1()
{
int k; //Local variable for fun1()
i=50;
}

Output:
The return statement:
A return statement is an optional part of the function.
It is used to populate the results without any printing function.
Syntax:

return; or return(expression);

Example:

if(x>0)
          return(1);
else
          return(0);

Function calling:
  • A function can be called by specifying the function name in the source program with parameters if presence within parentheses.
  • If a function is not declaring the variable to return, we must be specified the data type at return value.

For example:

  • It is divided into two types,

1.Function call without a parameter:

#include<stdio.h>
#include<conio.h>
void main()
{
msg_func();//Function definition without parameter and
                          called the function
printf("\n Message from main program or function");
getch();
}
//Function definition
msg_func()
{
clrscr();
          printf("\n Message from msg_func() user defined function");
}

Output:
2.Function call with parameter:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
a=msg_func(10,20);//Function definition with parameter and 
                                   called the function
printf("\nMessage from main program or function");
printf("\nThe value of a is %d",a);
getch();
}
//Function definition
int msg_func(int x,int y)
{
int z;
z=x+y;
printf("\nMessage from msg_func() user defined function");
return(z);
}

Output:



Share:

Handling of strings


String manipulation:
  • In the ‘C’ language, the group of characters, digits, and symbols enclosed within quotation marks are called as a string, otherwise strings are an array of characters.
  • The string is nothing but, it is a collection of character or words.
         For example, The word is Whereisstuff
         Here, w, h, e, r, e, i, s, t, u, f, f such as characters and Whereisstuff as string
  • The null character (‘\0’) is used to mark the end of the string. 
Syntax:

char name[]=”String”; //initialization
or
char name[Set maximum length of the string];//declaration

Example:

char name[]=”whereisstuff”;
or
char name[20];

String handling function:
Most widely used functions are,
1. The strlen() function:
It is used to count and return the number of characters presents.
Syntax:

Variable_name=strlen(string);

Example program strlen():

#include<stdio.h>
#include<conio.h>
void main()
{
char a[30];
char b[]="whereisstuff";
int len=strlen(a);
clrscr();
printf("Enter a string: \n");
scanf("%s",&a);
printf("The length of %s string is:  %d\n",a,len);
printf("String length of %s is %d \n",b,strlen(b));
getch();
}

Output:
2. The strcpy() function:
It is used to copy the content of one string to another and it almost works like a string assignment operator.
Syntax:

strcpy(string1,string2);

Example program strcpy():

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[30];
char str2[]="whereisstuff";
clrscr();
strcpy(str1,str2);
printf("\n Original string is %s",str2);
printf("\n Copied string is %s",str1);
getch();
}

Output:
3. The strcat() function:
It is used to concatenate or combine, two strings together and forms a new string.
Syntax:

strcat(string1,string2);

Example program strcat():

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[]="whereisstuff";
char str2[]=".com";
clrscr();
printf("\n Concatenate string is %s", strcat(str1,str2));
getch();
}

Output:
4. The strcmp() function:
It is used to compare two strings to find out whether they are the same or different.
Syntax:

strcmp(string1,string2);

Example program strcmp():

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("\n  Enter two string: ");
scanf("%s %s",&str1,&str2);
if (strcmp(str1,str2)==0)
{
printf("\n Compared string is right because 
two strings are the same case.");
}
else
{
printf("\n Compared string is wrong because
 two strings are different case");
}
getch();
}

Output:
if condition:
else condition:
5. The strrev() function;
It is used to reverse a string.
Syntax:

strrev(string);

Example program strrev():

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
          printf("\n Enter a string: ");
          scanf("%s",&str1);
printf("\n The reversed string is %s",strrev(str1));
getch();
}

Output:
6. The strlwr() function:
It is used convert string into lower case(small letters).
Syntax:

strlwr(string);

Example program strlwr():

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
          printf("\n  Enter a string in capital letters ");
          scanf("%s",&str1);
printf("\n Lower string is %s", strlwr(str1));
getch();
}

Output:
7. The strupr() function:
It is used convert string into upper case(capital letters).
Syntax:

strupr(string);

Example program strupr():

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
          printf("\n  Enter a string in small letters ");
          scanf("%s",&str1);
printf("\n Upper string is %s", strupr(str1));
getch();
}

Output:

8. The stricmp() function:
It is used to compare two strings, no difference between small and capital letters.

Syntax:

stricmp(string1,string2);

Example program stricmp():

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
          printf("\n  Enter two string: ");
          scanf("%s %s",&str1,&str2);
          if (stricmp(str1,str2)==0)
          {
          printf("\n Compared string is right because 
two strings are the same.");
          }
          else
          {
           printf("\n Compared string is wrong because 
two strings are different.");
          }
getch();
}

Output:
if condition:
else condition:
Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me