0% found this document useful (0 votes)
32 views10 pages

Unit 1

A function is a block of code that performs a specific task. There are two types of functions in C: library functions and user-defined functions. Library functions are pre-compiled functions included in header files like printf() and scanf(), while user-defined functions are created by the programmer. Functions have components like a function prototype, parameters, definition, call, and a return statement. Functions can be used to organize code into logical, reusable blocks and improve modularity. There are four categories of functions based on whether they accept arguments and return values.

Uploaded by

shivamchunara42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views10 pages

Unit 1

A function is a block of code that performs a specific task. There are two types of functions in C: library functions and user-defined functions. Library functions are pre-compiled functions included in header files like printf() and scanf(), while user-defined functions are created by the programmer. Functions have components like a function prototype, parameters, definition, call, and a return statement. Functions can be used to organize code into logical, reusable blocks and improve modularity. There are four categories of functions based on whether they accept arguments and return values.

Uploaded by

shivamchunara42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

US02MABCA01 : Advanced C Programming

Unit – 1

What is a function?

A function is a set of program statements that can be processed independently.

Types of functions in C

1. Library function
2. User defined function

Library functions

Functions are included with C compilers are known as library functions. These functions are
built-in, pre-compiled and ready to use.

Functions such as printf(), scanf(), pow(), sqrt() etc. are part of C standard library functions.
These functions are defined in C header files. We include these header files in our program as
per our need.

User defined functions

Functions defined by theuserare known as user defined function.

The user can define any number of functions depending on the need.

Syntax of user-define function:

Return_type function_name(arguments)

//Body of a function

Example:

void sum()

Page 1
:

Function components/Function elements

Every function has the following components/elements

- Function declaration or prototype


- Function parameters
- Function definition
- Function call
- return statement
Example: Write a C++ program to find maximum of two integer numbers.

#include<stdio.h>

#include<conio.h>

void max(int x, int y); // function prototype

void main()

int a, b, max;

clrscr();

printf(“Enter value of a :”);

scanf(“%d”, &a);

printf(“Enter value of b :“);

scanf(“%d”, &b);

max(a, b); // a and b is actual parameter

getche();

void max( int x, int y) // x and y formal parameter

Page 2
{

if ( x > y )

printf(“Maximum number is %d”, x);

else

printf(“Maximum number is %d”, y);

Function prototype: The declaration of a function before main() function is a function prototype. In above
program statement - void max(int x, int y); is a function prototype.

Function definition: It includes function declarator (or prototype) and function body.

Function parameters: The parameters specified in the function call are known as actual parameter. The
parameters specified in the calling function are known as formal parameter. In above example, the
parameters a and b are actual parameter. The parameters x and y are formal parameter. The scope of
formal parameter is limited to its function only.

Function call: A function call is specified by the function name followed by the arguments enclosed in
parentheses and terminated by a semicolon.

In above program, function call statement is max(a, b);

Callee: A function which is called. It is also known as called function.

Caller: A function which calls. It is also known as calling function.

return statement:

- A function that returns something is indicated by the return data types instead of void.

- The return statement can be anywhere in the body of function.

- When return statement found, then execution control will be return to the calling function.

Example: Write a function in C that returns maximum of two integer numbers.

#include<stdio.h>

#include<conio.h>

Page 3
int max(int x, int y);

void main()

{ int a, b, c, max;

clrscr();

printf(“Enter a…”);

scanf(“%d”,&a);

printf(“Enter b…”);

sacnf(%d”, &b);

c = max(a, b);

printf(“Maximum number is %d”, c);

getche();

int max( int x, int y)

{ if ( x > y )

return x;

else

return y;

In above example, return x; and return y; are return statements. Its value is stored in variable c which is in
main function.

The limitation of return statement is that it can be return only one value from a function.

Page 4
Need of Functions:

The Advantages of the function are

- Modular programming
- Reduction in the amount of work and development time
- Program and function debugging is easier.
- Division of work is simplified due to the use of divide-and-conquer principle.
- Reduction in size of the program due to code reusability
- Function can be access repeatedly without redevelopment, which in turn promotes reuse of code.
- Library of function can be implemented by combining well designed, tested and proven function.

Categories of a Function:

The categories of a function are:

1. Function without argument, No return statement


2. Function without argument, with return statement
3. Function with argument, No return statement
4. Function with argument, with return statement

Let us understand each category with an example.

[ 1 ]. Function without argument, No return statement:

- This type of function does not receive any argument as well as does not return any value.
- This type of function does not communicate with the caller function.
- It works as an independent working block.
Syntax:
The syntax of a function without argument, no return statement is …
void function_name( )
{
// Function body
}

Example: To find sum of two numbers.


#include<stdio.h>

Page 5
#include<conio.h>

void sum( )

int a=5, b=3, c ;

c = a + b;

printf(“\n sum = %d”,c);

void main( )

clrscr( );

sum( );

getch( );

Output:

sum = 8

- In above example, the function sum( ) is declared. This function does not contain argument and
return data type.

[ 2 ]. Function without argument, with return statement:

- This type of function does not receive any argument but it returns a value.
- This type of function communicates with the caller function by returning value back to
the caller.
Syntax:
The syntax of a function without argument, with return statement is …
return_type function_name( )
{
Page 6
// Function body

return some_value;
}

Example: To find sum of two numbers.


#include<stdio.h>

#include<conio.h>

int sum( )

int a=5, b=3, c ;

c = a + b;

return c;

void main( )

int c ;

clrscr( );

c = sum( );

printf(“\n sum = %d”,c);

getch( );

Output:

sum = 8

- In above example, the function sum( ) is declared. This function does not contain argument but it
returns summation of two numbers ( a and b).
[ 3 ]. Function with argument, No return statement:
Page 7
- This type of function receives an argument but does not return any value.
- For this type of function you must define function return type as void.
Syntax:
The syntax of a function with argument, No return statement is …

void function_name(datatype arg1, datatype arg2,...)


{
// Function body
}

- Example: To find sum of two numbers.


#include<stdio.h>

#include<conio.h>

void sum( int a, int b)

int c ;

c = a + b;

printf(“\n sum = %d”,c);

void main( )

int a=5, b=3;

clrscr( );

sum( a, b);

getch( );

Output:

sum = 8
Page 8
- In above example, the function sum( ) is declared. This function contains two integer arguments
(a and b).
[ 4 ]. Function with argument, with return statement:

- This type of function receives an argument as well as returns a value.


- Function with return and arguments, returns a value and may accept arguments.
- Since the function accepts input and returns a result to the caller, hence this type of
functions are most used and best for modular programming.
Syntax:
The syntax of a function with argument, with return statement is …
return_type function_name(datatype arg1,datatype arg2,...)
{
// Function body

return some_variable;
}

Example: To find sum of two numbers.


#include<stdio.h>

#include<conio.h>

int sum( int a, int b)

int c ;

c = a + b;

return c;

void main( )

int c;

clrscr( );

Page 9
c = sum( );

printf(“\n sum = %d”, c);

getch( );

Output:

sum = 8

- In above example, the function sum( ) is declared. This function contains two integer arguments
(a and b) and return its summation.

------X-----

Page 10

You might also like