C functions can be defined separately from objects, require function prototypes, and always pass parameters by copy rather than by reference. Function definitions specify the return type, name, and parameter list. Parameters are local variables initialized by copying the argument values. To modify arguments, pointers must be used. Header files contain common function prototypes and declarations to allow separate compilation. Scope rules determine where variables and functions can be accessed. Macros allow text substitution before compilation. Optional parameters are indicated by an ellipsis and accessed using stdarg.h functions.
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
Modular programming involves breaking down a program into individual components (modules) that can be programmed and tested independently. Functions are used to implement modules in C++. Functions must be declared before use so the compiler knows their name, return type, and parameters. Functions are then defined by providing the body of code. Variables used within a function have local scope while variables declared outside have global scope. Functions can pass arguments either by value, where a copy is passed, or by reference, where the address is passed allowing the argument to be modified. Arrays and strings passed to functions are passed by reference as pointers.
The document discusses functions in C++. It covers standard and user-defined functions, value-returning and void functions, formal and actual parameters, and how to define, declare, and call functions. It also discusses scope of identifiers, value vs reference parameters, function overloading, and functions with default parameters.
The document discusses functions in C programming. It covers function definitions, prototypes, parameters, return types, scope rules, recursion, and examples. Functions allow dividing a program into smaller modular pieces to make it more manageable. Key points include defining functions with a return type, name, and parameters; using function prototypes for validation; and recursively calling functions to solve problems by breaking them down into base cases.
Functions are reusable blocks of code that perform specific tasks. There are two types of functions: standard library functions and user-defined functions. A function consists of a declaration, definition, and call. Functions can take parameters and return values. Parameters can be passed by value or by reference. Functions can be recursive or non-recursive. Arrays can also be passed to functions by passing individual elements or the whole array.
The document discusses storage classes and functions in C/C++. It explains the four storage classes - automatic, external, static, and register - and what keyword is used for each. It provides examples of how to declare variables of each storage class. The document also discusses different types of functions like library functions, user-defined functions, function declaration, definition, categories based on arguments and return values, actual and formal arguments, default arguments, and recursion.
Functions allow programmers to organize code into reusable blocks. A function is defined using the def keyword and can accept parameters. The body of a function contains a set of statements that run when the function is called. Functions can return values and allow code to be reused, reducing errors and improving readability. Parameters allow information to be passed into functions, while return values allow functions to provide results.
This presentation of ROBO INDIA comprises all of the elements that must be known to learn the programming language C.
This ppt also explains all these topics in details.
We welcome all you views and queries. Please write us, we are found at-
website: http://roboindia.com
mail: info@roboindia.com
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
Modular programming involves breaking down a program into individual components (modules) that can be programmed and tested independently. Functions are used to implement modules in C++. Functions must be declared before use so the compiler knows their name, return type, and parameters. Functions are then defined by providing the body of code. Variables used within a function have local scope while variables declared outside have global scope. Functions can pass arguments either by value, where a copy is passed, or by reference, where the address is passed allowing the argument to be modified. Arrays and strings passed to functions are passed by reference as pointers.
The document discusses functions in C++. It covers standard and user-defined functions, value-returning and void functions, formal and actual parameters, and how to define, declare, and call functions. It also discusses scope of identifiers, value vs reference parameters, function overloading, and functions with default parameters.
The document discusses functions in C programming. It covers function definitions, prototypes, parameters, return types, scope rules, recursion, and examples. Functions allow dividing a program into smaller modular pieces to make it more manageable. Key points include defining functions with a return type, name, and parameters; using function prototypes for validation; and recursively calling functions to solve problems by breaking them down into base cases.
Functions are reusable blocks of code that perform specific tasks. There are two types of functions: standard library functions and user-defined functions. A function consists of a declaration, definition, and call. Functions can take parameters and return values. Parameters can be passed by value or by reference. Functions can be recursive or non-recursive. Arrays can also be passed to functions by passing individual elements or the whole array.
The document discusses storage classes and functions in C/C++. It explains the four storage classes - automatic, external, static, and register - and what keyword is used for each. It provides examples of how to declare variables of each storage class. The document also discusses different types of functions like library functions, user-defined functions, function declaration, definition, categories based on arguments and return values, actual and formal arguments, default arguments, and recursion.
Functions allow programmers to organize code into reusable blocks. A function is defined using the def keyword and can accept parameters. The body of a function contains a set of statements that run when the function is called. Functions can return values and allow code to be reused, reducing errors and improving readability. Parameters allow information to be passed into functions, while return values allow functions to provide results.
This presentation of ROBO INDIA comprises all of the elements that must be known to learn the programming language C.
This ppt also explains all these topics in details.
We welcome all you views and queries. Please write us, we are found at-
website: http://roboindia.com
mail: info@roboindia.com
A storage class in C defines the scope, visibility, and lifetime of variables and functions. There are four main storage classes: automatic, register, static, and extern. Automatic variables are local to the function they are declared in and are destroyed when the function exits. Static variables persist for the duration of the program, while extern variables can be accessed from any file. Register variables may be stored in processor registers for faster access.
This document discusses functions in C programming. It defines a function as a self-contained block of statements that performs a specific task. Functions have a unique name, receive values from the calling program, may return a value, and are independent and reusable. There are two types of functions: predefined/standard library functions and user-defined functions. The document outlines the advantages of using functions and modular design. It also explains function declarations, definitions, parameters, scope, and how to define and call user-defined functions in C using both call-by-value and call-by-reference parameter passing.
C++ basics include object-oriented programming concepts like encapsulation, inheritance and polymorphism. Functions can be overloaded in C++ by having the same name but different parameters. Variables have scope depending on whether they are local, global or block variables. Inline functions avoid function call overhead by copying the function code directly into the calling code. Recursion allows functions to call themselves, which can be useful for computing things like factorials.
Multidimensional arrays store data in tabular form with multiple indices. A 3D array declaration would be datatype arrayName[size1][size2][size3]. Elements can be initialized and accessed similar to 2D arrays but with additional nested brackets and loops for each dimension. Functions allow dividing a problem into smaller logical parts. Functions are defined with a return type, name, parameters and body. Arguments are passed by value or reference and arrays can also be passed to functions. Recursion occurs when a function calls itself, requiring a base case to terminate the recursion.
The document discusses functions in C programming. The key points are:
1. A function is a block of code that performs a specific task. Functions allow code reusability and modularity.
2. main() is the starting point of a C program where execution begins. User-defined functions are called from main() or other functions.
3. Functions can take arguments and return values. There are different ways functions can be defined based on these criteria.
4. Variables used within a function have local scope while global variables can be accessed from anywhere. Pointers allow passing arguments by reference.
This document discusses functions and modular programming in C++. It defines what a function is and explains that functions allow dividing code into separate and reusable tasks. It covers function declarations, definitions, parameters, return types, and calling functions. It also discusses different ways of passing arguments to functions: call by value, call by pointer, and call by reference. Finally, it provides an example program that calculates addition and subtraction using different functions called within the main function. Modular programming is also summarized as dividing a program into independent and reusable modules to reduce complexity, decrease duplication, improve collaboration and testing.
This document provides information about a C++ course for the second semester. It includes the course name "B Sc IT", subject name "C++", and semester "II".
A function provides a convenient way of packaging a computational recipe, so that it can be used as often as required. A function definition consists of two parts: interface and body. The interface of a function (also called its prototype) specifies how it may be used. It consists of three entities:
The function name. This is simply a unique identifier.
The function parameters (also called its signature). This is a set of zero or more typed identifiers used for passing values to and from the function.
The function return type. This specifies the type of value the function returns. A function which returns nothing should have the return type void.
The body of a function contains the computational steps (statements) that comprise the function.
This document provides an overview of functions in C++. It defines what a function is, how to declare and define functions, how to call functions, and the differences between passing arguments by value versus by reference. A function is a block of code that performs a specific task. Functions are declared with a return type and parameter list, and defined with a body of code. Arguments can be passed into functions either by value, where the function receives a copy of the argument, or by reference, where any changes to the argument are reflected in the original variable. Well-designed programs use modular functions to organize code into reusable components.
This document discusses functions in C programming. It defines functions as a group of statements that perform a specific task and have a name. Main functions must be included in every C program as it is where program execution begins. Functions help facilitate modular programming by dividing programs into smaller parts. Functions can be user-defined or built-in library functions. Parameters can be passed to functions by value or by reference. Functions can call themselves through recursion. Variables have different storage classes like auto, register, static, and external that determine scope and lifetime.
A flowchart is a pictorial representation of a program algorithm, which is the step-by-step process to execute a program. The pre-processor directive processes source code before compilation. Variables store data values like numbers and characters. C language was developed by Dennis Ritchie in 1972. Functions can be user-defined or library functions. Storage classes determine lifetime, visibility, location, and initial values of variables, including automatic, static, register, and external.
The document provides information about preparing for a technical interview, including:
- Check the company profile, areas of interest, and domain to understand what may be asked.
- Be prepared to discuss your fundamental computer engineering knowledge as well as one or two subjects you are interested in and knowledgeable about.
- Also prepare to discuss your past project work and the latest technologies.
- Key topics that may be covered include C, C++, data structures, algorithms, databases, operating systems, networks, and various programming languages.
- Understanding pointers, memory allocation functions, structures, unions, classes, inheritance and polymorphism in C/C++ is also important for technical interviews.
The document discusses pointers in C programming. It defines a pointer as a variable that stores the memory address of another variable rather than the actual data. Pointers can be used to return multiple values from a function, pass arrays and strings to functions, and dynamically allocate and access memory. The key pointer operators are the address of (&) operator, which returns the address of its operand, and the dereferencing (*) operator, which refers to the object a pointer points to. Pointers can be passed to functions by value or by reference. The document also discusses structures, which allow grouping of different data types, and arrays, which can be passed to functions where only the array address is passed.
The document provides an overview of key concepts in the C programming language, including:
- Data types, variables, constants, and arrays. Arrays must be declared before use with the format data-type variable-name[size]. Two dimensional arrays are supported.
- Storage classes like automatic, external/global, static, and register that determine variable scope, lifetime, and memory location.
- Functions and different ways they can be called in C - call by value where copies of arguments are passed, and call by reference where addresses of variables are passed.
This document discusses functions in Python. It defines functions as collections of statements that perform specific tasks. There are three types of functions: built-in functions, module functions, and user-defined functions. Built-in functions are predefined in Python, module functions are contained in .py files, and user-defined functions are created by the user. The document provides examples of various types of functions and how they can be called and used.
This document discusses functions in programming. It defines a function as a block of code that performs a task and can be broken into two categories: value-returning functions and void functions. The components of a function are the header, which specifies the return type, name, and parameters, and the body, which contains the code to perform the task. Functions are called by passing actual parameters, which can be variables or literals, and formal parameters in the header store the passed information. The scope and lifetime of variables are also covered.
Overloaded functions allow multiple functions with the same name but different parameter types. This improves readability and handles incorrect arguments. The document provides examples of overloaded averaging and calculator functions. Pass by reference passes a variable's address rather than value, allowing the original variable to be modified. Scope defines variable visibility - variables are destroyed when exiting a scope unless declared static or global. Practice questions demonstrate overloaded functions, pass by reference, scopes, and static/global variables.
1. The document discusses functions in Python including types of functions, arguments, parameters, scope of variables, and returning values from functions.
2. Functions allow you to organize and reuse code, and in Python are defined using the def keyword. Arguments pass information into a function as variables called parameters.
3. Variables can have local or global scope depending on whether they are defined inside or outside of a function. The global keyword is used to read or write global variables inside a function.
A storage class in C defines the scope, visibility, and lifetime of variables and functions. There are four main storage classes: automatic, register, static, and extern. Automatic variables are local to the function they are declared in and are destroyed when the function exits. Static variables persist for the duration of the program, while extern variables can be accessed from any file. Register variables may be stored in processor registers for faster access.
This document discusses functions in C programming. It defines a function as a self-contained block of statements that performs a specific task. Functions have a unique name, receive values from the calling program, may return a value, and are independent and reusable. There are two types of functions: predefined/standard library functions and user-defined functions. The document outlines the advantages of using functions and modular design. It also explains function declarations, definitions, parameters, scope, and how to define and call user-defined functions in C using both call-by-value and call-by-reference parameter passing.
C++ basics include object-oriented programming concepts like encapsulation, inheritance and polymorphism. Functions can be overloaded in C++ by having the same name but different parameters. Variables have scope depending on whether they are local, global or block variables. Inline functions avoid function call overhead by copying the function code directly into the calling code. Recursion allows functions to call themselves, which can be useful for computing things like factorials.
Multidimensional arrays store data in tabular form with multiple indices. A 3D array declaration would be datatype arrayName[size1][size2][size3]. Elements can be initialized and accessed similar to 2D arrays but with additional nested brackets and loops for each dimension. Functions allow dividing a problem into smaller logical parts. Functions are defined with a return type, name, parameters and body. Arguments are passed by value or reference and arrays can also be passed to functions. Recursion occurs when a function calls itself, requiring a base case to terminate the recursion.
The document discusses functions in C programming. The key points are:
1. A function is a block of code that performs a specific task. Functions allow code reusability and modularity.
2. main() is the starting point of a C program where execution begins. User-defined functions are called from main() or other functions.
3. Functions can take arguments and return values. There are different ways functions can be defined based on these criteria.
4. Variables used within a function have local scope while global variables can be accessed from anywhere. Pointers allow passing arguments by reference.
This document discusses functions and modular programming in C++. It defines what a function is and explains that functions allow dividing code into separate and reusable tasks. It covers function declarations, definitions, parameters, return types, and calling functions. It also discusses different ways of passing arguments to functions: call by value, call by pointer, and call by reference. Finally, it provides an example program that calculates addition and subtraction using different functions called within the main function. Modular programming is also summarized as dividing a program into independent and reusable modules to reduce complexity, decrease duplication, improve collaboration and testing.
This document provides information about a C++ course for the second semester. It includes the course name "B Sc IT", subject name "C++", and semester "II".
A function provides a convenient way of packaging a computational recipe, so that it can be used as often as required. A function definition consists of two parts: interface and body. The interface of a function (also called its prototype) specifies how it may be used. It consists of three entities:
The function name. This is simply a unique identifier.
The function parameters (also called its signature). This is a set of zero or more typed identifiers used for passing values to and from the function.
The function return type. This specifies the type of value the function returns. A function which returns nothing should have the return type void.
The body of a function contains the computational steps (statements) that comprise the function.
This document provides an overview of functions in C++. It defines what a function is, how to declare and define functions, how to call functions, and the differences between passing arguments by value versus by reference. A function is a block of code that performs a specific task. Functions are declared with a return type and parameter list, and defined with a body of code. Arguments can be passed into functions either by value, where the function receives a copy of the argument, or by reference, where any changes to the argument are reflected in the original variable. Well-designed programs use modular functions to organize code into reusable components.
This document discusses functions in C programming. It defines functions as a group of statements that perform a specific task and have a name. Main functions must be included in every C program as it is where program execution begins. Functions help facilitate modular programming by dividing programs into smaller parts. Functions can be user-defined or built-in library functions. Parameters can be passed to functions by value or by reference. Functions can call themselves through recursion. Variables have different storage classes like auto, register, static, and external that determine scope and lifetime.
A flowchart is a pictorial representation of a program algorithm, which is the step-by-step process to execute a program. The pre-processor directive processes source code before compilation. Variables store data values like numbers and characters. C language was developed by Dennis Ritchie in 1972. Functions can be user-defined or library functions. Storage classes determine lifetime, visibility, location, and initial values of variables, including automatic, static, register, and external.
The document provides information about preparing for a technical interview, including:
- Check the company profile, areas of interest, and domain to understand what may be asked.
- Be prepared to discuss your fundamental computer engineering knowledge as well as one or two subjects you are interested in and knowledgeable about.
- Also prepare to discuss your past project work and the latest technologies.
- Key topics that may be covered include C, C++, data structures, algorithms, databases, operating systems, networks, and various programming languages.
- Understanding pointers, memory allocation functions, structures, unions, classes, inheritance and polymorphism in C/C++ is also important for technical interviews.
The document discusses pointers in C programming. It defines a pointer as a variable that stores the memory address of another variable rather than the actual data. Pointers can be used to return multiple values from a function, pass arrays and strings to functions, and dynamically allocate and access memory. The key pointer operators are the address of (&) operator, which returns the address of its operand, and the dereferencing (*) operator, which refers to the object a pointer points to. Pointers can be passed to functions by value or by reference. The document also discusses structures, which allow grouping of different data types, and arrays, which can be passed to functions where only the array address is passed.
The document provides an overview of key concepts in the C programming language, including:
- Data types, variables, constants, and arrays. Arrays must be declared before use with the format data-type variable-name[size]. Two dimensional arrays are supported.
- Storage classes like automatic, external/global, static, and register that determine variable scope, lifetime, and memory location.
- Functions and different ways they can be called in C - call by value where copies of arguments are passed, and call by reference where addresses of variables are passed.
This document discusses functions in Python. It defines functions as collections of statements that perform specific tasks. There are three types of functions: built-in functions, module functions, and user-defined functions. Built-in functions are predefined in Python, module functions are contained in .py files, and user-defined functions are created by the user. The document provides examples of various types of functions and how they can be called and used.
This document discusses functions in programming. It defines a function as a block of code that performs a task and can be broken into two categories: value-returning functions and void functions. The components of a function are the header, which specifies the return type, name, and parameters, and the body, which contains the code to perform the task. Functions are called by passing actual parameters, which can be variables or literals, and formal parameters in the header store the passed information. The scope and lifetime of variables are also covered.
Overloaded functions allow multiple functions with the same name but different parameter types. This improves readability and handles incorrect arguments. The document provides examples of overloaded averaging and calculator functions. Pass by reference passes a variable's address rather than value, allowing the original variable to be modified. Scope defines variable visibility - variables are destroyed when exiting a scope unless declared static or global. Practice questions demonstrate overloaded functions, pass by reference, scopes, and static/global variables.
1. The document discusses functions in Python including types of functions, arguments, parameters, scope of variables, and returning values from functions.
2. Functions allow you to organize and reuse code, and in Python are defined using the def keyword. Arguments pass information into a function as variables called parameters.
3. Variables can have local or global scope depending on whether they are defined inside or outside of a function. The global keyword is used to read or write global variables inside a function.
Literature Review Basics and Understanding Reference Management.pptxDr Ramhari Poudyal
Three-day training on academic research focuses on analytical tools at United Technical College, supported by the University Grant Commission, Nepal. 24-26 May 2024
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSIJNSA Journal
The smart irrigation system represents an innovative approach to optimize water usage in agricultural and landscaping practices. The integration of cutting-edge technologies, including sensors, actuators, and data analysis, empowers this system to provide accurate monitoring and control of irrigation processes by leveraging real-time environmental conditions. The main objective of a smart irrigation system is to optimize water efficiency, minimize expenses, and foster the adoption of sustainable water management methods. This paper conducts a systematic risk assessment by exploring the key components/assets and their functionalities in the smart irrigation system. The crucial role of sensors in gathering data on soil moisture, weather patterns, and plant well-being is emphasized in this system. These sensors enable intelligent decision-making in irrigation scheduling and water distribution, leading to enhanced water efficiency and sustainable water management practices. Actuators enable automated control of irrigation devices, ensuring precise and targeted water delivery to plants. Additionally, the paper addresses the potential threat and vulnerabilities associated with smart irrigation systems. It discusses limitations of the system, such as power constraints and computational capabilities, and calculates the potential security risks. The paper suggests possible risk treatment methods for effective secure system operation. In conclusion, the paper emphasizes the significant benefits of implementing smart irrigation systems, including improved water conservation, increased crop yield, and reduced environmental impact. Additionally, based on the security analysis conducted, the paper recommends the implementation of countermeasures and security approaches to address vulnerabilities and ensure the integrity and reliability of the system. By incorporating these measures, smart irrigation technology can revolutionize water management practices in agriculture, promoting sustainability, resource efficiency, and safeguarding against potential security threats.
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...University of Maribor
Slides from talk presenting:
Aleš Zamuda: Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapter and Networking.
Presentation at IcETRAN 2024 session:
"Inter-Society Networking Panel GRSS/MTT-S/CIS
Panel Session: Promoting Connection and Cooperation"
IEEE Slovenia GRSS
IEEE Serbia and Montenegro MTT-S
IEEE Slovenia CIS
11TH INTERNATIONAL CONFERENCE ON ELECTRICAL, ELECTRONIC AND COMPUTING ENGINEERING
3-6 June 2024, Niš, Serbia
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesChristina Lin
Traditionally, dealing with real-time data pipelines has involved significant overhead, even for straightforward tasks like data transformation or masking. However, in this talk, we’ll venture into the dynamic realm of WebAssembly (WASM) and discover how it can revolutionize the creation of stateless streaming pipelines within a Kafka (Redpanda) broker. These pipelines are adept at managing low-latency, high-data-volume scenarios.
We have compiled the most important slides from each speaker's presentation. This year’s compilation, available for free, captures the key insights and contributions shared during the DfMAy 2024 conference.
International Conference on NLP, Artificial Intelligence, Machine Learning an...gerogepatton
International Conference on NLP, Artificial Intelligence, Machine Learning and Applications (NLAIM 2024) offers a premier global platform for exchanging insights and findings in the theory, methodology, and applications of NLP, Artificial Intelligence, Machine Learning, and their applications. The conference seeks substantial contributions across all key domains of NLP, Artificial Intelligence, Machine Learning, and their practical applications, aiming to foster both theoretical advancements and real-world implementations. With a focus on facilitating collaboration between researchers and practitioners from academia and industry, the conference serves as a nexus for sharing the latest developments in the field.
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTjpsjournal1
The rivalry between prominent international actors for dominance over Central Asia's hydrocarbon
reserves and the ancient silk trade route, along with China's diplomatic endeavours in the area, has been
referred to as the "New Great Game." This research centres on the power struggle, considering
geopolitical, geostrategic, and geoeconomic variables. Topics including trade, political hegemony, oil
politics, and conventional and nontraditional security are all explored and explained by the researcher.
Using Mackinder's Heartland, Spykman Rimland, and Hegemonic Stability theories, examines China's role
in Central Asia. This study adheres to the empirical epistemological method and has taken care of
objectivity. This study analyze primary and secondary research documents critically to elaborate role of
china’s geo economic outreach in central Asian countries and its future prospect. China is thriving in trade,
pipeline politics, and winning states, according to this study, thanks to important instruments like the
Shanghai Cooperation Organisation and the Belt and Road Economic Initiative. According to this study,
China is seeing significant success in commerce, pipeline politics, and gaining influence on other
governments. This success may be attributed to the effective utilisation of key tools such as the Shanghai
Cooperation Organisation and the Belt and Road Economic Initiative.
Technical Drawings introduction to drawing of prisms
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