unit4_cprogramming_function
unit4_cprogramming_function
unit4_cprogramming_function
Usually, In C programs flows from top left to right bottom of main() functions.
We can create any number of functions below that main() functions. These
function are called from main() function.
Advantage:
1. Library Function
C programming language provides some library functions to perform some predefined tasks.
These functions are also called the built-in or predefined function in the C header files whose
meaning cannot change. When we use these functions on any program, we call the function
name with appropriate header files because these functions are defined inside the header
files. In other words, we did not require to write the complete code to perform a specific task;
instead, we can directly call the function in our program whenever it is required. For example:
printf(), scanf(), getch(), etc., are the predefined library functions.
As the name suggests, a user-defined function is a function written by the user to write any
program code and execute specific actions. These user-defined functions can be modified and
execute according to the requirement of the programmer. A programmer can change the user-
defined function, but these functions are not defined in the C header files. A user-defined
function is made up using the function declaration, function definition, and the function call.
Syntax:
return_type function_name ( data_type arg1, data_type arg2, ..) ;
Function Calling
Here return type represents the function that can return any defined data type values. The return
type value can be int, float, char, double, etc., where the function_name represents the name of a
function that contains more than one argument in it.
After defining the function definition, we need to call the defined function in a program to execute
its tasks. However, a function can be called multiple times by writing the function name followed by
arguments lists.
Syntax:
Here, arg1, arg2 are the actual arguments passed to the function_name.
Syntax:
statement to be executed;
return (expr);
#include<stdio.h>
void sum();
void main()
{
printf("\nGoing to calculate the sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
#include<stdio.h>
int sum();
void main()
{
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Function with argument and without return value
#include<stdio.h>
void sum(int, int);
void 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);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}
Function Methods:-
There are two methods to pass the data into the function in C language, i.e., call by value and call by
reference.
a. Call by Value
In call by value method, the value of the actual parameters is copied into the formal
parameters. In other words, we can say that the value of the variable is used in the function
call in the call by value method.
In call by value method, we can not modify the value of the actual parameter by the formal
parameter.
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.
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:- WAP to Swap two numbers using call by value method.
#include <stdio.h>
int main()
int a = 10;
int b = 20;
swap(a,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
}
b. Call by reference:-
In call by reference, the address of the variable is passed into the function call as the actual
parameter.
The value of the actual parameters can be modified by changing the formal parameters since
the address of the actual parameters is passed.
In call by reference, the memory allocation is similar for both formal parameters and actual
parameters. All the operations in the function are performed on the value stored at the address
of the actual parameters, and the modified value gets stored at the same address.
#include <stdio.h>
int main()
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
int temp;
temp = *a;
*a=*b;
*b=temp;
Recursive Function:-
A function which calls itself directly or indirectly again and again until some specified condition is
satisfied is known as Recursive Function.
A recursive function is a function defined in terms of itself via self-calling expressions. This means
that the function will continue to call itself and repeat its behavior until some condition is satisfied
to return a value.
Recursion is the process of defining something in terms of itself. Recursion has many negatives. It
repeatedly invokes the mechanism, and consequently increases the overhead of function calls. This
repetition can be expensive in terms of both processor time and memory space.
E.g WAP to find out the factorial of given number using recursive function.
include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
); } }