0% found this document useful (0 votes)
10 views29 pages

Functions

This document provides an overview of functions in C programming, including their definitions, types, and usage. It explains function declaration, definition, and the two methods of function calling: call by value and call by reference. Additionally, it covers recursion, user-defined functions, and includes examples to illustrate the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views29 pages

Functions

This document provides an overview of functions in C programming, including their definitions, types, and usage. It explains function declaration, definition, and the two methods of function calling: call by value and call by reference. Additionally, it covers recursion, user-defined functions, and includes examples to illustrate the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Functions in C

1. Through this lecture, students will understand


Learning the need of functions, how to declare and
Outcomes define functions in C.
2. Students will learn the two methods of function
calling – call by value and call by reference.
3. Students will be acquainted with the concept of
recursion, their declaration and definition.
Contents
• What is Function?
• Need of Function
• Types of function
• Function declaration and definition
• Call by value and call by reference
• Recursion
Functions

Also called subroutine


Self contained block of
in some other
code that performs a
languages like php,
specific task
python

It is independent which
can perform its task
without interference
A function is named
from or interfering with
other parts of the
program.

A function is defined
Every C program has at using its predefined
least one function, syntax, and are called
which is main() from elsewhere in your
program.
Why Functions?

Improves the
readability of code

Debugging of code
Avoid duplicating
is easier, errors are
codes
easy to be traced.

Improve Reduces the size of


Modularity the code
C Functions: General Form
Types of functions
Library Functions User-defined
1.Built-in functions to handle tasks Functions
such as mathematical computations,
1.Users or programmers define a
I/O processing, string handling etc.
function according to their
2.Defined in the header file requirement.
3.printf() : function to send formatted 2.The main() function is also a user-
output to the screen : defined defined function because the
in "stdio.h" header file. statements inside the main function
4. Some other library functions are is defined by the user, only the
scanf() to read input from the function name is defined in a
screen, getchar() to return library.
characters typed on screen,
putchar() to output a single
character to the screen, fopen() to
open a file, fclose() to close a file
etc.
User Defined Functions
”What you have to do when I call you“
A programmer can define their own sets of code inside a function and use it
for n number of times using its function name.
Function Declaration
•A declaration merely provides a function prototype.
•It tells the compiler about a function's name, return type, and
parameters.
Function header :
Includes the return type and the list of parameters

Syntax:
return_type function_name( arg-typ name-1,...,arg-typ name-n);

Example:
int hex(int val);
int max(int num1, int num2);

Note: 1. Parameter names are not important in function declaration only


their type is required, so int max(int, int); is also a valid declaration
2. Function declaration is required when you define a function in one
source file and you call that function in another file.
Function Definition
The definition of a function includes both the function prototype and
the function body, i.e. its implementation.
Syntax : return_type function_name (argument list)
{
Set of statements – Block of code
}

• Contains Block of code


• Can be of any function_name
data type such variables names
• Advised to have along with their • Set of C
as int, double, a meaningful data types statements,
char, void, name which will be
short etc. • Kind of inputs for
• Easy to the function executed
understand the whenever a call
return_type purpose of argument list will be made to
function seeing the function.
it’s name
Calling a function

When a program
By creating a C To use a function, calls a function, the
function, we give a we will have to call program control is
definition of what that function to transferred to the
the function has to perform the defined called function
do. task. which performs a
defined task

When its return


To call a function, statement is
If the function you simply need to executed or when
returns a value, pass the required its closing brace is
then we can store parameters along reached, it returns
the returned value. with the function the program control
name. back to the main
program.
Illustration of
working of
Functions in C
Example of functions

#include <stdio.h>
int add(int a, int b); //function declaration
int main()
{ //main function definition
int a = 5, b = 10;
int sum;
printf("The value of a and b : %d %d ", a, b);
The value of a
sum = add(a, b); //function call and b : 5 10
printf("\nsum = %d ", sum); sum = 15
}
//function definition
int add(int a, int b)
{
int c;
c = a + b;
return c; //returns a integer value to the calling function
}
Classification of user defined functions into four
types, according to parameters and return value

Functions with Functions with


out out
arguments but arguments an
with return d without
values return values

Functions with Functions with


arguments but arguments an
without d with return
return values values
Functions Without Arguments Without Return Values

The calling function will not send parameters to the called function
and called function will not pass the return value to the calling
function.

#include <stdio.h>
void add(); //function declaration
int main()
{ //main function definition
add(); //function call without passing arguments
}
//function definition
The value of a and b :
void add() 5 10
{ int a = 5, b = 10; sum = 15
int c;
printf(" The values of a and b : %d %d ", a, b);
c = a + b;
printf(" \nsum : %d ", c); //no return values to the calling function
}
Functions With Arguments Without Return Values

The calling function will pass parameters to the called function but
called function will not pass the return value to the calling function.
#include <stdio.h>
void add(int,int); //function declaration
int main()
{ //main function definition
int sum;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
add(a, b); //function call with passing arguments to called function }
// function definition
The value of a and b :
void add(int a, int b) 5 10
{ sum = 15
int c;
c = a + b;
printf(" \nsum : %d ", c); //no return values to the calling function
main
}
Functions Without Arguments With Return Values

The calling function will not pass parameters to the called function
but called function will pass the return value to the calling function.
#include <stdio.h>
int add(); //function declaration
int main()
{ //main function definition
int sum;
sum = add(); //function call without passing arguments to called
function
printf(" \nsum = %d ", sum);
}
The value of a and b :
// function definition 5 10
int add() sum = 15
{ int c;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
c = a + b;
return c; //passing return values to the calling function main }
Functions With Arguments With Return Values

The calling function will pass parameters to the called function and
called function also pass the return value to the calling function
#include <stdio.h>
int add(int, int); //function declaration
int main()
{ //main function definition
int sum;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
sum = add( a, b); //function call with passing arguments
printf(" \nsum = %d ", sum);
}
// function definition The value of a and b :
5 10
int add(int a, int b)
sum = 15
{ int c;
c = a + b;
return c; //passing return values to the calling function main
}
Function Parameters
There are two types of function parameters:
Formal parameters:
Appear in a declaration or a definition of a function.

Actual parameters:
Appear in a call to the function.

Examples:
int f(int x); here x is a formal parameter

i = f(3); here 3 is the actual parameter


corresponding to the formal parameter.
Demonstrating difference between actual and formal
arguments
#include <stdio.h>
int addTwoInts(int, int); /* Prototype */
int main() /* Main function */
{
int n1 = 10, n2 = 20, sum;
/* n1 and n2 are actual arguments. They are the source of data. Caller
program supplies the data to called function in
form of actual arguments. */
sum = addTwoInts(n1, n2); /* function call */
printf("Sum of %d and %d is: %d \n", n1, n2, sum);
}
int addTwoInts(int a, int b)
/* a and b are formal parameters. They receive the values from actual
arguments when this function is called. */
{ return (a + b); }

OUTPUT ====== Sum of 10 and 20 is: 30


Difference between Actual and Formal Parameters

Actual vs Formal Parameters

The Formal Parameters are the


The Actual parameters are the values
variables defined by the function that
that are passed to the function when it
receives values when the function is
is invoked.
called.

Related Function

The actual parameters are passed by The formal parameters are in the called
the calling function. function.

Data Types

In actual parameters, there is no


In formal parameters, the data types of
mentioning of data types. Only the
the receiving values should be included.
value is mentioned.
Two ways of passing arguments to a function
Call by Value Call by Reference

1. While calling a function, instead of


1. While calling a function, we pass
passing the values of variables, we
values of variables to it. Such
pass address of variables(location of
functions are known as “Call By
variables) to the function known as
Values”.
“Call By References”.
2. In this method, the value of each 2. In this method, the address of actual
variable in calling function is copied variables in the calling function are
into corresponding dummy variables copied into the dummy variables of
of the called function. the called function.
3. With this method, the changes made
3. With this method, using addresses we
to the dummy variables in the called
would have an access to the actual
function have no effect on the values
variables and hence we would be
of actual variables in the calling
able to manipulate them
function.
Call by Value Call by Reference

4. Thus actual values of a and b remain 4. Thus actual values of a and b get
unchanged even after exchanging the changed after exchanging values of x
values of x and y. and y.

5. In call by values we cannot alter the


5. In call by reference we can alter the
values of actual variables through
values of variables through function calls.
function calls.

6. Pointer variables are necessary to


6. Values of variables are passed by
define to store the address values of
Simple technique.
variables.

7. A copy of the original variables are 7. No copy of the variables are created
created in memory stack. on the memory stack.

8. The pass by value approach creates


8. The pass by reference method creates
two or more copies of the original
only a single copy, thereby,
variables and, hence it is not memory
providing memory efficiency.
efficient.

9. The changes are visible only in


9. The changes are visible in every
a particular method where the
function.
arguments are passed.
// C program to illustrate call by value
#include <stdio.h>
// Function Prototype
void swapx(int x, int y);
// Main function
int main()
{ int a = 10, b = 20;
// Pass by Values
Output: x=20
swapx(a, b);
y=10 a=10 b=20
printf("a=%d b=%d\n", a, b);
return 0; }
// Swap functions that swaps two values
void swapx(int x, int y)
{ int t;
t = x;
x = y;
y = t;
printf("x=%d y=%d\n", x, y); }
// C program to illustrate call by reference
#include <stdio.h>
// Function Prototype
void swapx(int x, int y);
// Main function
int main()
{ int a = 10, b = 20;
// Pass by Reference Output: x=20
swapx(&a, &b); y=10 a=20
printf("a=%d b=%d\n", a, b); b=10
return 0; }
// Swap functions that swaps two values
void swapx(int* x, int* y)
{ int t;
t = *x;
*x = *y;
*y = t;
printf("x=%d y=%d\n", *x, *y); }
Program to check prime numbers between two integers
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
{ int n1, n2, i, flag;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1+1; i<n2; ++i) {
// i is a prime number, flag will be equal to 1 Enter two positive
flag = checkPrimeNumber(i); integers: 12 30
if(flag == 1) printf("%d ",i); } Prime numbers
return 0; } between 12 and 30
// user-defined function to check prime number are: 13 17 19 23 29
int checkPrimeNumber(int n)
{ int j, flag = 1;
for(j=2; j <= n/2; ++j)
{ if (n%j == 0) { flag =0; break; } }
return flag; }
Program to Check Whether a Number can be Expressed
as Sum of Two Prime Numbers
#include <stdio.h>
int checkPrime(int n);
int main()
{ int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i = 2; i <= n/2; ++i) Enter a positive
{ // condition for i to be a prime number integer: 34
if (checkPrime(i) == 1) 34 = 3 + 31
{ // condition for n-i to be a prime number 34 = 5 + 29
if (checkPrime(n-i) == 1) 34 = 11 + 23 34 =
{ // n = primeNumber1 + primeNumber2 17 + 17
printf("%d = %d + %d\n", n, i, n - i); flag = 1; } } }
if (flag == 0)
printf("%d cannot be expressed as the sum of two prime numbers.", n);
return 0; }
// Function to check prime number
int checkPrime(int n) { int i, isPrime = 1; for(i = 2; i <= n/2; ++i) { if(n % i == 0)
{ isPrime = 0; break; } } return isPrime; }
• Program to check prime and
armstrong number using
function
• Program to print maximum of
Implement C three numbers using function.
programs • Program to find square of a
given number using function
• Write a program in C to check
a given number is even or odd
using the function

You might also like