function in c
function in c
Main program
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
● Like variables , all functions in a C program must be declared before they are
invoked. It is also known as prototype declaration.
● Syntax:
● Yes
● No
Find the given declarations are valid or not
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
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.
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));