C - Function
C - Function
Outline
What is C function?
Uses of C functions
C function declaration, function call and definition with example
program
How to call C functions in a program?
Call by value
Call by reference
C function arguments and return values
C function with arguments and with return value
C function with arguments and without return value
C function without arguments and without return value
C function without arguments and with return value
Types of C functions
Library functions in C
User defined functions in C
Creating/Adding user defined function in C library
a function body.
parts of a function
Here are all the parts of a function:
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.
Return Type: A function may return a value. The return
type is the data type of the value the function returns. If you
don’t want to return a result from a function, you can use void
return type. Some functions perform the desired operations
without returning a value. In this case, the return type is the
keyword void.
Function Body: The function body contains a collection of
statements that define what the function does.
Example: Function
Following is the source code for a function
called max(). This function takes two parameters
num1 and num2 and returns the maximum
between the two:
Function Declarations
A function declaration(function prototype) 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:
A C program with
function declaratio
n/function
prototype.
Function Arguments
C functions can accept an unlimited number of
parameters.
If a function is to use arguments, it must declare
variables that accept the values of the arguments.
These variables are called the formal
parameters of the function.
The formal parameters behave like other local
variables inside the function and are created upon
entry into the function and destroyed upon exit.
Global and local variables
Local variable:
A local variable is a variable that is declared inside
a function.
A local variable can only be used in the function
where it is declared.
Global variable:
A global variable is a variable that is declared
outside all functions.
A global variable can be used in all functions.
See the following example( see the next slide )
Global and local variables
As you can see
two global
variables are
declared, A and
B. These
variables can be
used in main()
and Add().
The local
variable answer
can only be used
in main().
Type of Function call
Output:
Output: