Functions
Functions
Need of a Function
Knowingly or unknowingly we rely on so many persons for so
many things. Man is an intelligent species, but still cannot
perform all of life’s tasks all alone. He has to rely on others.
For Example:
You may call a mechanic to fix up your bike, rely on a store to
supply you groceries every month.
A computer program finds itself in a similar situation. It
cannot handle all the tasks by itself. Instead, it requests other
program like entities—called ‘functions’ in C—to get its tasks
done. So now we will look at a variety of features of these
functions, starting with the simplest one and then working
towards those that demonstrate the power of C functions.
What is a Function?
Output:
Hello World
C User defined Function Aspects
If you want to return any value from the function, you need to use any data type
such as int, long, char, etc. The return type depends on the value to be returned
from the function.
Example with return value:
int get(){
return 10;
}
Different ways of function calling
value
function without arguments and with return value
function with arguments and without return value
function with arguments and with return value
Example for Function without argument and
without return value
#include<stdio.h>
void printName(); //function declaration
int main ()
{
printf("Hello ");
printName(); //fun call
return 0;
}
void printName() //fun definition
{
printf(“World");
}
Output:
Hello World
Example for Function without arguments and with return
value
#include<stdio.h>
int sum(); //fun decl
int main()
{
int a,b;
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = sum(); //fun call
printf("%d”,result);
return 0;
}
int sum() //fun defi 11
{
int a, b;
printf("\nEnter two numbers");
scanf("%d %d”,&a,&b); 5 6
return a+b; 5+6=11
}
Output:
Going to calculate the sum of two numbers:
Enter two numbers
45
9
Example for Function with argument and without return value
#include<stdio.h>
void sum(int , int); //fun declare
int main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b); //fun call
return 0;
}
void sum(int a, int b) //fun defin
{
printf("\nThe sum is %d",a+b);
}
Output:
Sum(a,b);//actual parameters
}
Int sum(int a,int b) //formal parameters
{
}
Call by value and Call by reference in C
There are two methods to pass the data into the function in C language,
i.e., call by value and call by reference.
Call by value in C
1. In call by value method, the value of the actual parameters is copied
into the formal parameters.
2. In call by value method, we can not modify the value of the actual
parameter by the formal parameter.
3. In call by value, different memory is allocated for actual and formal
parameters since the value of the actual parameter is copied into the
formal parameter.
4. The actual parameter is the argument which is used in the function
call whereas formal parameter is the argument which is used in the
function definition.
Example of call by value in c
#include<stdio.h>
void change(int num);
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x); //passing value in function
printf("After function call x=%d \n", x);
return 0;
}
Output:
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
Call by Value Example: Swapping the values of the two variables
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); //printing value of a & b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); //Actual parameters a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\
n",a,b); // Formal parameters a = 20, b =10
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
Call by reference in C
Memory Location Actual and formal arguments will be Actual and formal arguments will be
created in different memory location created in the same memory location