C Language Video

Back - C Language Notes By Vivek Sir email: vivekdubey22@gmail.com (w) 9826424484

User Defined Function

Why we need functions in C?

Functions are used because of following reasons –

·       To improve the readability of code.

Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.

·       Debugging of the code would be easier if you use functions, as errors are easy to be traced.

·       Reduces the size of the code, duplicate set of statements are replaced by function calls.

Text Box: return_type  function_name ( argument list )
{
    Set of statements – Block of code
}
Explain Syntax and Terminology used in a function.

 

 

 

 

 

 

 

 

·       return_type:

Return type can be of any data type such as int, double, char, void, short etc.

·       function_name:

It can be anything, however it should be a meaningful name for the functions so that it would be easy to understand the purpose of function just by seeing it’s name.

·       argument list:

Argument list contains variables names along with their data types. These arguments are kind of inputs for the function.

·       Block of code:

Set of C statements, which will be executed whenever a call will be made to the function.

 

How to call a function in C?


Example-1: WAP to display WELCOME using function.

#include<stdio.h>

void Display()

{

printf(“welcome”);

}

 

void main()

{

Display();

}

 


Example-2: WAP to Add two number using Function.

#include<stdio.h>

void Add()                  

{

int A,B;

scanf(“%d %d”, &A,&B);

prinf(“%d” A+B);

}

void main()

{

Add();

}


Type of User-Defined Function

Case 1:     No Way Communication (No arguments passed and no return value)

Example-1

#include<stdio.h>

#include<conio.h>

void ADD()

{       int A,B,C;

scanf(“%d %d”, &A, &B);

        C = A + B;

printf(“%d”, C);

}

void main()

{      

Add();

}


Case 2:     One Way Communication (arguments passed and no return value)

Example-2

#include<stdio.h>

#include<conio.h>

void ADD(int A, int B)

{       int C;

        C = A + B;

printf(“%d”, C);

}

void main()

{       int X,Y;

scanf(“%d %d”, &X, &Y);

Add(X,Y);

}

 

Case 3:     One Way Communication (No arguments passed and return value)

Example-4

#include<stdio.h>

#include<conio.h>

int ADD(int A, int B)

{       int A,B,C;

        C = A + B;

return C;

}

void main()

{       int Z;

Z = Add();

printf(“%d” , Z);

}

 

Case 4:     Two Way Communication (arguments passed and return value)

Example-4

#include<stdio.h>

#include<conio.h>

int ADD(int A, int B)

{       int C;

        C = A + B;

return C;

}

void main()

{       int X,Y,Z;

scanf(“%d %d”, &X, &Y);

Z = Add(X,Y);

printf(“%d”, Z);

}

 

C Language Video

Back - C Language Notes By Vivek Sir email: vivekdubey22@gmail.com (w) 9826424484