SlideShare a Scribd company logo
C Functions
• Three major differences between C and Java functions:
– Functions are stand-alone entities, not part of objects
• they can be defined in a file with other functions, or separately and then
loaded as desired
– Function prototypes must appear in any file where functions
are to be used
• the prototype describes the function’s header (name, return type,
number and type of params) – although you can also place these in
header files for simplicity
– Parameters are always passed by copy
• this means that the value of the parameter is copied into the parameter
in the function’s header, and the parameter then becomes a local
variable
• no values are passed back from the function via the parameter list
• returning a single value is done by the function’s return
• returning multiple values must be done by passing pointers (or
returning a structure like an array)
Function Format
• If no return type, it defaults to int (not void)
– void is used if the function is to not return any value, otherwise at least one
return statement is expected
• Functions can be compiled separately
– So information about the function must be made known explicitly
• either declare the function, as you would a variable
– example: double afunction(int, int);
• or place a prototype prior to the function that uses it
– example: void anotherFunction(int x, int y);
• Note: prototypes do not need variable names, so it could also be specified as
– void anotherFunction(int, int);
– this makes the prototype look just like the declaration, the only difference is
placement – the prototype goes before the function that calls it, the declaration goes
with the var and const declarations inside the function that calls it
• the prototype approach is more common but either is acceptable
return-type name(param list)
{
var and const declarations
executable statements
return statement(s)
}
NOTE: in C, declarations must
precede executable statements, you
cannot mix them up as you do in Java
Pass By Copy
• This is the only form of parameter passing in C
– The value stored in the parameter in the function call is copied into the
parameter in the function header
• that is, the formal parameter is initialized with the value in the actual parameter
• from that point forward, the formal parameter is completely independent of the
actual parameter
• changing the formal parameter does nothing to the actual parameter’s value
– If you pass a pointer:
• then the formal parameter is a pointer, pointing at the same location as the
actual parameter
• by using the pointer, you can change the value being pointed to
• so changing the value that the pointer points to changes the actual parameter
• changing the formal parameter means that you are changing the pointer, or the
memory location being pointed at, not the value being pointed at, so changing
the formal parameter does nothing to the actual parameter and will now cause
your formal parameter to point somewhere else in memory
– this could lead to run-time and/or logical errors!
• Using parameters appropriately can be very tricky
– This is one of the biggest sources of logical errors in C programming
• but passing pointers can result in all kinds of problem – be very careful!
Example: Swapping
• As you are aware, swapping 2 values is a common
routine – for instance, when sorting
• Here, we look at the wrong way and the right way
to swap two values in a function
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
The wrong way to swap
When called with swap(x, y);
x and y will remain the same
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
The right way to swap
When called with swap(&x, &y); where x and y
are int values, then a points to x and b points
to y, so the values pointed to are swapped,
not the pointers themselves
extern
• Global variables are available within a file by declaring
variables prior to the functions
– Global variables are available between files using the reserved
word extern to define them
• The extern declaration appears in the files that are using the externally
declared variables, not in the file where they were originally declared
– So, if file 1 declares variables and file 2 wants to use those variables, you
use an #include statement to load file 1 and extern statements to declare any
of file 1’s global variables to be used in file 2
– Take care when using global variables, they are not encouraged
and lead to hard-to-read and hard-to-debug code
• In fact, I would recommend that you avoid them if at all possible!
• NOTE that extern defines an external variable, but does
not declare it, a separate declaration is required although
they can be combined into a single statement as in:
– extern int x;
Scope Rules
• Scope for a variable (or a function) are the locations
within a program where that variable (or function) can
be accessed
• There are generally two types of scope: local and global
– A local variable is defined within the local environment: the
current block or the current function
• Variables declared within { } are local to that block, whether the block
is a block within a function or the function itself
• Variables declared within a for-loop are local to that for-loop
• Parameters in a function header are local to that function
• Note that if two variables share the same name but are in different
blocks or functions, then the variable declared in this environment will
be the one used in a reference
– A global variable is defined outside of the local environment
and available anywhere within the file, or in other files by
using extern
Scope Example with Blocks
#include <stdio.h>
void func1(int, int);
void func2(int, int);
void main()
{
int x = 5, y = 10;
printf("Values before first function
call: %d %dn", x, y);
func1(x, y);
{
int x = 15, y = 20;
printf("Values inside
block: %d %dn", x, y);
func2(x, y);
}
printf("Values before after block:
%d %dn", x, y);
}
void func1(int x, int y)
{
x++; y--;
printf("Values inside func1:
%d %dn", x, y);
}
void func2(int x, int y)
{
x+=2; y-=2;
printf("Values inside func2:
%d %dn", x, y);
{
int x = 0, y = 1;
printf("Values inside func2's
block: %d %dn", x, y);
}
}
Header Files
• In order to call upon functions compiled in separate files,
you need to include their definition as a declaration or a
prototype
– for simplicity, if you have functions in several files, each of
which call upon some of the same functions, you can place the
prototypes in a single file, called a header file
• all other shared definitions and declarations can go here as well
• A header files typically only contain definitions and
declarations, not executable code
– consider as an example a calculator program that has its’
functions split into multiple files:
• a main function in one file which calls upon
• stack operations in a file stack.c
• a parsing operation to get tokens from a string in the file getop.c
• a function to get char input in the file getch.c
• a header file contains prototypes and common declarations called calc.h
Static and Register
• The reserved word
static is used to define
a variable as being
static, typically used
with a variable
defined using extern
– Static means that the
storage of that variable
remains in existence
• Local variables are
removed from memory
once the function
terminates, a static
variable remains in
memory so that it can
be accessed later
• The register reserved word is
used to suggest to the compiler
that a particular variable should
be moved to and kept in a
register
– The idea is to give the compiler
some advice
– The register declaration is only
permitted for local variables and
parameters and the advice is not
necessarily taken by the compiler
• Just because you place register in
front of a variable’s declaration
does not mean that the variable will
be placed into a register
Macro Substitution
• We saw #define can be used for constants
– Form: #define name substitution-text
• Example: #define MAX 10
– But unlike a constant in other languages as a variable whose
value does not change
• here the definition is a form of macro substitution
– all instances of the name are replaced by the value
• this can be used in some interesting ways, such as in the following:
– #define forever for( ; ; ) /* infinite loop */
– #define max(A, B) ((A) > (B) ? (A) : (B)) /* defines a max operation */
• #define is actually a preprocessing directive – that is, an
activity to be performed by the compiler prior to
attempting compilation
Optional Parameters
• C permits functions to have optional parameters
– The format is returntype name(params, …)
• That is, the … indicates that further parameters can be passed
• The … must be listed only after the required parameters
– Notice that, since you specify the parameters as …, you do not
know their names!
• How then can you use these additional parameters (if they were
passed)?
– The stdarg.h file contains the definition of va_list (variable
argument list) so that you can step through the optional
parameters
• Declare a variable of type va_list
• Use the macro va_start which initializes your variable to the first of the
optional params
• The function va_arg returns the next argument
– We will skip further coverage of this topic, but if you are
interested, consult your C textbook which should talk about it

More Related Content

PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPT
Chapter Introduction to Modular Programming.ppt
PPT
Functions
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PPTX
Functions
PPTX
Functions.pptx
PPTX
UNIT 3 python.pptx
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Chapter Introduction to Modular Programming.ppt
Functions
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
Functions
Functions.pptx
UNIT 3 python.pptx

Similar to c-functions.ppt (20)

PPTX
Storage Classes and Functions
PDF
Python functions
PPTX
C language
PPTX
STORAGE CLASS.pptx
PPTX
CH.4FUNCTIONS IN C (1).pptx
PPT
Basics of cpp
PPTX
_Python_ Functions _and_ Libraries_.pptx
PDF
3-Python Functions.pdf in simple.........
PPTX
PPTX
Functions in c
PPTX
Functions and modular programming.pptx
PPTX
ExamRevision_FinalSession_C++ notes.pptx
PPTX
Function Overloading Call by value and call by reference
PPT
106da session5 c++
PDF
OOP UNIT 1_removed ppt explaining object.pdf
PDF
Function in C++
PPTX
Chapter One Function.pptx
PPTX
FUNCTION CPU
PDF
Terms and Definitions.pdf
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
Storage Classes and Functions
Python functions
C language
STORAGE CLASS.pptx
CH.4FUNCTIONS IN C (1).pptx
Basics of cpp
_Python_ Functions _and_ Libraries_.pptx
3-Python Functions.pdf in simple.........
Functions in c
Functions and modular programming.pptx
ExamRevision_FinalSession_C++ notes.pptx
Function Overloading Call by value and call by reference
106da session5 c++
OOP UNIT 1_removed ppt explaining object.pdf
Function in C++
Chapter One Function.pptx
FUNCTION CPU
Terms and Definitions.pdf
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
Ad

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Big Data Technologies - Introduction.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Assigned Numbers - 2025 - Bluetooth® Document
NewMind AI Weekly Chronicles - August'25-Week II
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Big Data Technologies - Introduction.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
A Presentation on Artificial Intelligence
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
A comparative analysis of optical character recognition models for extracting...
Ad

c-functions.ppt

  • 1. C Functions • Three major differences between C and Java functions: – Functions are stand-alone entities, not part of objects • they can be defined in a file with other functions, or separately and then loaded as desired – Function prototypes must appear in any file where functions are to be used • the prototype describes the function’s header (name, return type, number and type of params) – although you can also place these in header files for simplicity – Parameters are always passed by copy • this means that the value of the parameter is copied into the parameter in the function’s header, and the parameter then becomes a local variable • no values are passed back from the function via the parameter list • returning a single value is done by the function’s return • returning multiple values must be done by passing pointers (or returning a structure like an array)
  • 2. Function Format • If no return type, it defaults to int (not void) – void is used if the function is to not return any value, otherwise at least one return statement is expected • Functions can be compiled separately – So information about the function must be made known explicitly • either declare the function, as you would a variable – example: double afunction(int, int); • or place a prototype prior to the function that uses it – example: void anotherFunction(int x, int y); • Note: prototypes do not need variable names, so it could also be specified as – void anotherFunction(int, int); – this makes the prototype look just like the declaration, the only difference is placement – the prototype goes before the function that calls it, the declaration goes with the var and const declarations inside the function that calls it • the prototype approach is more common but either is acceptable return-type name(param list) { var and const declarations executable statements return statement(s) } NOTE: in C, declarations must precede executable statements, you cannot mix them up as you do in Java
  • 3. Pass By Copy • This is the only form of parameter passing in C – The value stored in the parameter in the function call is copied into the parameter in the function header • that is, the formal parameter is initialized with the value in the actual parameter • from that point forward, the formal parameter is completely independent of the actual parameter • changing the formal parameter does nothing to the actual parameter’s value – If you pass a pointer: • then the formal parameter is a pointer, pointing at the same location as the actual parameter • by using the pointer, you can change the value being pointed to • so changing the value that the pointer points to changes the actual parameter • changing the formal parameter means that you are changing the pointer, or the memory location being pointed at, not the value being pointed at, so changing the formal parameter does nothing to the actual parameter and will now cause your formal parameter to point somewhere else in memory – this could lead to run-time and/or logical errors! • Using parameters appropriately can be very tricky – This is one of the biggest sources of logical errors in C programming • but passing pointers can result in all kinds of problem – be very careful!
  • 4. Example: Swapping • As you are aware, swapping 2 values is a common routine – for instance, when sorting • Here, we look at the wrong way and the right way to swap two values in a function void swap(int a, int b) { int temp = a; a = b; b = temp; } The wrong way to swap When called with swap(x, y); x and y will remain the same void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } The right way to swap When called with swap(&x, &y); where x and y are int values, then a points to x and b points to y, so the values pointed to are swapped, not the pointers themselves
  • 5. extern • Global variables are available within a file by declaring variables prior to the functions – Global variables are available between files using the reserved word extern to define them • The extern declaration appears in the files that are using the externally declared variables, not in the file where they were originally declared – So, if file 1 declares variables and file 2 wants to use those variables, you use an #include statement to load file 1 and extern statements to declare any of file 1’s global variables to be used in file 2 – Take care when using global variables, they are not encouraged and lead to hard-to-read and hard-to-debug code • In fact, I would recommend that you avoid them if at all possible! • NOTE that extern defines an external variable, but does not declare it, a separate declaration is required although they can be combined into a single statement as in: – extern int x;
  • 6. Scope Rules • Scope for a variable (or a function) are the locations within a program where that variable (or function) can be accessed • There are generally two types of scope: local and global – A local variable is defined within the local environment: the current block or the current function • Variables declared within { } are local to that block, whether the block is a block within a function or the function itself • Variables declared within a for-loop are local to that for-loop • Parameters in a function header are local to that function • Note that if two variables share the same name but are in different blocks or functions, then the variable declared in this environment will be the one used in a reference – A global variable is defined outside of the local environment and available anywhere within the file, or in other files by using extern
  • 7. Scope Example with Blocks #include <stdio.h> void func1(int, int); void func2(int, int); void main() { int x = 5, y = 10; printf("Values before first function call: %d %dn", x, y); func1(x, y); { int x = 15, y = 20; printf("Values inside block: %d %dn", x, y); func2(x, y); } printf("Values before after block: %d %dn", x, y); } void func1(int x, int y) { x++; y--; printf("Values inside func1: %d %dn", x, y); } void func2(int x, int y) { x+=2; y-=2; printf("Values inside func2: %d %dn", x, y); { int x = 0, y = 1; printf("Values inside func2's block: %d %dn", x, y); } }
  • 8. Header Files • In order to call upon functions compiled in separate files, you need to include their definition as a declaration or a prototype – for simplicity, if you have functions in several files, each of which call upon some of the same functions, you can place the prototypes in a single file, called a header file • all other shared definitions and declarations can go here as well • A header files typically only contain definitions and declarations, not executable code – consider as an example a calculator program that has its’ functions split into multiple files: • a main function in one file which calls upon • stack operations in a file stack.c • a parsing operation to get tokens from a string in the file getop.c • a function to get char input in the file getch.c • a header file contains prototypes and common declarations called calc.h
  • 9. Static and Register • The reserved word static is used to define a variable as being static, typically used with a variable defined using extern – Static means that the storage of that variable remains in existence • Local variables are removed from memory once the function terminates, a static variable remains in memory so that it can be accessed later • The register reserved word is used to suggest to the compiler that a particular variable should be moved to and kept in a register – The idea is to give the compiler some advice – The register declaration is only permitted for local variables and parameters and the advice is not necessarily taken by the compiler • Just because you place register in front of a variable’s declaration does not mean that the variable will be placed into a register
  • 10. Macro Substitution • We saw #define can be used for constants – Form: #define name substitution-text • Example: #define MAX 10 – But unlike a constant in other languages as a variable whose value does not change • here the definition is a form of macro substitution – all instances of the name are replaced by the value • this can be used in some interesting ways, such as in the following: – #define forever for( ; ; ) /* infinite loop */ – #define max(A, B) ((A) > (B) ? (A) : (B)) /* defines a max operation */ • #define is actually a preprocessing directive – that is, an activity to be performed by the compiler prior to attempting compilation
  • 11. Optional Parameters • C permits functions to have optional parameters – The format is returntype name(params, …) • That is, the … indicates that further parameters can be passed • The … must be listed only after the required parameters – Notice that, since you specify the parameters as …, you do not know their names! • How then can you use these additional parameters (if they were passed)? – The stdarg.h file contains the definition of va_list (variable argument list) so that you can step through the optional parameters • Declare a variable of type va_list • Use the macro va_start which initializes your variable to the first of the optional params • The function va_arg returns the next argument – We will skip further coverage of this topic, but if you are interested, consult your C textbook which should talk about it