Lecture 2.3.2 User Defined Function
Lecture 2.3.2 User Defined Function
Examples
1
DEFINITION: A large C program is divided into basic building
blocks called C function. C function contains set of instructions
enclosed by “{ }” which performs specific operation in a C
program. Actually, Collection of these functions creates a C
program.
Components of a Function:
User
Defined
Function
2
Return-type: This specifies the data type of the value being returned
by the function. A function may or may not return a value. If the
function does not return a value, then the return type is void. In this
case, the return value is an integer value c, which means the return
type would be int.
Parameter list: The list of formal parameters being passed onto the
function. In this case, there are two parameters of type int passed to
the function.
User Defined
Function Local variables or local declarations: The variables that are declared
inside the function are called local variables. The scope of these
variables lies within the function and they are not accessible outside
the function.
3
Function name: The name of the function can be anything that you want.
The standard is to make it something related to what it's supposed to do.
The naming convention follows the same rule as that of variable naming
convention in C.
Function declaration: Tells the compiler all about the function. These
include the function's name, the return type, and the number and types
of parameters. The body of the function having the function definition
can be defined somewhere else.
User Defined A function declaration has the following parts:
• Or
• return_type function_name(datatypes)
int sum(int,int);
4
Types of user defined functions:
5
Such functions can either be used to display information or they are completely
dependent on user inputs.
Example: It takes 2 numbers as input from user, and display which is the greater
number.
#include<stdio.h>
void greatNum(); // function declarationint
main()
{
greatNum(); // function call
return 0;
}
Function with no void greatNum() // function definition
arguments and {
int i, j;
no return value printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
if(i > j)
{
printf("The greater number is: %d", i);
}
else
{
printf("The greater number is: %d", j);
}
} 6
We have modified the above example to make the function greatNum() return the
number which is greater amongst the 2 input numbers.
#include<stdio.h>
int greatNum(); // function declaration
int main()
{
int result;
result = greatNum(); // function call
printf("The greater number is: %d", result);
return 0;}int greatNum() // function definition
Function with {
int i, j, greaterNum;
no arguments printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j); if(i > j)
and a return {
greaterNum = i;
value }
else
{
greaterNum = j;
} // returning the result
return greaterNum;
}
7
We are using the same function as example again and again, to demonstrate that to
solve a problem there can be many different ways.
This time, we have modified the above example to make the
function greatNum() take two int values as arguments, but it will not be returning
anything.
#include<stdio.h>
void greatNum(int a, int b); // function declarationint main()
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
Function with greatNum(i, j); // function call
arguments and }
return 0;
Passing
Parameters
10
EXAMPLE: PROGRAM TO FIND SQUARE OF A NUMBER
#include<stdio.h>
float square ( float x ); //function declaration
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
Example of scanf ( "%f", &m ) ;
n = square ( m ) ; // function call
parameter printf ( "\nSquare of the given number %f is %f”,m,n );
passing }
float square ( float x ) // function definition
{
float p ;
p=x*x;
return ( p ) ;
}
11
There are two ways that a C function can be called from a program.
Functions can be invoked in two ways:
• Call by Value
• Call by Reference.
12
Example: Swap two numbers using
Call by Value
#include <stdio.h>
void swap(int x, int y);
int main()
{
int a = 2, b = 3;
swap(a, b);
printf("a=%d b=%d\n", a, b);
Call by Value return 0;
}
Example void swap(int x, int y)
{
int t;
t = *x;
*x = *y; Thus actual values of a and b get
*y = t;
changed after exchanging values of x
printf("x=%d y=%d\n", *x, *y); and y. In call by reference we can alter
} the values of variables through
function calls.
14
SUMMARY…..
15
PROGRAMS
Q1 Write a program in C to check a given number is even or odd using the
function.
2. Write a program in C to check whether a number is a prime number or
not using the function.
3. Write a program in C to check armstrong numbers using the function.
FREQUENTLY 4. Write a program in C to find the sum of the series
1!/1+2!/2+3!/3+4!/4+5!/5 using the function.
ASKED 5. Write a program to display fibonacci series upto user required number
using function.
QUESTIONS
16
1. Choose correct statement about Functions in C Language.
A) A Function is a group of c statements which can be reused any number of
times.
B) Every Function has a return type.
C) Every Function may no may not return a value.
UTILISE YOUR D) All the above.
KNOWLEDGE TO 2 What is the output of C Program with functions.? void show(); int main()
ANSWER { show(); printf("ARGENTINA "); return 0; } void show() { printf("AFRICA "); }
A) ARGENTINA AFRICA
B) AFRICA ARGENTINA
Let us see how much you have
C) ARGENTINA
learned from the lecture and how
D) Compiler error
effectively you can apply your
knowledge…!!
3) How many values can a C Function return at a time.?
A) Only One Value
B) Maximum of two values
C) Maximum of three values
D) Maximum of 8 values
17
4 What are types of Functions in C Language.?
A) Library Functions
B) User Defined Functions
C) Both Library and User Defined
D) None of the above
UTILISE YOUR
5. Which of the following statement is true about a function with an
KNOWLEDGE TO argument?
ANSWER A. No value is pass to the function during function call.
B. function with an argument must not have return type
Let us see how much you have C. function with an argument is declared and define with parameter list
learned from the lecture and how D. none of the above
effectively you can apply your
knowledge…!!
18
Discussion
Forum Write a program for ATM machine using function.
19
Book Reference:
https://en.wikibooks.org/wiki/C_Programming
•
Vedio Lecture: REFERENCES
• https://nptel.ac.in/courses/106/106/106106127/
• https://www.youtube.com/watch?v=4-xX9vmPDsc
• https://www.youtube.com/watch?v=Dt9q3qiaqiA Book Reference
•
Websites:
https://www.tutorialspoint.com/cprogramming/c_functions.htm Video Lectures
• https://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functio
ns.html
• https://beginnersbook.com/2014/01/c-function-call-by-value-example Websites
/
•
•
•
20
•
•
THANK YOU….
21