Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

EXPERIMENT NO.

07

DATE OF PERFORMANCE: GRADE:

DATE OF ASSESSMENT: SIGNATURE OF LECTURER/ TTA:

AIM: To Understand the Concept of User Defined Function for C Programs.

THEORY:
A function is a group of statements that together perform a task. Every C program has at
least one function, which is main(), and all the most trivial programs can define additional
functions.You can divide up your code into separate functions. How you divide up your
code among different functions is up to you, but logically the division is such that each
function performs a specific task.A function declaration tells the compiler about a
function's name, return type, and parameters. A function definition provides the actual
body of the function.
The C standard library provides numerous built-in functions that your program can call.
For example, strcat() to concatenate two strings, memcpy() to copy one memory location
to another location, and many more functions.A function can also be referred as a method
or a sub-routine or a procedure, etc.

FIGURE 7.1 TYPES OF FUNCTION


 LIBRARY FUNCTIONS are those functions which are defined by C library,
example printf(), scanf(), strcat() etc. You just need to include appropriate header
files to use these functions. These are already declared and defined in C libraries.

Page 1 of 9
 USER-DEFINED FUNCTIONS are those functions which are defined by the user at
the time of writing program. Functions are made for code reusability and for saving
time and space.

DEFINING A FUNCTION
The general form of a function definition in C programming language is as follows,

return_type function_name( parameter list ) {

body of the function

A function definition in C programming consists of a function header and a function body.


Here are all the parts of a function,

RETURN TYPE: A function may return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.

FUNCTION NAME: This is the actual name of the function. The function name and the
parameter list together constitute the function signature.

PARAMETERS: A parameter is like a placeholder. When a function is invoked, you pass a


value to the parameter. This value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.

FUNCTION BODY: The function body contains a collection of statements that define what
the function does.

EXAMPLE

Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two.

/* function returning the max between two numbers */


int max(int num1, int num2) {

/* local variable declaration */


int result;

if (num1 > num2)

Page 2 of 9
result = num1;
else
result = num2;

return result;
}

FUNCTION DECLARATIONS
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.
A function declaration has the following parts,

return_type function_name( parameter list );

The function declaration is as follows,


int max(int num1, int num2);

Parameter names are not important in function declaration only their type is required, so
the following is also a valid declaration,

int max(int, int);

CALLING A FUNCTION
While creating a C function, you give a definition of what the function has to do. To use a
function, you will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called function.
A called function performs a defined task and when its return statement is executed or
when its function-ending closing brace is reached, it returns the program control back to
the main program.To call a function, you simply need to pass the required parameters
along with the function name, and if the function returns a value, then you can store the
returned value. For example,

#include <stdio.h>
/* 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);
Page 3 of 9
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;

return result;
}

OUTPUT:
Max value is : 200

CATEGORIES OF USER-DEFINED FUNCTION:


1.NO ARGUMENTS PASSED AND NO RETURN VALUE
2. NO ARGUMENTS PASSED BUT A RETURN VALUE
3. ARGUMENT PASSED BUT NO RETURN VALUE
4. ARGUMENT PASSED AND A RETURN VALUE

NO ARGUMENTS PASSED AND NO RETURN VALUE


EXAMPLE:
/* Function declaration */
void printline (void);
void value (void);
main()
{
printline();
value();
printline();
}
/* Function1: printline( ) */

void printline(void) /* contains no arguments */


{
int i ;

for(i=1; i <= 35; i++)


printf("%c",'-');
Page 4 of 9
printf("\n");
}
/* Function2: value( ) */
void value(void) /* contains no arguments */
{
int year, period;
float inrate, sum, principal;

printf("Principal amount?");
scanf("%f", &principal);
printf("Interest rate? ");
scanf("%f", &inrate);
printf("Period? ");
scanf("%d", &period);

sum = principal;
year = 1;
while(year <= period)
{
sum = sum *(1+inrate);
year = year +1;
}
printf("\n%8.2f %5.2f %5d %12.2f\n", principal,inrate,period,sum);
}

OUTPUT:
-----------------------------------

Principal amount? 5000

Interest rate? 0.12

Period? 5

5000.00 0.12 5 8811.71


-----------------------------------

NO ARGUMENTS PASSED BUT A RETURN VALUE


EXAMPLE:
#include <stdio.h>
int getInteger();

int main()
{

Page 5 of 9
int n, i, flag = 0;

// no argument is passed to the function


// the value returned from the function is assigned to n
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;
}

// getInteger() function returns integer entered by the user


int getInteger()
{
int n;

printf("Enter a positive integer: ");


scanf("%d",&n);

return n;
}

ARGUMENT PASSED BUT NO RETURN VALUE


EXAMPLE:
/* prototypes */
void printline (char c);
void value (float, float, int);
main( )
{
float principal, inrate;
int period;

Page 6 of 9
printf("Enter principal amount, interest");
printf(" rate, and period \n");
scanf("%f %f %d",&principal, &inrate, &period);
printline('Z');
value(principal,inrate,period);
printline('C');
}

void printline(char ch)


{
int i ;
for(i=1; i <= 52; i++)
printf("%c",ch);
printf("\n");
}
void value(float p, float r, int n)
{
int year ;
float sum ;
sum = p ;
year = 1;
while(year <= n)
{
sum = sum * (1+r);
year = year +1;
}
printf("%f\t%f\t%d\t%f\n",p,r,n,sum);
}
OUTPUT:
Enter principal amount, interest rate, and period

5000 0.12 5

ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

5000.000000 0.120000 5 8811.708984

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

ARGUMENT PASSED AND A RETURN VALUE


EXAMPLE:
void printline (char ch, int len);
value (float, float, int);

main( )
Page 7 of 9
{
float principal, inrate, amount;
int period;
printf(“Enter principal amount, interest”);
printf(“rate, and period\n”);
scanf(%f %f %d”, &principal, &inrate, &period);
printline (‘*’ , 52);
amount = value (principal, inrate, period);
printf(“\n%f\t%f\t%d\t%f\n\n”,principal,
inrate,period,amount);
printline(‘=’,52);
}
void printline(char ch, int len)
{
int i;
for (i=1;i<=len;i++)
printf(“%c”,ch);
printf(“\n”);
}
value(float p, float r, int n) /* default return type */
{
int year;
float sum;
sum = p; year = 1;
while(year <=n)
{
sum = sum * (l+r);
year = year +1;
}
return(sum); /* returns int part of sum */
}

OUTPUT
Enter principal amount, interest rate, and period

5000 0.12 5
***************************************************

5000.000000 0.1200000 5 8811.000000


= = = = = = = = = = = = = = = = = = = = = = = = = =

Page 8 of 9
EXERCISE:
1. Write a function to find sum of odd numbers from the natural numbers.
2. Write a program to find factorial of given number using function.
3. Write a function pow(x,y).
4. Write a function to find maximum from given array.
5. Write a function to reverse the string.
6. Write a function to sort a string from A to Z.

Page 9 of 9

You might also like