C Programming Class 12 Functions (1)
C Programming Class 12 Functions (1)
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.
When you divide a large program into various functions, it becomes easy to
manage each function individually.
Whenever an error occurs in the program, you can easily investigate faulty
functions and correct only those errors. You can easily call and use functions
whenever they are required which automatically leads to saving time and space.
Advantages of Function
Types of function
There are two types of function in C programming:
User-defined functions
The printf() is a standard library function to send formatted output to the screen
(display output on the screen). This function is defined in the stdio.h header file.
Hence, to use the printf()function, we need to include the stdio.h header file using
#include <stdio.h>.
The sqrt() function calculates the square root of a number. The function is defined
in the math.h header file.
To compute the square root of a number, you can use the sqrt() library function.
The function is defined in the math.h header file.
#include <stdio.h>
#include <math.h>
int main()
{
float num, root;
printf("Enter a number: ");
scanf("%f", &num);
#include <math.h>
int main()
{
float num, varibale_name, exp;
printf("Enter a number: ");//25
scanf("%f", &num); //num=25
The function declarations (called prototype) are usually done above the main ()
function and take the general form:
The return_data_type: is the data type of the value function returned back to the
calling statement.
The function_name: is followed by parentheses
Arguments names with their data type declarations optionally are placed inside the
parentheses.
Function Definition
Function definition means just writing the body of a function. A body of a function
consists of statements which are going to perform a specific task. A function body
consists of a single or a block of statements.
return_type function_name (argument list)
{
function body;
Function call
A function call means calling a function whenever it is required in a program.
Whenever we call a function, it performs an operation for which it was designed.
function_name (argument_list)
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.
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 a C function that returns int value from the function.
int get()
{
return 0;
}
Example
#include <stdio.h>
/* function declaration */
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b); // actual parameters or arguments
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2) //num1=100 //num2=200 ( call by value) ( copy by
value)//formal parameters or parameters
{
/* local variable declaration */
int result;
return result;
}
/* function declaration */
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
return result;
}
#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}
#include<stdio.h>
int main()
{
float num1, num2, num3, largest;
#include<stdio.h>
// function prototype, also called function declaration
float cube ( float x );
// main function, program starts from here
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding cube \n");
scanf ( "%f", &m ) ;
// function call
n = cube ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}
//return ( x*x);
}
#include<stdio.h>
// function prototype, also called function declaration
float area_of_rectangle ( float l,float w );
// main function, program starts from here
int main( )
{
float a, b ,result;
printf ( "\nEnter the length of the rectangle \n");
scanf ( "%f", &a ) ;
// function call
result = area_of_rectangle ( a,b ) ;
printf ( "\nThe area of rectangle having length %f and breadth %f is
%f",a,b,result );
}
//return ( x*x);
}
#include<stdio.h>
// function declaration
float circleArea(float r);
int main()
{
float radius, area;
return 0;
}
// function definition
float circleArea(float r)
{
float area = 3.14 * r * r;
return area; // return statement
}
// function declarations
float minimum(float a, float b, float c);
float maximum(float a, float b, float c);
int main()
{
float n1, n2, n3;
return 0;
}
#include<stdio.h>
int sum(int x);
int main()
{
int range, result;
printf("Upto which number you want to find sum: ");
scanf("%d", &range);
result = sum(range);
printf("1+2+3+….+%d+%d = %d",range-1, range, result);
}
int sum(int n)
{
int add = 0;
for(int i=1; i<=n; i++)
{
add += i;
}
return add;
}
int main()
{
int c, n, f = 1;
return 0;
}
#include <stdio.h>
void main()
{
int no,factorial;
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
In the Fibonacci series, the next element will be the sum of the previous two
elements. The Fibonacci sequence is a series of numbers where a number is found
by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0,
1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth.
0 1 1 2 3 5 8 13 21 34 55
#include<stdio.h>
void main()
{
int n;
fibonacciSeries(n);
while (a<=range)
{
printf("%d\t", a);
temp = a+b;
a = b;
b = temp;
}
#include<stdio.h>
void fibonacciSeries(int n);
int main()
{
int term;
fibonacciSeries(term);
return 0;
}
void fibonacciSeries(int n)
{
int a=0, b=1, c;
for(int i=0; i<n; i++)
{
printf("%d\t", a);
c = a+b;
a = b;
b = c;
}
}
//WAP TO Find nth term of Fibonacci series in C using
function
#include<stdio.h>
result = fibonacciTerm(term);
return 0;
}
int fibonacciTerm(int n)
{
int a=0, b=1, c;
for(int i=1; i<n; i++)
{
c = a+b;
a = b;
b = c;
}
return a;
}
#include<stdio.h>
long power(int a, int b);
int main()
{
int num1, num2;
printf("Enter base and power: ");
scanf("%d %d",&num1, &num2);
return 0;
}
#include <stdio.h>
#include <math.h> // Used for constant PI referred as M_PI
/* Function declaration */
double getDiameter(double radius);
double getCircumference(double radius);
double getArea(double radius);
int main()
{
float radius, dia, circ, area;
return 0;
}
/**
* Calculate diameter of circle whose radius is given
*/
double getDiameter(double radius)
{
return (2 * radius);
}
/**
* Calculate circumference of circle whose radius is given
*/
double getCircumference(double radius)
{
return (2 * M_PI * radius); // M_PI = PI = 3.14 ...
}
/**
* Find area of circle whose radius is given
*/
double getArea(double radius)
{
return (M_PI * radius * radius); // M_PI = PI = 3.14 ...
}
#include <stdio.h>
void check_palindrome(int num);
int main()
{
int n;
printf("Enter an integer: ");
scanf("%d", &n);
check_palindrome(n);
}
void check_palindrome(int num)
{
int reverse_num=0, remainder,temp;
temp=num;
while(temp!=0)
{
remainder=temp%10;
reverse_num=reverse_num*10+remainder;
temp=temp/10;
}
if(reverse_num==num)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);
}
//C program to check whether the given number is prime or composite using
user-defined functions
#include<stdio.h>
#include <stdio.h>
void check_factors(int num);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
check_factors(n);
}
void check_factors(int num)
{
int i;
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i)
{
if (num % i == 0)
{
printf("%d ", i);
}
}
Call by value in C
● In the 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 the 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
the formal parameter is the argument which is used in the function definition.
Call by value method copies the value of an argument into the formal
parameter of that function. Therefore, changes made to the parameter of the
main function do not affect the argument.
#include<stdio.h>
int main()
int a = 10;
printf("Address of a = %p\n",&a);
print(a);
return 0;
void print(int a)
{
printf("From print function ...\n");
printf("Address of a = %p\n",&a);
Output:
Address of a = 0x7ffee9fcfbd8
Address of a = 0x7ffee9fcfbbc
Note:
We can observe that the address of a in the main function and the address of a in print
function are different.
So, if we change the value of a in print function, it will not affect the value of a in the
main function.
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
swap(a,b);
int temp;
temp = a;
a=b;
b=temp;
// Formal parameters, a = 20, b = 10
Output:-
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.
While calling a function, instead of passing the values of variables, we pass address
of variables(location of variables) to the function known as “Call By References.
#include <stdio.h>
// Function Prototype
void swapx(int*, int*);
// Main function
int main()
{
int a = 10, b = 20;
// Pass reference
swapx(&a, &b);
return 0;
}
t = *x;
*x = *y;
*y = t;
#include<stdio.h>
int main()
{
greatNum(); // function call
return 0;
}
#include<stdio.h>
int main()
{
int result;
result = greatNum(); // function call
printf("The greater number is: %d", result);
return 0;
}
#include<stdio.h>
int main()
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
greatNum(i, j); // function call
return 0;
}
#include<stdio.h>
int greatNum(int x, int y ); // function declaration
int main()
{
int i, j, result;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
result = greatNum(i, j); // function call
printf("The greater number is: %d", result);
return 0;
}