0% found this document useful (0 votes)
98 views14 pages

1.explain About Functios: Advantage of Functions in C o

Functions allow programmers to organize programs into reusable blocks of code. There are two types of functions in C: library functions defined in header files, and user-defined functions created by the programmer. User-defined functions make programs more modular and reusable by encapsulating common logic. Functions can accept arguments as input and return values. Well-defined functions improve program structure and maintainability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views14 pages

1.explain About Functios: Advantage of Functions in C o

Functions allow programmers to organize programs into reusable blocks of code. There are two types of functions in C: library functions defined in header files, and user-defined functions created by the programmer. User-defined functions make programs more modular and reusable by encapsulating common logic. Functions can accept arguments as input and return values. Well-defined functions improve program structure and maintainability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Explain about functios

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 o

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.

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.

1
2.explain about Mathematical functions

tion Description

This function returns the nearest integer which is less than or equal to
floor ( ) the argument passed to this function.

This function returns the nearest integer value of the


float/double/long double argument passed to this function. If decimal
value is from “.1 to .5”, it returns integer value less than the
argument. If decimal value is from “.6 to .9”, it returns the integer
round ( ) value greater than the argument.

This function returns nearest integer value which is greater than or


ceil ( ) equal to the argument passed to this function.

sin ( ) This function is used to calculate sine value.

cos ( ) This function is used to calculate cosine.

cosh ( ) This function is used to calculate hyperbolic cosine.

exp ( ) This function is used to calculate the exponential “e” to the xth power.

tan ( ) This function is used to calculate tangent.

tanh ( ) This function is used to calculate hyperbolic tangent.

sinh ( ) This function is used to calculate hyperbolic sine.

log ( ) This function is used to calculates natural logarithm.

log10 ( ) This function is used to calculates base 10 logarithm.

This function is used to find square root of the argument passed to


sqrt ( ) this function.

pow ( ) This is used to find the power of the given number.

This function truncates the decimal value from floating point value


trunc.(.) and returns integer value.

2
3.Explain about Character functions
 ctype.h” header file support all the below functions in C language. Click on each
function name below for detail description and example programs.
Functions Description

isalpha() checks whether character is alphabetic

isdigit() checks whether character is digit

isalnum() Checks whether character is alphanumeric

isspace() Checks whether character is space

islower() Checks whether character is lower case

isupper() Checks whether character is upper case

isxdigit() Checks whether character is hexadecimal

iscntrl() Checks whether character is a control character

isprint() Checks whether character is a printable character

ispunct() Checks whether character is a punctuation

isgraph() Checks whether character is a graphical character

tolower() Checks whether character is alphabetic & converts to lower case

toupper() Checks whether character is alphabetic & converts to upper case

3
3.Explan about Date and time functions
Time functions
Time functions in C are used to interact with system time routine and formatted time
outputs are displayed. Example programs for the time functions are given below.

Function Description

setdate() This function used to modify the system date

getdate() This function is used to get the CPU time

clock() This function is used to get current system time

time() This function is used to get current system time as structure

This function is used to get the difference between two given


difftime() times

strftime() This function is used to modify the actual time format

mktime() This function interprets tm structure as calendar time

localtime( This function shares the tm structure that contains date and
) time informations

This function shares the tm structure that contains date and


gmtime() time informations

This function is used to return string that contains date and


ctime() time informations

Tm structure contents are interpreted by this function as


asctime() calendar time. This time is converted into string.

 4.Explain aboutUser-defined functions ( 0r )elements of functions


A function is a block of code that performs a specific task.
C allows you to define functions according to your need. These functions are known as user-
defined functions. For example:
Example: User-defined function
Here is an example to add two integers. To perform this task, we have created an user-
defined addNumbers().
#include <stdio.h>
int addNumbers(int a, int b); // function prototype

4
int main()
{
int n1,n2,sum;

printf("Enters two numbers: ");


scanf("%d %d",&n1,&n2);

sum = addNumbers(n1, n2); // function call


printf("sum = %d",sum);

return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}

Function prototype or function declaration


A function prototype is simply the declaration of a function that specifies function's name,
parameters and return type. It doesn't contain function body.
A function prototype gives information to the compiler that the function may later be used in
the program.
Syntax of function prototype
returnType functionName(type1 argument1, type2 argument2, ...);
In the above example, int addNumbers(int a, int b); is the function prototype which provides
the following information to the compiler:
1. name of the function is addNumbers()
2. return type of the function is int
3. two arguments of type int are passed to the function
The function prototype is not needed if the user-defined function is defined before
the main() function.

5
Calling a function
Control of the program is transferred to the user-defined function by calling it.
Syntax of function call
functionName(argument1, argument2, ...);
In the above example, the function call is made using addNumbers(n1, n2); statement inside
the main() function.

Function definition
Function definition contains the block of code to perform a specific task. In our example,
adding two numbers and returning it.
Syntax of function definition
returnType functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
}
When a function is called, the control of the program is transferred to the function definition.
And, the compiler starts executing the codes inside the body of a function.

Passing arguments to a function


In programming, argument refers to the variable passed to the function. In the above
example, two variables n1 and n2 are passed during the function call.
The parameters a and b accepts the passed arguments in the function definition. These
arguments are called formal parameters of the function.

6
The type of arguments passed to a function and the formal parameters must match,
otherwise, the compiler will throw an error.
If n1 is of char type, a also should be of char type. If n2 is of float type, variable b also
should be of float type.
A function can also be called without passing an argument.

Return Statement
The return statement terminates the execution of a function and returns a value to the calling
function. The program control is transferred to the calling function after the return statement.
In the above example, the value of the result variable is returned to the main function.
The sum variable in the main() function is assigned this value.

Syntax of return statement


return (expression);
For example,
return a;
return (a+b);
The type of value returned from the function and the return type specified in the function
prototype and function definition must match.

7
5.Different function arguments and return types
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
Example for Function without argument and return value
Example
#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);  
}  
Output
Going to calculate the sum of two numbers:
Enter two numbers 10
24
The sum is 34
Example for Function without argument and with return value
Example 1
#include<stdio.h>  
int sum();  
void main()  

8
{  
    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;   
}  
Output
Going to calculate the sum of two numbers:
Enter two numbers 10
24
The sum is 34
Example for Function with argument and without return value
Example 1
#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);      

9
}  
Output
Going to calculate the sum of two numbers:
Enter two numbers 10
24
The sum is 34
Example for Function with argument and with return value
Example 1
#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;  }

6. explain about 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.
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.
However, some problems are best suited to be solved by the recursion, for example, tower of
Hanoi, Fibonacci series, factorial finding, etc.

In the following example, recursion is used to calculate the factorial of a number.

#include <stdio.h>
int fact (int);

10
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);
}
}
Output
Enter the number whose factorial you want to calculate?5
factorial = 120

7. Explain about 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.

Call by value in C
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.

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

Call by Value Example: Swapping the values of the two variables

#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 the value
of a and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual
parameters do not change by changing the formal parameters in call by value, 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 parameters, 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 = 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. 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.

Call by reference Example: Swapping the values of the two variables

#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;

12
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the 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 actual
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 parameters,
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

8. Explain about Return Statement


The return statement terminates the execution of a function and returns a value to the calling
function. The program control is transferred to the calling function after the return statement.
In the above example, the value of the result variable is returned to the main function.
The sum variable in the main() function is assigned this value.

Syntax of return statement


return (expression);
For example,
return a;
return (a+b);
The type of value returned from the function and the return type specified in the function
prototype and function definition must match.

13
14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy