fb popup

/* remove if it's exist in your template */

Functions

Functions are generally used to reduce confusion and also for reuse of the code.

You may think,We already have "for" loop for reusing a group of commands but what if we want to invoke a set of code again and again in different parts of the program then we need a concept called function.

There are 2 types called function and calling function. Called function is a function which has been called by the calling function. Confused!

Simply Calling function is in the "main()" part and the called function will be outside of the main() function(remember main() is also a Function)

Before using functions in your program,you have to declare the function globally so that it can be used any where in your program. In order to declare it the syntax is:-

 Syntax for Function declaration:
return_type function_name(parameters);
This is to be given globally that means outside the main(). Then how to write it's definition i.e the code within the function so that it will execute the code within it after calling the function from main().

Syntax:-
returntype Function_name(parameters)
{
....................
Code.............
.....................
}

Ok  Let me explain this clearly with a program.  

Program:-
Before using function you have to declare it above main() as shown and it is your wish to give variables in declaration.
#include<stdio.h>
int sum(int,int);  //function declaration
                   /* You can also declare it as 
                       int sum(int a,int b)
                       both have no problem*/
void main()
{
int num1,num2,result;
printf("Enter number1: ");
scanf("%d",&num1);
printf("Enter number2: ");
scanf("%d",&num2);
result=sum(num1,num2);    //calling function
printf("The sum of %d and %d is %d",num1,num2,result);

}  

int sum(int a,int b)   //Called function
{
int c;
c=a+b;
return(c);
}
Output:-

Functions return type

By now I guess everyone can understand the normal code so I won't go in detail but the main part of the code  
Explanation:-
1.Num 1 and Num 2 are taken from the user which are sent to the function outside the "main()" (i.e. Called function and the function sending is calling function)
2. Then the numbers are taken as parameters by called function where they are added and stored in variable "c".
3.This "c" which stores the value of "a+b" is returned back to main() where the calling function located and then the value sent from called function (i.e. a+b) is stored in variable "result".
4.The result is then printed.
Note:- If there is no return in your function you need to use void as your return type
Program without return type:-
#include<stdio.h>
void sum(int,int);  //function declaration
/*change from int to void as no return value is there from called function to calling function*/
               
void main()
{
int num1,num2,result;
printf("Enter number1: ");
scanf("%d",&num1);
printf("Enter number2: ");
scanf("%d",&num2);
sum(num1,num2);    //calling function
}  

void sum(int a,int b)   //Called function
{
int c;
c=a+b;
printf("The sum of %d and %d is %d",a,b,c); 
}
Output:-

Functions without return type


There is no change in the output except the user input.  

Now you may ask me why do we do in this manner and why should we show our nose in the longest way while there is the shortest way.  

Yeah of course for this small program it might be like that but there are real time programs where you find the use of this.  

I will show a few of them, then you will really appreciate this concept.
Example:- Let us find solution for the expression result=4 r2 s3 y4 + 5 r4 s2 y
 
#include<stdio.h>
int square(int);
int cube(int);
int topower4(int);
void main()
{
int r,s,y;
int result;
printf("Enter r,s,y in given order in integers:\n");
scanf("%d",&r);
scanf("%d",&s);
scanf("%d",&y);
result=(4*square(r)*cube(s)*topower4(y))+(5*topower4(r)*square(s)*square(y));
printf("The result of 4 r2 s3 y4 + 5 r4 s2 y2 is %d ",result);
}
int square(int num)
{
return(num*num);
}
int cube(int num)
{
int s=square(num);
return(s*num);
}
int topower4(int num)
{
int s=square(num);
return(s*s);
}
Output:-  

Expression Output


Explanation:-
1.Here we have three functions named
  • square()-for getting square of a value
  • cube()-for cube of value
  • topower4-for getting power 4 of the given value
2.all the values for r,s,y are taken from user. 3.Then the numbers are sent to their corresponding functions for getting their value. 4.Then finally the result is printed. When you observe you find functions with in function. Don't confuse,functions can be called even outside of the main. You can take this as an example. Here for getting cube we sent the value for getting it's square and then multiplied with the same number which are same.i.e 2*2*2=8 as well as square(2)*2 =8 both are nothing but same. //ly 2*2*2*2=16 as square(2)*square(2)=16 This is what I did there.

No comments:

Post a Comment