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

Emailing C Functions-converted_converted_by_abcdpdf-converted

The document provides an overview of functions in C programming, detailing their advantages, aspects, types, and examples of user-defined and library functions. It explains the concepts of call by value and call by reference, along with their differences, and introduces recursion with examples. Additionally, it includes code snippets demonstrating various functionalities such as calculating the area of a square, average of numbers, and generating a Fibonacci series.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Emailing C Functions-converted_converted_by_abcdpdf-converted

The document provides an overview of functions in C programming, detailing their advantages, aspects, types, and examples of user-defined and library functions. It explains the concepts of call by value and call by reference, along with their differences, and introduces recursion with examples. Additionally, it includes code snippets demonstrating various functionalities such as calculating the area of a square, average of numbers, and generating a Fibonacci series.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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 procedureor subroutinein 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.
o However, Function calling is always a overhead in a C program.

Function Aspects

There are three aspects of a C function.

o 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.

o 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.

o 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.

SN C function Syntax
aspects

1 Function return_type function_name (argument list);


declaration

2 Function call function_name (argument_list);

3 Function return_type function_name (argument list)


definition {function body;}

The syntax of creating function in c language is given below:

return_type function_name(data_type parameter...)


{
//code to be executed
}

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. It reduces the complexity
of a big program and optimizes the code.
Return Value

A C function may or may not return a value from the function. If you don't have to
return any value from the function, use void for the return type.

Let's see a simple example of C function that doesn't return any value from the
function.

Example without return value:

void hello(){
printf("hello c");
}

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.

Let's see a simple example of C function that returns int value from the function.

Example with return value:


int get(){
return 10;
}

In the above example, we have to return 10 as a value, so the return type is int. If
you want to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use
float as the return type of the method.

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

40.Implement User-Defined Functions


#include<stdio.h>
void printName();
int square() ;
void average(int, int, int, int, int);
int even_odd(int);
void main ()
{
printf("Hello ");
printName();
printf("Going to calculate the area of the square\n");
float area = square();
printf("The area of the square: %f\n",area);
int a,b,c,d,e;
printf("\nGoing to calculate the average of five numbers:");
printf("\nEnter five numbers:");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
average(a,b,c,d,e);
int n,flag=0;
printf("\nGoing to check whether a number is even or odd");
printf("\nEnter the number: ");
scanf("%d",&n);
flag = even_odd(n);
if(flag == 0)
{
printf("\nThe number is odd");
}
else
{
printf("\nThe number is even");
}
}
void printName()
{
printf("Javatpoint");
}
int square()
{
float side;
printf("Enter the length of the side in meters: ");
scanf("%f",&side);
return side * side;
}
void average(int a, int b, int c, int d, int e)
{
float avg;
avg = (a+b+c+d+e)/5;
printf("The average of given five numbers : %f",avg);
}
int even_odd(int n)
{
if(n%2 == 0)
{
return 1;
}
else
{
return 0;
}
}

Output

Hello Javatpoint
Going to calculate the area of the square
Enter the length of the side in meters: 10
The area of the square: 100.000000
Going to calculate the average of five numbers:
Enter five numbers:10
20
30
40
50
The average of given five numbers : 30.000000
Going to check whether a number is even or odd
Enter the number: 100
The number is even

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. For example, printf is a library function used to print on the console.
The library functions are created by the designers of compilers. All C standard
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. For example, To use the library
functions such as printf/scanf we need to include stdio.h in our program which is a
header file that contains all the library functions regarding standard input/output.

The list of mostly used header files is given in the following table.
SN Header Description
file

1 stdio.h This is a standard input/output header file. It contains


all the library functions regarding standard input/output.

2 conio.h This is a console input/output header file.

3 string.h It contains all string related library functions like gets(),


puts(),etc.

4 stdlib.h This header file contains all the general library


functions like malloc(), calloc(), exit(), etc.

5 math.h This header file contains all the math operations related
functions like sqrt(), pow(), etc.

6 time.h This header file contains all the time-related functions.

7 ctype.h This header file contains all character handling


functions.

8 stdarg.h Variable argument functions are defined in this header


file.

9 signal.h All the signal handling functions are defined in this


header file.

10 setjmp.h This file contains all the jump functions.

11 locale.h This file contains locale functions.

12 errno.h This file contains error handling functions.

13 assert.h This file contains diagnostics functions.


41.Implement Library Functions
#include <math.h>

#include <stdio.h>

int main() {

double number, squareRoot;

printf("Enter a number: ");


scanf("%lf", &number);

// computing the square root


squareRoot = sqrt(number);

printf("Square root of %.2lf = %.2lf", number, squareRoot);

return 0;
}

Output

Enter a number: 23.4


Square root of 23.40 = 4.84

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.
Let's understand call by value and call by reference in c language one by one.

Call by value in C

o 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.
o In call by value method, we cannot modify the value of the actual
parameter by the formal parameter.
o 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.
o 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.

42.Implement call by value


#include<stdio.h>
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
After function call x=100

Call by reference in C

o In call by reference, the address of the variable is passed into the function
call as the actual parameter.
o The value of the actual parameters can be modified by changing the
formal parameters since the address of the actual parameters is passed.
o 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.
43.Implement call by reference
#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 th
e value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of a
ctual parameters do change in call by reference, 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 p
arameters, 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 = 20, b = 10

Difference between call by value and call by reference in c

No. Call by value Call by reference

1 A copy of the value is passed into the An address of value is passed into the
function function

2 Changes made inside the function is Changes made inside the function validate
limited to the function only. The values of outside of the function also. The values of
the actual parameters do not change by the actual parameters do change by
changing the formal parameters. changing the formal parameters.

3 Actual and formal arguments are created at Actual and formal arguments are created at
the different memory location the same memory location
Recursion in C

Any function which calls itself is called recursive function, and such function
calls are called recursive calls. Recursion involves several numbers of recursive
calls.

Recursion cannot be applied to all the problem, but it is more useful for the tasks
that can be defined in terms of similar subtasks. For Example, recursion may be
applied to sorting, searching, and traversal problems.

Generally, iterative solutions are more efficient than recursion since function call is
always overhead. Any problem that can be solved recursively, can also be solved
iteratively. However, some problems are best suited to be solved by the recursion,
for example, tower of Hanoi, Fibonacci series, factorial finding, etc.

44.Generate Fibonacci series using Recursive Functions

#include<stdio.h>
int fibonacci(int);
void main ()
{
int n,f;
printf("Enter the value of n?");
scanf("%d",&n);
f = fibonacci(n);
printf("%d",f);
}
int fibonacci (int n)
{
if (n==0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return fibonacci(n-1)+fibonacci(n-2);
}
}
Output
Enter the value of n?12
144

You might also like