Function Examples (No Pass and No Return)Set 1

No Pass No Return

In no pass no return, no value is passed from the calling function to the called function and no value is returned back from the called function to the calling function.

Question:1
Write a function in C language to calculate and print the square of a number?
Sol:

#include <stdio.h>
void square(); //function declaration
int main()
{
square(); //function calling
return 0;
}
//function definition
void square()
{
int a,b;
printf("Enter any no ");
scanf("%d",&a);
b=a*a;
printf("square = %d",b);
}

•The function definition can be given either above or below the calling function (main function).
•If the definition of the function is given above the calling function [main ()] then giving function declaration becomes optional.

Method :2

#include <stdio.h>

//function definition
void square()
{
int a,b;
printf("Enter any no ");
scanf("%d",&a);
b=a*a;
printf("square = %d",b);
}
int main()
{
square(); //function calling
return 0;
}

Question:2
Write a function in C language to calculate and print sum of 2 numbers?
Sol:

#include <stdio.h>

//function definition
void sum()
{
int a,b,c;
printf("Enter 2 nos ");
scanf("%d %d",&a,&b);
c=a+b;
printf("sum = %d",c);
}
int main()
{
sum(); //function calling
return 0;
}

Question:3 
Write a function in C language to take input for three numbers check and print the largest number?
Sol:

#include <stdio.h>

//function definition
void largest()
{
int a,b,c,m;
printf("Enter 3 nos ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
m=a;
else
if(b>c)
m=b;
else
m=c;
printf("Max no = %d",m);
}
int main()
{
largest(); //function calling
return 0;
}

Question: 4
Write a function in C language to take input for two numbers to calculate and print their difference?
Sol:

#include <stdio.h>

//function definition
void diff()
{
int a,b,c;
printf("Enter 2 nos ");
scanf("%d %d",&a,&b);
if(a>b)
c=a-b;
else
c=b-a;
printf("diff = %d",c);
}
int main()
{
diff(); //function calling
return 0;
}

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects