0% found this document useful (0 votes)
28 views46 pages

15 Functions 14 08 2023

Here are the key points about functions without arguments and without return value: - The function does not accept any parameters or arguments. - The function does not return any value. - Commonly used for tasks like printing, displaying messages etc. where returning a value is not required. - The return type of such functions is void. - Control returns to the calling function after the execution of statements inside the defined function. - Example: A function to print a welcome message without needing any input or returning output. So in summary, functions without arguments and return value are useful when we want to perform some task without needing inputs or returning outputs, like simple printing or displaying messages. The control returns to the calling function after

Uploaded by

Dhruv Gupta
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)
28 views46 pages

15 Functions 14 08 2023

Here are the key points about functions without arguments and without return value: - The function does not accept any parameters or arguments. - The function does not return any value. - Commonly used for tasks like printing, displaying messages etc. where returning a value is not required. - The return type of such functions is void. - Control returns to the calling function after the execution of statements inside the defined function. - Example: A function to print a welcome message without needing any input or returning output. So in summary, functions without arguments and return value are useful when we want to perform some task without needing inputs or returning outputs, like simple printing or displaying messages. The control returns to the calling function after

Uploaded by

Dhruv Gupta
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/ 46

Programming

1 Dr. Poongundran Selvaprabhu 19/02/2024


C programming

Dr. Poongundran Selvaprabhu, B.E., M.S.,


Ph.D.,
Assistant Professor (Senior),
Department of Communication Engineering,
School of Electronics Engineering (SENSE),
Vellore Institute of Technology (VIT),
Vellore, Tamil Nadu, India.
02/19/2024

3
PoonGundran
WHY???

02/19/2024

4
PoonGundran
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.

02/19/2024

5
PoonGundran
FUNCTIONS IN C/C++
• A function is a set of statements that take
inputs, do some specific computation and
produces output.
• The idea is to put some commonly or repeatedly
done task together and make a function so that
instead of writing the same code again and
again for different inputs, we can call the
function.

02/19/2024

6
PoonGundran
WHY DO WE NEED
FUNCTIONS?
• Functions help us in reducing code
redundancy.
• If functionality is performed at multiple
places in software, then rather than writing
the same code, again and again, we create
a function and call it everywhere.
• Consider a big file having many lines of
codes. It becomes really simple to read and
use the code if the code is divided into
functions.
02/19/2024

7
PoonGundran
ADVANTAGE OF FUNCTIONS IN C
• There are the following advantages of C 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.
02/19/2024

8
PoonGundran
• 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.

02/19/2024

9
PoonGundran
SN C function aspects Syntax

1 Function return_type function_name


declaration (argument list);

2 Function call function_name (argument_list)

3 Function definition return_type function_name


(argument list) {function body;}

10
02/19/2024
PoonGundran
THE SYNTAX OF CREATING
FUNCTION IN C
return_type function_name
(data_type parameter...)
{
//code to be executed
}

11
02/19/2024
PoonGundran
12
02/19/2024
PoonGundran
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.

13
02/19/2024
PoonGundran
HOW USER-DEFINED FUNCTION WORKS?

14
02/19/2024
PoonGundran
EX: USER-DEFINED FUNCTION
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
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
}02/19/2024

15
PoonGundran
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 a,b are passed to the
function.
The function prototype is not needed if the user-defined
function is defined before the main() function.

16
02/19/2024
PoonGundran
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.

17
02/19/2024
PoonGundran
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.

18
02/19/2024
PoonGundran
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.
• If n1 is of char type, variable 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.

19
02/19/2024
PoonGundran
20
02/19/2024
PoonGundran
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; (or) return (a+b);

21
02/19/2024
PoonGundran
WITHOUT 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.

22
02/19/2024
PoonGundran
EXAMPLE WITH RETURN
VALUE:
int get()
{
return 10;
}

In the above example, we have to return 10 as a


value, so the return type is int. If you want to return
floating-point value (e.g., 10.2, 3.1, 54.5, etc), you
need to use float as the return type of the method.

23
02/19/2024
PoonGundran
EXAMPLE WITH RETURN
FLOAT VALUE:
float get(){
return 10.2;
}

In the above example, we have to return 10.2 as a value,


so the return type is float. you need to call the function, to
get the value of the function.

24
02/19/2024
PoonGundran
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.
•Function without arguments & without return value.
•Function without arguments & with return value.
•Function with arguments & without return value.
•Function with arguments & with return value.

25
02/19/2024
PoonGundran
Function without argument
and without return value

26
02/19/2024
PoonGundran
EX1: FUNCTION WITHOUT ARGUMENT
AND WITHOUT RETURN VALUE
#include<stdio.h>
void printName();
void main ()
{
printf("Hello!\n");
printName();
}
void printName()
{
printf("Welocme to C Programming.\n");
}

27
02/19/2024
PoonGundran
EX2: FUNCTION WITHOUT ARGUMENT
AND WITHOUT RETURN VALUE
#include<stdio.h>
void sum();
void main()
{ printf("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); }

28
02/19/2024
PoonGundran
NO ARGUMENTS PASSED AND NO RETURN VALUE
#include <stdio.h>
void checkPrimeNumber();
int main()
{ checkPrimeNumber(); // argument is not passed
return 0;
} // return type is void meaning doesn't return any value
void checkPrimeNumber()
{ int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i <= n/2; ++i)
{ if(n%i == 0)
{ flag = 1; } }
if (flag == 1)
printf("%d is not a prime number.", n);
else

29
printf("%d is a prime number.", n); }
PoonGundran
02/19/2024
Function without argument and with
return value

30
02/19/2024
PoonGundran
EX: FUNCTION WITHOUT ARGUMENT AND
WITH RETURN VALUE
#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);

31
PoonGundran

02/19/2024
EX: FUNCTION WITHOUT ARGUMENT AND
WITH RETURN VALUE
#include<stdio.h>
int square()
void main()
{
printf("Going to calculate the area of the square\n");
float area = square();
printf("The area of the square: %f\n",area);
}
int square()
{
float side;
printf("Enter the length of the side in meters: ");
scanf("%f",&side);
return side * side;

32
} PoonGundran

02/19/2024
#include <stdio.h>
int getInteger();
int main()
{ int n, i, flag = 0;
// no argument is passed
n = getInteger();
for(i=2; i<=n/2; ++i)
{ if(n%i==0){
flag = 1;
break;
} }
if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
return 0; }
// returns integer entered by the user
int getInteger()
{ int n;
printf("Enter a positive integer: ");
scanf("%d",&n);

33
return n; }
PoonGundran
02/19/2024
Function with argument and without
return value

34
02/19/2024
PoonGundran
EX: FUNCTION WITH ARGUMENT AND
WITHOUT RETURN VALUE
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b;
printf("\n 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);
}

35
02/19/2024
PoonGundran
EX2: WITHOUT RETURN VALUE
#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;

36
printf("The average of given five numbers :02/19/2024
PoonGundran
%f",avg); }
#include <stdio.h>
void checkPrimeAndDisplay(int n);
int main()
{ int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
// n is passed to the function
checkPrimeAndDisplay(n);
return 0;
}
// return type is void meaning doesn't return any value
void checkPrimeAndDisplay(int n)
{ int i, flag = 0;
for(i=2; i <= n/2; ++i)
{ if(n%i == 0){
flag = 1;
break;
} }
if(flag == 1)
printf("%d is not a prime number.",n);
else
printf("%d is a prime number.", n); }

37
02/19/2024
PoonGundran
Function with argument and with
return value

38
02/19/2024
PoonGundran
Function with argument and with return value
#include<stdio.h>
int sum(int, int);
void main()
{ int a,b,result;
printf("\n 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; }

39
02/19/2024
PoonGundran
CHECK WHETHER A NUMBER IS EVEN OR ODD
#include<stdio.h>
int even_odd(int);
void main()
{ int n,flag=0;
printf("\nGoing to 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 1; }
else {
return 0; } }

40
02/19/2024
PoonGundran
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
{ int n, flag;
printf("Enter a positive integer: ");
scanf("%d",&n);
// n is passed to the checkPrimeNumber() function
// the returned value is assigned to the flag variable
flag = checkPrimeNumber(n);
if(flag == 1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
// int is returned from the function
int checkPrimeNumber(int n)
{ int i;
for(i=2; i <= n/2; ++i)
{ if(n%i == 0)
return 1; }

41
02/19/2024
return 0; }
PoonGundran
C LIBRARY FUNCTIONS

• Functions are used to perform some specific operations.


• The library functions are created by the designers of
compilers.
• All C standard library functions are defined inside the
different header files saved with the extension .h.
• We need to include these header files in our program to
make use of the library functions defined in such header
files.

42
02/19/2024
PoonGundran
SN Header Description
file
1 stdio.h This is a standard input/output header file. It
contains all the library functions regarding
standard input/output.
2 conio.h C header file used mostly by MS-DOS compilers
to provide console input/output.
3 string.h It contains all string related library functions
like gets(), puts(),etc.
4 stdlib.h This header file contains all the general library
functions like malloc(), calloc(), exit(), etc.
5 math.h This header file contains all the math
operations related functions like sqrt(), pow(),
etc.
6 time.h This header file contains all the time-related
functions.
7 ctype.h This header file contains all character handling
functions.

43
02/19/2024
PoonGundran
8 stdarg.h Variable argument functions are defined in this
header file.

9 signal.h All the signal handling functions are defined in


this header file.

10 setjmp.h This file contains all the jump functions.

11 locale.h This file contains locale functions.

12 errno.h This file contains error handling functions.

13 assert.h This file contains diagnostics functions.

44
02/19/2024
PoonGundran
C FUNCTION EXAMPLES
PROBLEMS
1. Write a C program using functions and Display all prime numbers
between two Intervals.
Note: Use Function with argument and with return value Method
Output:
Enter Lowest limit: 11
Enter Highest limit: 30
Prime numbers between 11 and 30 are: 11,13, 17, 19 ,23 ,29

45
02/19/2024
PoonGundran
2. Write a C programming to find out maximum and minimum of
some values using function which will return an array.
Note: Use Function with argument and without return value Method
Output:
Enter 5 values:
29
10
35
69
20
Expected Output :
Minimum value is: 10
Maximum value is: 69

46
02/19/2024
PoonGundran

You might also like