Professional Documents
Culture Documents
CONTINUATION
FUNCTIONS
*Definition
• A function is a sub-program that performs a
specific task.
• A function is a set of statements that take
inputs, do some specific computation and
produces output.
• A function is a block of code which only runs
when it is called.
Types of functions in C programming
Statements;
-----------------
-----------------
return(expression);
}
• return_datatype - Denotes the datatype of the
result returned by the function.
• Eg: int ,float etc...
• function_name- represents the name of the
function. function name is an identifier and should
be unique
• type1 arg1, type2 arg2 ... - are the list of arguments
or variables separated by commas.
• There is no semi colon(;) after the closing
parenthesis.
• Local variable declaration - Are the variable
declaration within the function.
• Return statement- Is used to send back values from
the sub program or function to the main function.
*Local & Global variables
• Local Variables – The variables which are
declared within the function is known as local
variables.
• Global Variables – The variables which are
declared outside the function is called global
variables.
How user-defined function works?
#include <stdio.h>
main()
{
int a,b
functionName(); ----------------------Function Call(calling function)
}
Return_Datatype functionName() -------------------(called function)
{
int x,y ; local variables\return (exp);
-----
return(exp);
}
• The execution of a C program begins from
the main() function. When the compiler
encounters functionName(); inside the main
function, control of the program jumps to void
functionName() And, the compiler starts
executing the codes inside the user-defined
function.
• The control of the program jumps to statement
next to functionName(); once all the codes
inside the function definition are executed, it
will return the value to the calling function.
*Advantages of function
• The program will be easier to understand,
maintain and debug.
• Reusable codes that can be used in other
programs.
• A large program can be divided into smaller
modules. Hence, a large project can be divided
among many programmers.
Example: User-defined function
**Types Arguments/Parameters
datatype *pointervariable ;