SlideShare a Scribd company logo
1 of 11
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

Similar to c-functions.ppt

Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
C language presentation
C language presentationC language presentation
C language presentation
bainspreet
 

Similar to c-functions.ppt (20)

STORAGE CLASS.pptx
STORAGE CLASS.pptxSTORAGE CLASS.pptx
STORAGE CLASS.pptx
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Functions
FunctionsFunctions
Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
106da session5 c++
106da session5 c++106da session5 c++
106da session5 c++
 
Function in C++
Function in C++Function in C++
Function in C++
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Terms and Definitions.pdf
Terms and Definitions.pdfTerms and Definitions.pdf
Terms and Definitions.pdf
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
C language presentation
C language presentationC language presentation
C language presentation
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
Function
FunctionFunction
Function
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 

Recently uploaded

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 

Recently uploaded (20)

WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 

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