Tip:
Highlight text to annotate it
X
now we want to understand functions
first we want to create
the prototype
that is the declaration
of the function
then the definition
and then the call to the function
so we will use pseudo code
to create this three fold step
in order to use functions
and grasp the idea of
declaring, defining and finally
calling the function
here we have the return type
for example: integer, character
floating point
we will see the types
later on
the function name
the parenthesis to start
the declaration of
the parameters
the parameter type
which is basically again
any type that the C programming language supports
then parameter a
which is just any variable name
comma, which separates one parameter
from the other
parameter type again
for the parameter b
in this occasion
so each type
and parameter couple
is separated by
a comma
you could avoid specifying the variable names
for the parameters, because really
the linker only checks
for the concordance of the types
of the prototype
against the function definition's
return and parameter types
I use the ...
to indicate that there can be
any number of parameters
depending on how much
RAM you have on your computer
but really it is
unlimited, in the theoretical sense
so parameter type and parameter name
are again used
for any number of parameters that you can pass
to the function, in this case we are simply declaring it
so that we can use this function
without being affraid of wrongfully
calling the function with missmatching variable types
we can group function prototypes
this is another function
it returns another type
and it is called another function name
firstly the return type is specified
then the function name
first parameter type
is specified for the parameter a
the parameter type for parameter b is also specified
and so on
so function prototypes go here
at the top of the file
if you are just using one source code file
and then at the bottom
bellow the main function
again if you are only using one source file
you define the function
by defining it, you are
simply specifying the code
to be executed
by the function when you call it
on the main function
or any other function
in this case you can notice
a striking similarity between
the declaration and the definition
there are miniscule differences
between the declaration and definition syntax
but we'll take a look at that
later on
for now bare in mind that the prototype
has to end with a ;
instead the definition's body will start
with a {
and end with a }
to end the function's code block
you can observe that we have the same
parameter types and parameter names list
as in the function prototypes
and then in order to specify
the code to be executed when the function is called
we have to create a code block
by opening a {
and then you see a series of statements
C statement 1; C statement 2
and so on
you can have any number of statements
that your program requires
up until the limits
of the target system
so we have any given number of statements
which can be variable declarations
variable definitions
loops, flow control statements
and so on
generally speaking
the arguments passed from
the call of the functions into the parameters
of the definition
are going to be used
inside these statements
to modify the values
and return a value
that is significant to the caller
statements are executed one after the other
up until the end of the code block
we close the }
the function code block is just like
any other code block
from other constructs
like the if code block
the loop code block
the definition code block
of structures
and so on
more details on that later
and we see that
we can create another function definition
for another_function_name
and it is very similar
again, the return type is specified
the function name is specified
parameter types and the parameter names
which in this case is parameter a
parameter b and so on
we open the curly bracket
again, C statement 1
C statement 2
you can have as many
statements as it is required
to compute your task in this function
the ellipsis because it is pseudo code
we can use them here
and we see that we close the curly bracket
to end the code block of the function
so function definitions go here
bellow the main function
when you are using a single source file
and pseudo code is used
we couldn't really compile
this pseudo code
it is not legitimate C syntax
and here at the main function we can
call the functions
that we defined bellow
and prototyped above
remember, the function must be called
within
the body of the main function
or any other function that is called
within main
or any other function that is called
within the function that is called
within main
and so on
we simply press "Enter"
and at the function call
where we want to call it
for that we see this syntax
function_name
you don't specify the types
when you are calling it
that's the difference
we don't need to do that
because the corroboration of types
is done at the linking phase
when the definition
is checked for the type matching
of the parameters
and the return value
with the declaration
in this case we call the function
only with the variable names
the types are implicit
they are called arguments
or actual parameters
so really what is being done here
is: another_function_name is being called
by the main function
although potentially
the caller could be just any other function
and the return type is being assigned
to variable_label
here is the list of arguments that will be passed
to the definition of the function
into the parameters
what we have at the end is parameter n
again representing the last argument being passed
to the n parameter in the definition
function calls go inside the main
and other functions as well
and it is encouraged to use a lot of function calls
to have modularity
in your code, which means
to divide and conquer
that is to have any code
that does something specific
encapsulated in a function
and functions can call other functions
which is called nesting functions
pseudo code has been used throughout
this video, in order
to explain the concepts behind
prototyping, defining and calling
a function
C lends itself for the functional programming paradigm.