🔸Functions
the overal form is like this:
return_type function_name( parameters - separated by commas) { statements }
return_type function_name( parameters - separated by commas) { statements }
example:
Function arguments
example:
we can also asign a variable to the return of a function:
Returning data
return type is the first parameter that the function returns.it can be any data type like double, int, char, pointers, void, etc.
the return statement provides the means of exiting from a function:
example:
all variables defined inside a function are known as automatic local variables
Global variables
the opposite of a local variable, defined outside of a function. the syntax is the same, global variables are just defined outside of all functions. if you want to make a variable global bot inaccessible from other C files in the program, use the 'restrict' type qualifier explained in type qualifiers section.
example:
avoid using global variables, it might get very hard to keep track of global variables specially when the program grows.
Utility functions
here are some of the useful utility functions used in C programming language.
Variadic function
the variation or change here is that we are dealing with unknown number of arguments that will be used for a function.several Cs built-in functions are variatic functions like printf().
a variatic function has 2 parts: mandatory args (at least 1 is required and is the first one listed) optional args (listed after mandatory args)
<stdarg.h> must be included
va_list
used when we need to access optional parameters and it is an arg list. represents a data object used to hold the parameters for the ellipsis part of the parameter list
va_start
will connect our argument list with some argument pointer, the list specified in va_list is the first arg and the second arg is the last fixed parameter
va_arg
returns the value of the current argument. will fetch the current argument connected to the argument list. we would need to know the type of the argument that we are reading
va_end
used in situation when we would like to stop using a variable argument list (cleanup)
va_copy
used in situations for which we need to save our current location
Creating a variadic function
provide a function prototype using a ellipsis(three dots), this indicates that a variable number of args may follow any number of fixed args, must have at least one fixed argument.
ellipsis should always be last, the function must have at least one mandatory arg
create a va_list type variable in the function definition. initialize the var to an arg list.need to copy the arg list to the va_list variable using va_start
access the content of the arg list using va_arg() takes 2 args: a type va_list var and a type name the first time it is called it returns the first item in the list the next time it is called it returns the second item in the list and so on
the automatic conversion of double to int that works for assignment doesn't work here
you should clean by using the va_end macro as your last step.resets the pointer to NULL.
example take at least 2 numbers and print their sum:
va_copy
its possible that we may need to process a var arg list more than once. may be useful to preserve a copy of the va_list type var.
the first statement creates a new va_list variable, parg_copy the next statement copies the content of parg to parg_copy
we can then process parg and parg_copy independently to extract argument values from the list prior to using the va_copy() routine, parg_copy will be in an identical state to parg with some arg values already extracted
do not use the va_list object parg_copy as the destination for another copy operation before you have executed va_end() for parg_copy
example:
compile this with gcc main.c -o main -lm
Recursion
when a function calls itself directly or indirectly
example:
Inline functions
helps avoiding the amount of overhead that comes along with invoking a function, the point is to hint the compiler that it is worth making some form of extra effort to call the function faster than it would otherwise.
when using inline functions, the compiler will substitute the code of the function into its caller (eliminating the need for a call and return sequence). the program no longer calls the code of the function, instead, the compiler replaces every call to an inline function with the code body of that function.
inline declaration is only advice to compiler which can be decided to be ignored. may cause the compiler to replace the function call with inline code and/or perform some other sort of optimizations.
Noreturn functions
c11 added a second function specifier named _Noreturn
the perpose of this is to inform the user and compiler that a particular function will not return control to the calling program when it completes execution.
just like inline function this is just a hint for the compiler which can be ignored.
the exit() function is and example of a _Noreturn function, once called, never resumes
example using noreturn macro:
if we have a multi-file program we need an inline definition in each file that calls the function.
Last updated