Functions in C
Compiled By:
Dr. Narendra Kumar Sharma
Asst. Professor, Dept. of MCA
Pranveer Singh Institute of Technology, Kanpur
Contents
• C – Functions
• Advantage of functions in C
• Function Aspects
– Function declaration
– Function call
– Function definition
• Types of Functions
– Library Functions
– User defined Functions
• Different aspects of function calling
• Function Prototype
• Call by Value and Call by Reference
• Recursion
• Examples
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.
1. By using functions, we can avoid rewriting same logic/code again and
again in a program.
2. We can call C functions any number of times in a program and from
any place in a program.
3. We can track a large C program easily when it is divided into multiple
functions.
4. Reusability is the main achievement of C functions.
5. However, Function calling is always a overhead in a C program.
Function Aspects
• There are three aspects of a C function.
• 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.
SN C function aspects Syntax
1 Function declaration return_type function_name (argument
list);
2 Function call function_name (argument_list)
3 Function definition return_type function_name (argument
list) {function body;}
The syntax of creating function in C language is given below:
return_type function_name(data_type parameter...)
{
//code to be executed
}
• Function Declarations
Syntax:
return_type name_of_the_function (parameter_1, parameter_2…..);
• Example
int sum(a, b);
int sum(int a, int b);
• Function Definition
Syntax:
return_type function_name (para1_type para1_name, para2_type para2_name)
{
// body of the function
}
• Function Call
Working of Function Call
Example
// C program to show function call and definition
#include <stdio.h>
// Function that takes two parameters a and b as inputs and returns their sum
int sum(int a, int b)
{
return a + b;
}
int main() //main method
{
// Calling sum function and storing its value in add variable
int add = sum(10, 30);
printf("Sum is: %d", add);
return 0;
}
Types of Functions
• There are two types of functions in C programming:
• Library Functions: are the functions which are declared in the C header
files such as scanf(), printf(), gets(), puts() etc.
• 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. Example
// C program to implement the pre defined library function
#include <math.h>
#include <stdio.h>
// Driver code
int main()
{
double Number;
Number = 49;
// Computing the square root with the help of predefined C library function
double squareRoot = sqrt(Number);
printf("The Square root of %.2lf = %.2lf", Number, squareRoot);
return 0;
}
2. Example
// C program to show user-defined functions
#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
// Driver code
int main()
{
int a = 30, b = 40;
// function call
int res = sum(a, b);
printf("Sum is: %d", res);
return 0;
}
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.
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.
1. Example with return value:
int get()
{
return 10;
}
2. Example with return value:
float get()
{
return 10.2;
}
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.
1. function without arguments and without return value
2. function without arguments and with return value
3. function with arguments and without return value
4. function with arguments and with return value
1. Example for Function without argument and without return value
1.1 Example
#include<stdio.h>
void printName();
void main ()
{
printf("Hello ");
printName();
}
void printName()
{
printf("MCA");
}
1.2 Example
#include<stdio.h>
void sum();
void main()
{
printf("n 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);
}
2. Example for Function without argument and with return value
2.1 Example
#include<stdio.h>
int sum();
void main()
{
int result;
printf("n 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;
}
Example 2.2: program to calculate the area of the square
#include<stdio.h>
int square();
void main()
{
printf(“calculate the area of the squaren");
float area = square();
printf("The area of the square: %fn", area);
}
int square()
{
float side;
printf("Enter the length of the side in meters: ");
scanf("%f", &side);
return side * side;
}
3. Example for Function with argument and without return value
3.1 Example
#include<stdio.h>
void sum(int, int);
void main()
{
int a, b;
printf("ncalculate 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);
}
Example 3.2: program to calculate the average of five numbers.
#include<stdio.h>
void average(int, int, int, int, int);
void main()
{
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);
}
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);
}
4. Example for Function with argument and with return value
4.1 Example
#include<stdio.h>
int sum(int, int);
void main()
{
int a, b, result;
printf("ncalculate 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;
}
Example 4. 2: Program to check whether a number is even or odd
#include<stdio.h>
int even_odd(int);
void main()
{
int n,flag=0;
printf("nTo 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");
} }
int even_odd(int n)
{
if(n%2 == 0)
{
return 0;
}
else
{
return 0;
} }
Define multiple functions in C
#include <stdio.h>
int sum(int a, int b) // Function to calculate the sum of two numbers
{
return a + b;
}
int product(int a, int b) // Function to calculate the product of two numbers
{
return a * b;
}
double area_of_circle(double radius) // Function to calculate the area of a circle
{
return radius * radius * 3.14;
}
int main()
{
int result = sum(10, 20); // Call the sum function
printf("The sum is %dn", result);
result = product(10, 20); // Call the product function
printf("The product is %dn", result);
double area = area_of_circle(5); // Call the area of a circle function
printf("The area of the circle is %fn", area);
return 0;
}
Example
#include<stdio.h>
int sum(int a, int b);
int substact(int a, int b);
int multiplay(int a, int b);
double devision(int a, int b);
int main()
{
int operation, fc, v1, v2;
double fcd;
printf("input 2 valuesn");
scanf("%d%d",&v1, &v2);
/* telling the user to choose any type of calculator */
printf("please choose what you want to do with your valuesn");
printf("1- Sumn");
printf("2- substracttionn");
printf("3- multiplayn");
printf("4- devisionn");
scanf("%d", &operation); //input a
switch(operation)
{
case 1:
fc = sum(v1, v2);
printf("sum of two values = %dn", fc);
break;
case 2:
fc = substact(v1, v2);
printf("substract of two values = %d", fc);
break;
case 3:
fc = multiplay(v1, v2);
printf("multiply of two values = %dn", fc);
break;
case 4:
fcd = devision(v1, v2);
printf("division of two values = %dn", fcd);
break;
default:
printf("wrong choicen");
}
return 0;
}
int sum(int a, int b)
{
int sum=0;
sum=a+b;
return sum;
}
int substact(int a, int b)
{
int sub=0;
sub=a-b;
return sub;
}
int multiplay(int a, int b)
{
int mult=1;
mult=a*b;
return mult;
}
double devision(int a, int b)
{
double devi=1;
devi=a/b;
return devi; }
Function Prototype
• 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:
returntype function_name (datatype of parameter1 parameter1 , datatype of
parameter2 parameter2, datatype of parameter3 parameter 3, . . . . . . . . . . .);
• For Example :
int add(int number1, int number2, int number3);
• Here in this function prototype, we can easily understand that :
Return type of the function = integer
Number of parameters = 3
DataType of Parameters = integer
Importance of Function Prototype in C
• In C language, programs are compiled line by line and if we call a
function before we have defined it in the program , we will get some
warning like
[Warning] conflicting types for 'function' previous implicit declaration
of 'function' was here.
• So to avoid this we declare function prototypes at the start of our
programs and may define the functions later on. So that the compiler
gets to know that there exists a function with this method signature
and hence doesn't give an error.
Example: Program Without Function Prototype
#include <stdio.h>
int main()
{
int a=10; //first number
int b=4; // second number
int c=5; // third number
int res=add(a, b, c); // calling add function
printf("Sum of all the above 3 numbers is %d ",res); // printing result
}
// here function definition is after the main program
int add(int a, int b, int c)
{
int result=a+b+c;
return result;
}
Examples of Function Prototype in C
#include <stdio.h>
#include<math.h>
#include<string.h>
void odd_even(int number); // function prototype for odd_even function
int main()
{
int number1;
printf(" Please enter the number you want to know " );
scanf( "%d",&number1) ; // taking number as input from user
odd_even(number1); // calling odd even function
return 0 ;
}
// here function definition is after the main program
void odd_even(int number) // function definition
{
if(number%2==0)
printf("%d is an even number",number);
else
printf("%d is an odd number ",number);
}
Difference between Function Prototype and Function Definition
Function Prototype Function Definition
1. The function prototype includes the
function name, return type, and
parameters but does not include the
function body.
1. The function definition includes the
function name, return type, and
parameters, as well as the function
body.
2. The function implementation is not
present in the function prototype.
2. The function implementation is
included in the function definition.
3. Typically appears at the top of a
source file or in a header file.
3. Typically appears below the
prototype in a source file.
4. Used to enable the compiler to
verify the function’s usage and type-
checking during compilation.
4. Used to enable the linker to create
the executable code that can be
executed by the computer.
Syntax:
return_type
function_name(parameter_list);
Syntax:
return_type function_name();
Call by Value and Call by Reference
• 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: A copy of the variable is passed to the function.
• Call by reference: An address of the variable is passed to the function.
• Functions can be invoked in two ways: Call by Value or Call by
Reference. These two ways are generally differentiated by the type of
values passed to them as parameters.
• The parameters passed to the function are called actual
parameters whereas the parameters received by the function are
called formal parameters.
1. Call By Value
• In call by value method of parameter passing, the values of actual
parameters are copied to the function’s formal parameters.
• There are two copies of parameters stored in different memory
locations.
• One is the original copy and the other is the function copy.
• Any changes made inside functions are not reflected in the actual
parameters of the caller.
1. Example of Call by Value
#include <stdio.h>
void swapx(int x, int y); // Function Prototype
int main() // Main function
{
int a = 10, b = 20;
// Pass by Values
swapx(a, b); // Actual Parameters
printf("In the Caller:na = %d b = %dn", a, b);
return 0;
}
// Swap functions that swaps two values
void swapx(int x, int y) // Formal Parameters
{
int t;
t = x;
x = y;
y = t;
printf("Inside Function:nx = %d y = %dn", x, y);
}
2. Example
#include<stdio.h>
void swap(int n1, int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
int main()
{
int x = 20;
int y = 68;
printf("The numbers before swapping n1 and n2 %d %d n",x, y);
swap(x, y);
printf("The numbers after swapping n1 and n2 %d %d n",x, y);
return 0;
}
2. Call by Reference
• In call by reference method of parameter passing, the address of the
actual parameters is passed to the function as the formal parameters.
• Both the actual and formal parameters refer to the same locations.
• Any changes made inside the function are actually reflected in the
actual parameters of the caller.
1. Example of Call by Reference
#include <stdio.h>
void swapx(int*, int*); // Function Prototype
int main() // Main function
{
int a = 10, b = 20;
// Pass reference
swapx(&a, &b); // Actual Parameters
printf("Inside the Caller:na = %d b = %dn", a, b);
return 0;
}
// Function to swap two variables by references
void swapx(int* x, int* y) // Formal Parameters
{
int t;
t = *x;
*x = *y;
*y = t;
printf("Inside the Function:nx = %d y = %dn", *x, *y);
}
2. Example
#include<stdio.h>
void swap(int *n1 ,int *n2)
{
int temp = *n1;
*n1 = *n2;
*n2 = temp;
}
int main()
{
int n1 = 20;
int n2 = 68;
printf("The numbers before swapping n1 and n2 %d %d n",n1, n2);
swap(&n1, &n2);
printf("The numbers after swapping n1 and n2 %d %d",n1, n2);
return 0;
}
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 function
An address of value is passed into the
function
2 Changes made inside the
function is limited to the function
only. The values of the actual
parameters do not change by
changing the formal parameters.
Changes made inside the function
validate outside of the function also.
The values of the actual parameters
do change by changing the formal
parameters.
3 Actual and formal arguments are
created at the different memory
location
Actual and formal arguments are
created at the same memory location
Recursion in C
• Recursion is the process of calling a function itself repeatedly until a
particular condition is met. A function that calls itself directly or
indirectly is called a recursive function and such kind of function calls
are called recursive calls.
How recursion works?
void recurse()
{ ... .. ...
recurse();
... .. ...
}
int main()
{ ... .. ...
recurse();
... .. ...
}
Working of Recursion
Example: Sum of Natural Numbers Using Recursion
#include <stdio.h>
int sum(int n);
int main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
return 0;
}
int sum(int n)
{
if (n != 0)
return n + sum(n-1); // sum() function calls itself
else
return n;
}
THANKS

function in in thi pdf you will learn what is function

  • 1.
    Functions in C CompiledBy: Dr. Narendra Kumar Sharma Asst. Professor, Dept. of MCA Pranveer Singh Institute of Technology, Kanpur
  • 2.
    Contents • C –Functions • Advantage of functions in C • Function Aspects – Function declaration – Function call – Function definition • Types of Functions – Library Functions – User defined Functions • Different aspects of function calling • Function Prototype • Call by Value and Call by Reference • Recursion • Examples
  • 3.
    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.
  • 4.
    Advantage of functionsin C • There are the following advantages of C functions. 1. By using functions, we can avoid rewriting same logic/code again and again in a program. 2. We can call C functions any number of times in a program and from any place in a program. 3. We can track a large C program easily when it is divided into multiple functions. 4. Reusability is the main achievement of C functions. 5. However, Function calling is always a overhead in a C program.
  • 5.
    Function Aspects • Thereare three aspects of a C function. • 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.
  • 6.
    SN C functionaspects Syntax 1 Function declaration return_type function_name (argument list); 2 Function call function_name (argument_list) 3 Function definition return_type function_name (argument list) {function body;} The syntax of creating function in C language is given below: return_type function_name(data_type parameter...) { //code to be executed }
  • 7.
    • Function Declarations Syntax: return_typename_of_the_function (parameter_1, parameter_2…..); • Example int sum(a, b); int sum(int a, int b);
  • 8.
    • Function Definition Syntax: return_typefunction_name (para1_type para1_name, para2_type para2_name) { // body of the function }
  • 9.
    • Function Call Workingof Function Call
  • 10.
    Example // C programto show function call and definition #include <stdio.h> // Function that takes two parameters a and b as inputs and returns their sum int sum(int a, int b) { return a + b; } int main() //main method { // Calling sum function and storing its value in add variable int add = sum(10, 30); printf("Sum is: %d", add); return 0; }
  • 11.
    Types of Functions •There are two types of functions in C programming: • Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts() etc. • 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.
  • 12.
    1. Example // Cprogram to implement the pre defined library function #include <math.h> #include <stdio.h> // Driver code int main() { double Number; Number = 49; // Computing the square root with the help of predefined C library function double squareRoot = sqrt(Number); printf("The Square root of %.2lf = %.2lf", Number, squareRoot); return 0; }
  • 13.
    2. Example // Cprogram to show user-defined functions #include <stdio.h> int sum(int a, int b) { return a + b; } // Driver code int main() { int a = 30, b = 40; // function call int res = sum(a, b); printf("Sum is: %d", res); return 0; }
  • 14.
    Return Value • AC 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. 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.
  • 15.
    • Let's seea simple example of C function that returns int value from the function. 1. Example with return value: int get() { return 10; } 2. Example with return value: float get() { return 10.2; }
  • 16.
    Different aspects offunction 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. 1. function without arguments and without return value 2. function without arguments and with return value 3. function with arguments and without return value 4. function with arguments and with return value
  • 17.
    1. Example forFunction without argument and without return value 1.1 Example #include<stdio.h> void printName(); void main () { printf("Hello "); printName(); } void printName() { printf("MCA"); }
  • 18.
    1.2 Example #include<stdio.h> void sum(); voidmain() { printf("n 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); }
  • 19.
    2. Example forFunction without argument and with return value 2.1 Example #include<stdio.h> int sum(); void main() { int result; printf("n 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; }
  • 20.
    Example 2.2: programto calculate the area of the square #include<stdio.h> int square(); void main() { printf(“calculate the area of the squaren"); float area = square(); printf("The area of the square: %fn", area); } int square() { float side; printf("Enter the length of the side in meters: "); scanf("%f", &side); return side * side; }
  • 21.
    3. Example forFunction with argument and without return value 3.1 Example #include<stdio.h> void sum(int, int); void main() { int a, b; printf("ncalculate 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); }
  • 22.
    Example 3.2: programto calculate the average of five numbers. #include<stdio.h> void average(int, int, int, int, int); void main() { 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); } 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); }
  • 23.
    4. Example forFunction with argument and with return value 4.1 Example #include<stdio.h> int sum(int, int); void main() { int a, b, result; printf("ncalculate 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; }
  • 24.
    Example 4. 2:Program to check whether a number is even or odd #include<stdio.h> int even_odd(int); void main() { int n,flag=0; printf("nTo 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"); } } int even_odd(int n) { if(n%2 == 0) { return 0; } else { return 0; } }
  • 25.
    Define multiple functionsin C #include <stdio.h> int sum(int a, int b) // Function to calculate the sum of two numbers { return a + b; } int product(int a, int b) // Function to calculate the product of two numbers { return a * b; } double area_of_circle(double radius) // Function to calculate the area of a circle { return radius * radius * 3.14; } int main() { int result = sum(10, 20); // Call the sum function printf("The sum is %dn", result); result = product(10, 20); // Call the product function printf("The product is %dn", result); double area = area_of_circle(5); // Call the area of a circle function printf("The area of the circle is %fn", area); return 0; }
  • 26.
    Example #include<stdio.h> int sum(int a,int b); int substact(int a, int b); int multiplay(int a, int b); double devision(int a, int b); int main() { int operation, fc, v1, v2; double fcd; printf("input 2 valuesn"); scanf("%d%d",&v1, &v2); /* telling the user to choose any type of calculator */ printf("please choose what you want to do with your valuesn"); printf("1- Sumn"); printf("2- substracttionn"); printf("3- multiplayn"); printf("4- devisionn"); scanf("%d", &operation); //input a switch(operation) {
  • 27.
    case 1: fc =sum(v1, v2); printf("sum of two values = %dn", fc); break; case 2: fc = substact(v1, v2); printf("substract of two values = %d", fc); break; case 3: fc = multiplay(v1, v2); printf("multiply of two values = %dn", fc); break; case 4: fcd = devision(v1, v2); printf("division of two values = %dn", fcd); break; default: printf("wrong choicen"); } return 0; }
  • 28.
    int sum(int a,int b) { int sum=0; sum=a+b; return sum; } int substact(int a, int b) { int sub=0; sub=a-b; return sub; } int multiplay(int a, int b) { int mult=1; mult=a*b; return mult; } double devision(int a, int b) { double devi=1; devi=a/b; return devi; }
  • 29.
    Function Prototype • Afunction 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: returntype function_name (datatype of parameter1 parameter1 , datatype of parameter2 parameter2, datatype of parameter3 parameter 3, . . . . . . . . . . .); • For Example : int add(int number1, int number2, int number3); • Here in this function prototype, we can easily understand that : Return type of the function = integer Number of parameters = 3 DataType of Parameters = integer
  • 30.
    Importance of FunctionPrototype in C • In C language, programs are compiled line by line and if we call a function before we have defined it in the program , we will get some warning like [Warning] conflicting types for 'function' previous implicit declaration of 'function' was here. • So to avoid this we declare function prototypes at the start of our programs and may define the functions later on. So that the compiler gets to know that there exists a function with this method signature and hence doesn't give an error.
  • 31.
    Example: Program WithoutFunction Prototype #include <stdio.h> int main() { int a=10; //first number int b=4; // second number int c=5; // third number int res=add(a, b, c); // calling add function printf("Sum of all the above 3 numbers is %d ",res); // printing result } // here function definition is after the main program int add(int a, int b, int c) { int result=a+b+c; return result; }
  • 32.
    Examples of FunctionPrototype in C #include <stdio.h> #include<math.h> #include<string.h> void odd_even(int number); // function prototype for odd_even function int main() { int number1; printf(" Please enter the number you want to know " ); scanf( "%d",&number1) ; // taking number as input from user odd_even(number1); // calling odd even function return 0 ; } // here function definition is after the main program void odd_even(int number) // function definition { if(number%2==0) printf("%d is an even number",number); else printf("%d is an odd number ",number); }
  • 33.
    Difference between FunctionPrototype and Function Definition Function Prototype Function Definition 1. The function prototype includes the function name, return type, and parameters but does not include the function body. 1. The function definition includes the function name, return type, and parameters, as well as the function body. 2. The function implementation is not present in the function prototype. 2. The function implementation is included in the function definition. 3. Typically appears at the top of a source file or in a header file. 3. Typically appears below the prototype in a source file. 4. Used to enable the compiler to verify the function’s usage and type- checking during compilation. 4. Used to enable the linker to create the executable code that can be executed by the computer. Syntax: return_type function_name(parameter_list); Syntax: return_type function_name();
  • 34.
    Call by Valueand Call by Reference • 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: A copy of the variable is passed to the function. • Call by reference: An address of the variable is passed to the function.
  • 35.
    • Functions canbe invoked in two ways: Call by Value or Call by Reference. These two ways are generally differentiated by the type of values passed to them as parameters. • The parameters passed to the function are called actual parameters whereas the parameters received by the function are called formal parameters.
  • 36.
    1. Call ByValue • In call by value method of parameter passing, the values of actual parameters are copied to the function’s formal parameters. • There are two copies of parameters stored in different memory locations. • One is the original copy and the other is the function copy. • Any changes made inside functions are not reflected in the actual parameters of the caller.
  • 37.
    1. Example ofCall by Value #include <stdio.h> void swapx(int x, int y); // Function Prototype int main() // Main function { int a = 10, b = 20; // Pass by Values swapx(a, b); // Actual Parameters printf("In the Caller:na = %d b = %dn", a, b); return 0; } // Swap functions that swaps two values void swapx(int x, int y) // Formal Parameters { int t; t = x; x = y; y = t; printf("Inside Function:nx = %d y = %dn", x, y); }
  • 38.
    2. Example #include<stdio.h> void swap(intn1, int n2) { int temp = n1; n1 = n2; n2 = temp; } int main() { int x = 20; int y = 68; printf("The numbers before swapping n1 and n2 %d %d n",x, y); swap(x, y); printf("The numbers after swapping n1 and n2 %d %d n",x, y); return 0; }
  • 39.
    2. Call byReference • In call by reference method of parameter passing, the address of the actual parameters is passed to the function as the formal parameters. • Both the actual and formal parameters refer to the same locations. • Any changes made inside the function are actually reflected in the actual parameters of the caller.
  • 40.
    1. Example ofCall by Reference #include <stdio.h> void swapx(int*, int*); // Function Prototype int main() // Main function { int a = 10, b = 20; // Pass reference swapx(&a, &b); // Actual Parameters printf("Inside the Caller:na = %d b = %dn", a, b); return 0; } // Function to swap two variables by references void swapx(int* x, int* y) // Formal Parameters { int t; t = *x; *x = *y; *y = t; printf("Inside the Function:nx = %d y = %dn", *x, *y); }
  • 41.
    2. Example #include<stdio.h> void swap(int*n1 ,int *n2) { int temp = *n1; *n1 = *n2; *n2 = temp; } int main() { int n1 = 20; int n2 = 68; printf("The numbers before swapping n1 and n2 %d %d n",n1, n2); swap(&n1, &n2); printf("The numbers after swapping n1 and n2 %d %d",n1, n2); return 0; }
  • 42.
    Difference between callby value and call by reference in c No. Call by value Call by reference 1 A copy of the value is passed into the function An address of value is passed into the function 2 Changes made inside the function is limited to the function only. The values of the actual parameters do not change by changing the formal parameters. Changes made inside the function validate outside of the function also. The values of the actual parameters do change by changing the formal parameters. 3 Actual and formal arguments are created at the different memory location Actual and formal arguments are created at the same memory location
  • 43.
    Recursion in C •Recursion is the process of calling a function itself repeatedly until a particular condition is met. A function that calls itself directly or indirectly is called a recursive function and such kind of function calls are called recursive calls. How recursion works? void recurse() { ... .. ... recurse(); ... .. ... } int main() { ... .. ... recurse(); ... .. ... }
  • 44.
  • 45.
    Example: Sum ofNatural Numbers Using Recursion #include <stdio.h> int sum(int n); int main() { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum = %d", result); return 0; } int sum(int n) { if (n != 0) return n + sum(n-1); // sum() function calls itself else return n; }
  • 46.