5 Function
5 Function
1. Return Type − A function may return a value. The return_type is the data type of the value
the function returns. Some functions perform the desired operations without returning a
value. In this case, the return_type is the keyword void.
2. Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
3. Parameters − A parameter is like a placeholder. When a function is invoked, you pass a
value to the parameter. This value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.
4. terminating semicolon
Function Definition:
Syntax of a function
return_type function_name (argument list)
{
Set of statements – Block of code
}
1. Return Type − A function may return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
2. Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
3. Parameters − A parameter is like a placeholder. When a function is invoked, you pass a
value to the parameter. This value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.
4. Function Body − The function body contains a collection of statements that define what
the function does.
M.A.ANSARI Page 1
Calling a function:
When a function is called, control of the program gets transferred to the function.
functionName(argument1, argument2,...);
Parameter Passing to functions:
The parameters passed to function are called actual parameters.The parameters received by
function are called formal parameters.
There are two most popular ways to pass parameters.
1. Pass by Value: In this parameter passing method, values of actual parameters are copied to
function’s formal parameters and the two types of parameters are stored in different
memory locations. So any changes made inside functions are not reflected in actual
parameters of caller.
2. Pass by Reference: Both actual and formal parameters refer to same locations, so any
changes made inside the function are actually reflected in actual parameters of caller.
Call By Value Call By Reference
While calling a function, instead of passing the
While calling a function, we pass values of
values of variables, we pass address of
variables to it. Such functions are known as
variables(location of variables) to the function known
“Call By Values”.
as “Call By References.
In this method, the value of each variable in In this method, the address of actual variables in the
calling function is copied into corresponding calling function are copied into the dummy variables
dummy variables of the called function. of the called function.
With this method, the changes made to the
With this method, using addresses we would have an
dummy variables in the called function have no
access to the actual variables and hence we would be
effect on the values of actual variables in the
able to manipulate them.
calling function.
#include <stdio.h> #include <stdio.h>
void swapx(int x, int y); void swapx(int*, int*);
int main() int main()
{ {
int a = 10, b = 20; int a = 10, b = 20;
// Pass by Values // Pass reference
swapx(a, b); swapx(&a, &b);
printf("a=%d b=%d\n", a, b); printf("a=%d b=%d\n", a, b);
return 0; return 0;
} }
void swapx(int x, int y) void swapx(int* x, int* y)
{ {
int t; int t;
t = x; t = *x;
x = y; *x = *y;
y = t; *y = t;
printf("x=%d y=%d\n", x, y); printf("x=%d y=%d\n", *x, *y);
} }
In call by values we cannot alter the values of In call by reference we can alter the values of
actual variables through function calls. variables through function calls.
Values of variables are passes by Simple Pointer variables are necessary to define to store the
technique. address values of variables.
M.A.ANSARI Page 2
Category of Functions:
A function depending an whether the arguments are present or not and whether a value is
returned or not, may belong to one of following categories
1.).In this category, the function has no arguments. It does not receive any data from the calling
function. Similarly, it doesn’t return any value.
#include<stdio.h>
void greatNum(); // function declaration
int main()
{
greatNum(); // function call
return 0;
}
void greatNum() // function definition
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
if(i > j) {
printf("The greater number is: %d", i);
}
else {
printf("The greater number is: %d", j);
}
}
2.) In this category, function has some arguments . it receives data from the calling function, but it
doesn’t return a value to the calling function. The calling function doesn’t receive any data from the
called function. So, it is one way data communication between called and calling functions.
#include<stdio.h>
int greatNum(); // function declaration
int main()
{
int result;
result = greatNum(); // function call
printf("The greater number is: %d", result);
return 0;
}
int greatNum() // function definition
{
int i, j, greaterNum;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
M.A.ANSARI Page 3
if(i > j) {
greaterNum = i;
}
else {
greaterNum = j;
}
// returning the result
return greaterNum;
}
3.)In this category, functions has some arguments and it receives data from the calling function.
Simillarly, it returns a value to the calling function. The calling function receives data from the
called function. So, it is two-way data communication between calling and called functions.
#include<stdio.h>
void greatNum(int a, int b); // function declaration
int main()
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
greatNum(i, j); // function call
return 0;
}
void greatNum(int x, int y) // function definition
{
if(x > y) {
printf("The greater number is: %d", x);
}
else {
printf("The greater number is: %d", y);
}
}
4.)In this category, the functions has no arguments and it doesn’t receive any data from the calling
function, but it returns a value to the calling function. The calling function receives data from the
called function. So, it is one way data communication between calling and called functions.
#include<stdio.h>
int main()
{
int i, j, result;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
result = greatNum(i, j); // function call
printf("The greater number is: %d", result);
return 0;
M.A.ANSARI Page 4
}
#include<stdio.h>
long Factorial(int);
void main()
{ int num;
long fact;
printf("\n\tEnter any positive number : ");
scanf("%d",&num);
fact = Factorial(num);
printf("\n\tThe Factorial of %d is %ld",num,fact);
}
long Factorial(int num)
{ if (num == 1)
return 1;
else
return num*Factorial(num-1);
}
M.A.ANSARI Page 5