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

Unit 7 Function

The document discusses different aspects of functions in C programming including function declaration, definition, call, types of functions, passing arguments, recursion, and passing arrays to functions.

Uploaded by

Sakar Sapkota
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Unit 7 Function

The document discusses different aspects of functions in C programming including function declaration, definition, call, types of functions, passing arguments, recursion, and passing arrays to functions.

Uploaded by

Sakar Sapkota
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

C Functions

In c, we can divide a large program into the basic building blocks known as function. The
function contains the set of programming statements enclosed by {}.

A function can be called multiple times to provide reusability and modularity to the C
program. In other words,

We can say that the collection of functions creates a program. The function is also known
as procedure or subroutine in other programming languages.

Advantage of functions in C
There are the following advantages of C functions.

o By using functions, we can avoid rewriting same logic/code again and again in a
program.
o We can call C functions any number of times in a program and from any place in a
program.
o We can track a large C program easily when it is divided into multiple functions.
o Reusability is the main achievement of C functions.

Function Aspects
There are three aspects of a C function.

 Function Declaration
 Function Call
 Function Definition

Function declaration: A function must be declared globally in a c program to tell the


compiler about the function name, function parameters, and return type.

Function call: Function can be called from anywhere in the program. The parameter list
must not differ in function calling and function declaration. We must pass the same
number of functions as it is declared in the function declaration.
Function definition: It contains the actual statements which are to be executed. It is the
most important aspect to which the control comes when the function is called. Here, we
must notice that only one value can be returned from the function.

Types of Functions
There are two types of functions in C programming:

1. Library Functions: are the functions which are declared in the C header files such
as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
2. User-defined functions: are the functions which are created by the C programmer,
so that he/she can use it many times.

Different 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.
o function without arguments and without return value
o function without arguments and with return value
o function with arguments and without return value
o function with arguments and with return value

1.Function without argument and return value

#include<stdio.h>
void printName(); // function declaration
void main ()
{
printf("Hello ");
printName(); // function call
}
void printName() //function definition
{
printf("Birgunj");
}
Output
Hello Birgunj

Function without arguments and with return value


#include<stdio.h>
void sum();
void main()
{
sum();
}
void sum()
{
int a,b;
printf("\n Enter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d", a+b);
}
Output
Enter two numbers
10 20
The sum is 30

Function without argument and with return value


#include<stdio.h>
int sum();
void main()
{
int result;
result = sum();
printf("%d", result);
}
int sum()
{
int a,b;
printf("\n Enter two numbers");
scanf("%d %d",&a,&b);
return a+b; // program of all mathematical formula try
} // Average of five number
// Even Odd program
Output
Enter two numbers
10 20
30
Function with argument and without return value
#include<stdio.h>
void sum(int, int);
void main()
{
int a, b, result;
printf("\n Enter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b); // a,b are the actual parameters/arguments
}
void sum(int a, int b) // a,b are the formal parameters/arguments
{
printf("\n The sum is %d", a+b);
}

Output
Enter two numbers
10 20
The sum is 30

Function with argument and with return value

#include<stdio.h>
int sum(int, int);
void main()
{
int a, b, result;
printf("\n Enter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\n The sum is : %d", result);
}

int sum(int a, int b)


{
return a+b;
}
Output
Enter two numbers
10 20
The sum is 30

C Library Functions
Library functions are the inbuilt function in C that are grouped and placed at a common
place called the library. Such functions are used to perform some specific operations.

Eg:- printf() is a library function used to print on the console. The library functions are
created by the designers of compilers.

All C library functions are defined inside the different header files saved with the
extension .h. We need to include these header files in our program to make use of the
library functions defined in such header files.

The list of mostly used header files is given in the following table.
Call by value and Call by reference in C
There are two methods to pass the data into the function in C language.

 call by value
 call by reference.

Call by value in C
 In call by value method, the value of the actual parameters is copied into the
formal parameters.
 In call by value method, we cannot 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.
#include<stdio.h>
void swap(int x, int y);
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);
printf("After swapping values in main a = %d, b = %d\n", a,b);
}
void swap (int x, int y)
{
int temp;
temp = x;
x=y;
y=temp;
printf("After swapping values in function x = %d, y = %d\n", x, y);
}

Output

Before swapping the values in main a=10, b=20

After swapping values in function x=20, y=10

After swapping the values in main a=10, b=20

Call by reference in C
 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.
#include <stdio.h>
void swap(int *, int *);
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);
printf("After swapping values in main a = %d, b = %d\n", a,b);
}
void swap (int *x, int *y)
{
int temp;
temp = *x;
*x=*y;
*y=temp;
printf("After swapping values in function x = %d, y = %d\n",*x,*y);
}
Output

Before swapping the values in main a=10, b=20

After swapping values in function x=20, y=10

After swapping the values in main a=20, b=10

Recursion in C
Recursion is the process of calling itself. Any function which calls itself is called recursive
function, and such function calls are called recursive calls.

Recursion involves several numbers of recursive calls. However, it is important to impose


a termination condition of recursion. Recursion code is shorter than iterative code
however it is difficult to understand.
#include<stdio.h>
int factorial(int);
int main()
{
int n, f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d", &n);
f = factorial(n);
printf("factorial = %d", f);
}
int factorial(int n)
{
if (n==0)
return 0;
else if ( n == 1)
return 1;
else
return n*factorial(n-1)
}
Output

Enter the number whose factorial you want to calculate? 5

Factorial = 120

Fibonacci Series Using recursion


#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;

scanf("%d", &n);

printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}

return 0;
}

int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}

Passing Array to Function in C


In C, there are various general problems which requires passing more than one variable
of the same type to a function. For example, consider a function which sorts the 10
elements in ascending order. Such a function requires 10 numbers to be passed as the
actual parameters from the main function. Here, instead of declaring 10 different numbers
and then passing into the function, 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.

Declaration of passing array to the function

return_type function_name(array_variable); //passing array

#include<stdio.h>
void Bubble_Sort(int[]);
void main ()
{
int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
Bubble_Sort(arr);
}
void 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;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\t",a[i]);
}
}

Output

Printing Sorted Element List

7 9 10 12 23 23 34 44 78 144

You might also like