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

Functions in c

Uploaded by

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

Functions in c

Uploaded by

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

FUNCTIONS IN 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 program have at least one function called Main method.
Advantages of functions

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

• Functions

1.Library Functions 2.User defined Function


1.Library Functions :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: 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.
Structure of Function

• The structure of function contains three layers/ aspects.


• Function Declaration
• Function Definition
• Function Call

• 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.
• Syntax:
• Return_type function_name(argument_list);
Example: void sum(int a, int b);

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.
Syntax: return_type function_name(argument_list)
{
program lines to excuted
}
• Example:
void sum(){
int a,b;
int sum=0;
printf(“enter the values to add”);
scanf(“%d%d”,a,b);
sum = a+b;
printf(“total is “,sum);
}
• 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.

• Syntax
• function_name(arguements);
• Example
• sum();
• There are two methods to pass the data into the function in C language, i.e., call by
value and call by reference/address.

• 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:

1. #include<stdio.h>
2. void change(int num) {
3. printf("Before adding value inside function num=%d \n",num);
4. num=num+100;
5. printf("After adding value inside function num=%d \n", num);
6. }
7. int main() {
8. int x=100;
9. printf("Before function call x=%d \n", x);
10. change(x);//passing value in function
11. printf("After function call x=%d \n", x);
12. return 0;
13. }
• Output
• Before function call x=100
• Before adding the value in the function num=100
• After adding value in function num=200
• After function call x=200
• 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.
Example

1.#include<stdio.h>
2.void change(int *num) {
3. printf("Before adding value inside function num=%d \n",*num);
4. (*num) += 100;
5. printf("After adding value inside function num=%d \n", *num);
6.}
7.int main() {
8. int x=100;
9. printf("Before function call x=%d \n", x);
10. change(&x);//passing reference in function
11. printf("After function call x=%d \n", x);
12.return 0;
13. }
• Output

• Before function call x=100


• Before adding the value in the function num=100
• After adding value in function num=200
• After function call x=200

You might also like