0% found this document useful (0 votes)
21 views

FUNCTIONS IN C 2

Uploaded by

devaraj48212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

FUNCTIONS IN C 2

Uploaded by

devaraj48212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

FUNCTIONS IN C 2

Different types/aspects of Function calling

A function may or may not accept any argument. It may or may not return any
value. Based on these facts, There are four different aspects of function calls.

• function without arguments and without return value.


• function without arguments and with return value.
• function with arguments and without return value.
• function with arguments and with return value.
Function without return type and without arguments

• It means defined a function without return type and arguments.


• A function doesn’t return any value.

• Syntax : void fname (void); //function declaration


• void fname ()
{
// body
}
#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);
}
Function without arguments and with return type

• A function doesn’t pass a arguments but returns a value after completion of the
program.

• Syntax: return_type fname (void); //function declaration


• return_type fname() //function definition.
•{
//body

return value //return statement


•}
Ex:
#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 arguments and without return type.

• A function does pass arguments to function definition but doesn’t return the value.

• Syntax: void fname (arguments); //function declaration

• void fname(arguments) // function definition


•{
// body
•}
Ex:
#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);
}
Function with arguments and with return type

• A function does pass arguments and return value is known as function arguments
and return type.

• Syntax: return_type fname (arguments_list); //function declaration

• Return_type fname (arguments_list) //function definition


•{
// body
•}
Ex:
#include<stdio.h>
int sum(int, int); //function declaration
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); // function call
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b; // function definition
}
Passing arrays to functions.

• In C, there are various general problems which requires passing more than one
variable of the same type to a function.
• So we can declare and initialize an array and pass that into the function. This will
resolve all the complexity since the function will now work for any number of
values.
• Here, we must notice that we need to pass only the name of the array in the
function which is intended to accept an array. The array defined as the formal
parameter will automatically refer to the array specified by the array name defined
as an actual parameter.
Syntax:

• We declare a function by 3 ways to intend into arrays.


• 1.return_type fname(return_type array_name[])
• 2.return_type fname(return_type array_name[size])
• 3.return_type fname(return_type *array)
#include<stdio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
} //end of for
return min;
} //end of function
int main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9}; //declaration of array
min=minarray(numbers,6); //passing array with size
printf("minimum number is %d \n",min);
return 0;
}
Returning array from the function.

• As we know that, a function can not return more than one value. But in some
problems, we may need to return multiple values from a function.
• In such cases, an array is returned from the function.
• Returning an array is similar to passing the array into the function. The name of
the array is returned from the function.
• Syntax :
int * Function_name() {
//some statements;
return array_type;
}
Ex:
1. #include<stdio.h>
2. int* Bubble_Sort(int[]);
3. void main ()
4. {
5. int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
6. int *p = Bubble_Sort(arr), i;
7. printf("printing sorted elements ...\n");
8. for(i=0;i<10;i++)
9. {
10. printf("%d\n",*(p+i));
11. }
12.}
int* Bubble_Sort(int a[]) //array a[] points to arr.
{
int i, j,temp;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a; }
Recursion in C

• Recursion is the process which comes into existence when a function calls a copy
of itself to work on a smaller problem. Any function which calls itself is called
recursive function, and such function calls are called recursive calls. Recursion
involves several numbers of recursive calls.
Ex:

#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);
}
}

You might also like