Wednesday, June 21, 2023

C Language Function

 

Chapter-5

FUNCTION

Pre defined function

1. numeric or mathematical function

(#include<math.h>)

2. string function

(#include<string.h>)

3. single charter function

(#include<ctype.h>)

Numeric function

mathematical function is defined as the header file <math.h> before using math function we should include the header file

<math.h>

abs:

returns the absolute value of a number , a number without its sign.

syntax :abs(argument)

example :abs(-25)

result : 25

sqrt :

returns the square root of the number.

syntax :sqrt(argument)

example :sqrt(25)

result : 5

sin :

returns the sin value of an value.

syntax :sin(argument)

example :sin(90)

result :

the same format is applicable to find tan , cos values.

log:

returns the logarithmic values of a number.

syntax :log(argument)

example :log(10)

result : 1

floor :

returns the previous integer number to the given decimal value.

syntax :floor(argument)

example :floor(23.22)

result : 23

ceil :

returns the next integer number to the given decimal value.

syntax :ceil(argument)

example :ceil(34.34)

result : 35

pow :

returns the number raised to the power values.

syntax :pow(argu1,argu2)

argument 1 must be a number and argument 2 must be a power value.

example :pow(2,3)

result : 8

single char function

function used to handle many tasks on single character are called single character function. if the value return by the function is zero(0) then the condition is false and if it is positive number then the condition is considered as true.

header file:- ctype.h

isspace()  - to check the char is space or not.

syntax: issapce (character)

              character may be any character constant.

example:  isspace("c")

result : any positive value (true)

isupper : to check the given char is upper or not

syntax : isupper(character)

example : isupper("c")

result : zero (0).(false)

since c is not in upper case the condition becomes false and the function returned to 0.

islower() - to check the char is lower or not

syntax : islower (character)

example : islower("c")

result : any positive value true

             since c is in lower case the condition becomes true the function returned 0.

toupper() - it converts the given character into upper case.

syntax : toupper (character)

example : toupper("a")

result : A

tolower() - it converts the given character in to lower case

syntax : tolower(character)

example : tolower("A")

result : a

isalpha() - to check the given character is an alphabet

syntax : isalpha(character)

example : isalpha("a")

result : any positive value true

isdigit() - to check tthe given character is a numeric value

syntax : isdigit(character)

example : isdigit("a")

result : 0 (false)

 

example :

#include<stdio.h>

#include<conio.h>

main()

{

char c;

clrscr();

printf("enter a character");

scanf("%c",&c);

printf("isspace %d\n",isspace(c)); 

printf("isupper%d\n",isupper(c)); 

printf("islower%d\n",islower(c)); 

printf("toupper%c\n",toupper(c)); 

printf("tolower%c\n",tolower(c)); 

printf("isalpha%d\n",isalpha(c)); 

printf("isdigit%d",isdigit(c));

getch();

}

output:

enter a character

r

isspace 0

isupper 0

islower 8

toupper R

tolower r

isalpha 8

isdigit 0

User defined function:


Advantages:

vfunction make the program much easier to understand , debug and test.

vit saves time and memory space

 

steps in writing function:

1.  function declaration

2.  function call

3.  function definition

1. function declaration

syntax : returndatatype funcname(argument datatype,..);

2. calling the function

syntax : funcname (argument1,argument2...);

arguments may be constant, variables.

3. function definition

return_type function_name(argument list)

/*first line*/

definition_of_argument_list_members

/*argument declaration*/

{

local function variable;

function executable statements; /*body of the function*/

return(return value);

}

example:

#include<stdio.h>

#include<conio.h>

main()

{

int addnum(int,int);  /*function declaration*/

int sum,a,b;

printf("\n enter two number to be summed :");

scanf("%d%d",&a,&b);

sum=addnum(a,b);     /*function call*/

print("/n the sum of %d and %d is %d",a,b,sum);

getch();

}

int addnum(int num1,int num2)/*function definition*/

{

int tot;

tot=num1+num2;

return(tot);

}

output

enter two numbers to be summed :12,13

the sum of 12 and 13 is 25

 

Function Reference

Example

#include<stdio.h>

#include<conio.h>

main()

{

int maximum(int,int);

int a,b,c,d;

/*read the integer quantities*/

printf("\n a=");

scanf("%d",&a);

printf("\n b=");

scanf("%d",&b);

printf("\n c=");

scanf("%d",&c);

/*calculate and display the maximum value*/

d=maximum(a,b);

printf("\n\n maximum=%d",maximum(c,d));

}

int maximum(int x,int y)

/*determine the larger of two quantities*/

{

int z;

z=(x>=y)?x:y;

return(z);

}

output:

a=10

b=20

c=30

maximum=30

category of functions

1.   without argument without return type

2.  without argument with return type

3.  with argument without return type

4.  with argument with return type

No argument and No return values

In this type the function does not receive any data from the calling function,and it does not return a value. There is no data transfer between the calling function and the called function.

Example

#include<stdio.h>

#include<conio.h>

main()

{

 void test(); /*function declaration*/

clrscr();

test();           /*function call*/

test();          /*function call*/

getch();

}

void test()  /*function definition*/

{

printf("\nFunction has been called");

}

output:

Function has been called

Function has been called

 

 

Function with argument and no Return value

In this type of functions the calling function provide some data to the called function and the calling function does not receive any value from the called function.

Example

#include<stdio.h>

#include<conio.h>

main()

{

void line(int);/*function declaration*/

clrscr();

line(60);         /*function call*/

printf("\n Test for function call\n");

line(60);      /*function call*/

printf("\n Test Succeeded\n");

line(15);

getch();

}

void line(int b)

{

int i=0;

for(i=0;i<=b;i++)

printf("%c",'-');

}

output:

-------------------------------------------------------------------------          Test for  function call

-------------------------------------------------------------------------

        Test succeeded

---------------

Function without argument and with return value

In this method the function returns some data to the called function and it does not receive any value from

the calling function.

Example

#include<stdio.h>

#include<conio.h>

main()

{

int add();     /*function declaration*/

int r;

clrscr();

r=add();      /*function call*/

printf("%d",r);

printf("%d",add()); /*function call is present in the    printf()*/

getch();

}

int add()     /*function definition*/

{

int a,b,c;

printf("\n enter two numbers");

scanf("%d %d",&a,&b);

c=a+b;

return(c);

}

output

enter two numbers 56 67

123

enter two numbers 67 45

112

function with argument and with return value

in this method the function receives some data from the calling function(main()) and send some value to the calling function(main())

 example

#include<stdio.h>

#include<conio.h>

main()

{

int add(int,int);          /*function declaration*/

int a,b,r;

clrscr();

printf("enter two numbers");

scanf("%d%d",&a,&b);

r=add(a,b);                        /*function call*/

printf("%d",r);

getch();

}

int add(int x,int y)            /*function definition*/

{

int c;

c=x+y;

return(c);

}

output

enter two numbers 56 67

123

Storage Classes in C

Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C

  • Automatic
  • External
  • Static
  • Register

Storage Classes

Storage Place

Default Value

Scope

Lifetime

auto

RAM

Garbage Value

Local

Within function

extern

RAM

Zero

Global

Till the end of the main program Maybe declared anywhere in the program

static

RAM

Zero

Local

Till the end of the main program, Retains value between multiple functions call

register

Register

Garbage Value

Local

Within the function

Automatic(auto)

  • Automatic variables are allocated memory automatically at runtime.
  • The visibility of the automatic variables is limited to the block in which they are defined.

The scope of the automatic variables is limited to the block in which they are defined.

  • The automatic variables are initialized to garbage by default.
  • The memory assigned to automatic variables gets freed upon exiting from the block.
  • The keyword used for defining automatic variables is auto.
  • Every local variable is automatic in C by default.

Example

#include <stdio.h>  

#include<conio.h>

main()  

{  

int a; //auto  

char b;  

float c;   

printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.   

getch();

}  

Output:

garbage garbage garbage

842-28876  §  -0.000000

Static(static)

  • The variables defined as static specifier can hold their value between the multiple function calls.
  • Static local variables are visible only to the function or the block in which they are defined.
  • A same static variable can be declared many times but can be assigned at only one time.
  • Default initial value of the static integral variable is 0 otherwise null.
  • The visibility of the static global variable is limited to the file in which it has declared.
  • The keyword used to define static variable is static.

Example

#include<stdio.h> 

#include<conio.h> 

static char c;  

static int i;  

static float f;   

static char s[100];  

main ()  

{  

printf("%d %d %f %s",c,i,f); // the initial default value of c, i, and f will be printed.   

getch();

}  

Output:

0 0 0.000000 (null)

Register(register)

  • The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU.

Example

#include <stdio.h>  

#include<conio.h>

main()  

{  

register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0.   

clrscr();

printf("%d",a);  

getch();

}  

Output:

842

External(extern)

  • The external storage class is used to tell the compiler that the variable defined as extern is declared with an external linkage elsewhere in the program.
  • The variables declared as extern are not allocated any memory. The default initial value of external integral type is 0 otherwise null.

Example

#include <stdio.h>  

#include<conio.h>

int a;   

int main()  

{  

extern int a; // variable a is defined globally, the memory will not be allocated to a  

printf("%d",a);  

getch();

}  

Output

0

 

0 comments:

Post a Comment