0% found this document useful (0 votes)
4 views

function in c

The document provides an overview of functions in C programming, emphasizing modular programming and the importance of functions for code reuse, debugging, and maintenance. It details the types of functions, including library and user-defined functions, along with their advantages, syntax, and examples. Additionally, it covers function declaration, definition, return values, and function calls, illustrating these concepts with code snippets.

Uploaded by

tankmitesh143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

function in c

The document provides an overview of functions in C programming, emphasizing modular programming and the importance of functions for code reuse, debugging, and maintenance. It details the types of functions, including library and user-defined functions, along with their advantages, syntax, and examples. Additionally, it covers function declaration, definition, return values, and function calls, illustrating these concepts with code snippets.

Uploaded by

tankmitesh143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Functions in C

Prepared By: Prof. Shital Pathar


Assistant Professor
Department of Computer Engineering
Dharmsinh Desai UNiversity
Modular programming

● Modular programming takes a top-down approach towards software


development.
● When the algorithm of a certain problem involves long and complex logic, it
is broken into smaller, independent and reusable blocks known as modules.
● Each module is a separate, complete and reusable software component.
● The main advantage of this approach is that the code becomes easy to follow,
develop and maintain.
Top down Modular programming using functions

Main program

Function 1 Function 2 Function 3

Function A1 Function A2 Function B1 Function B2


What is function?

● A function in C is a block of organized reusable code that is performs a


single related action.
● Define the code once, and use it many times.
Need of Functions in C

● The length of source program can be reduced by using functions.


● Easy for debugging, testing and maintaining the code.
● Reuse the one code for many times.
Types of Functions in C

1. Library functions
2. User defined functions
Library Function
● A library function is also referred to as a “built-in function”.
● Built-in functions have the advantage of being directly usable without being
defined.
● A compiler package already exists that contains these functions, each of
which has a specific meaning.
● For Example: pow(), sqrt(), strcmp(), strcpy() etc.
● Advantages of C library functions
1. easy to use and optimized for better performance.
2. save a lot of time.
3. convenient as they always work.
User Defined Functions

● Functions that the programmer creates are known as User-Defined functions.


● They are also called subroutines or procedures in other languages.

Advantages of User-Defined Functions

1. Changeable functions can be modified as per need.


2. The Code of these functions is reusable in other programs.
3. These functions are easy to understand, debug and maintain.
Syntax of Functions in C

The syntax of function can be divided into 3 aspects:


1. Function Declaration
2. Function Definition
3. Function Calls
Function Declaration(prototype declaration)

● Like variables , all functions in a C program must be declared before they are
invoked. It is also known as prototype declaration.
● Syntax:

return_type functionname(parameter list);

int max(int num1, int num2);


Points to note
● The parameter list must be separated by commas.
● The parameter names do not need to be the same in the prototype declaration and
the function definition.
● The types must match the types of parameters in the function,in number and
order.
● Use of parameter names in declaration is optional.
● If the function has no formal parameters,the list is written as void.
● The return type is optional , when the function returns int type data.
● The return type must be void if no value is returned.
● When the declared types do not match with the types in the function definitions ,
compiler will produce a error.
Does prototype declaration is mandatory?

● Yes
● No
Find the given declarations are valid or not

1. int max(int a,int b , int c);


2. int MAX123(int a,b,c);
3. int 123max(int a, int b ,int c);
4. max(int a, int b);
5. int max(void);
6. int max();
7. int max(a,b,c);
Function definition / Function Implementation

The function definition consists of actual statements which are executed when the
function is called (i.e. when the program control comes to the function).
Syntax
return_type function_name (para1_type para1_name, para2_type para2_name)
{
// body of the function
}
Function Header

It consists of the following elements.

1. Function name : it should be a valid identifier and therefore must follow the
rule of defining an identifier.
2. Function type : it is the data type of a return value that a function is expected to
return.
3. List of parameters : it is also known as formal parameters. It servers as an input
value to the function.

Example : int sum(int num1, int num2){ …. }


Function Body
It consists of the following elements.
1. Local variable declarations : the variables that are declared in local
variable.
2. Function statements : the series of statements that required to be defined.
3. A return statement : the ans that is required to send back.
Example :
int sum( int num1, int num2)
{ int sum; //local variable declaration
sum=num1+num2; // statements
return sum; // return statement
}
Return value and their types

Syntax: we can return any one value or nothing.

1. return ;
2. return expression ;
Check the given declaration is valid or not

● return 5;
● return 5+2;
● return a;
● return (2.5*3.5);
Points to note

● When a function reaches its return statement , the control is transferred back
to the calling program.
● In the absence of a return statement, the closing braces act as a return
statement.
● A local variable is a variable that is defined in a function and used without
having any role in the communication between functions.
Function call
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.

It is a postfix expression

Formal arguments :When a function is defined with arguments, the arguments in front of
the function name are called formal arguments.

Actual arguments : When a function is called, the arguments passed to it are the actual
arguments.

Note : the formal and actual arguments must match exactly in type, order and
number. Their names however do not need to match.
C program to find the maximum number from two
numbers using function
#include <stdio.h> /* function returning the max between
/* function declaration */ two numbers */
int max(int num1, int num2); int max(int num1, int num2){
int main (){ /* local variable declaration */
/* local variable definition */ int result;
int a = 100, b = 200, ret; if (num1 > num2)
/* calling a function to get max result = num1;
value */ else
ret = max(a, b);
result = num2;
printf( "Max value is : %d\n", ret );
return result;
return 0;
} }
Check the following calling statements are valid of not

1. int result=max(50,20);
2. float result=max(50,20);
3. int result=max(5.0,2.0);
4. int result=max(5,2,3,4);
5. int result=max(5);
6. int result=max(5*2,10*2);
7. int result=max(5,add(5,2));

You might also like