0% found this document useful (0 votes)
24 views

Functions

Functions allow programmers to organize code into reusable blocks of code that perform specific tasks. There are three key parts to a function: the return type, function name, and parameters. Functions are defined with a return type, name, parameters, and function body. Functions can be called to execute their code from other parts of the program. Parameters allow data to be passed into functions and the function body specifies the code or task to be performed. Functions provide modularity and code reuse in programs.

Uploaded by

einsteinriyad223
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Functions

Functions allow programmers to organize code into reusable blocks of code that perform specific tasks. There are three key parts to a function: the return type, function name, and parameters. Functions are defined with a return type, name, parameters, and function body. Functions can be called to execute their code from other parts of the program. Parameters allow data to be passed into functions and the function body specifies the code or task to be performed. Functions provide modularity and code reuse in programs.

Uploaded by

einsteinriyad223
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Functions

C - Functions
• A function is a group of statements that together perform a task.
Every C program has at least one function, which is main(), and all the
most trivial programs can define additional functions.
• You can divide up your code into separate functions. How you divide
up your code among different functions is up to you, but logically the
division is such that each function performs a specific task.
• A function declaration tells the compiler about a function's name,
return type, and parameters. A function definition provides the actual
body of the function.
Defining a Function
The general form of a function definition in C programming language is
as follows −
return_type function_name( parameter list ) {
body of the function
}
Function element
• Return Type − A function may return a value. The return_type is the data
type of the value the function returns. Some functions perform the desired
operations without returning a value. In this case, the return_type is the
keyword void.
• Function Name − This is the actual name of the function. The function name
and the parameter list together constitute the function signature.
• Parameters − A parameter is like a placeholder. When a function is invoked,
you pass a value to the parameter. This value is referred to as actual
parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is, a
function may contain no parameters.
• Function Body − The function body contains a collection of statements that
define what the function does.
Example
Given below is the source code for a function called max(). This
function takes two parameters num1 and num2 and returns the
maximum value between the two −
Function Declarations
A function declaration tells the compiler about a function name and
how to call the function. The actual body of the function can be defined
separately.
A function declaration has the following parts −
return_type function_name( parameter list );
int max(int num1, int num2);
Calling a Function
While creating a C function, you give a definition of what the function
has to do. To use a function, you will have to call that function to
perform the defined task.
When a program calls a function, the program control is transferred to
the called function. A called function performs a defined task and when
its return statement is executed or when its function-ending closing
brace is reached, it returns the program control back to the main
program.
To call a function, you simply need to pass the required parameters
along with the function name, and if the function returns a value, then
you can store the returned value.
Example
Function Arguments
• If a function is to use arguments, it must declare variables that accept
the values of the arguments. These variables are called the formal
parameters of the function.
• Formal parameters behave like other local variables inside the
function and are created upon entry into the function and destroyed
upon exit.
Call Type & Description
• Call by Value
This method copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the parameter
inside the function have no effect on the argument.
• Call by reference
This method copies the address of an argument into the formal
parameter. Inside the function, the address is used to access the actual
argument used in the call. This means that changes made to the
parameter affect the argument.
By default, C uses call by value to pass arguments.
Function call by Value in C
The call by value method of passing arguments to a function copies the
actual value of an argument into the formal parameter of the function.
In this case, changes made to the parameter inside the function have
no effect on the argument.
Function call by reference in C
The call by reference method of passing arguments to a function copies
the address of an argument into the formal parameter. Inside the
function, the address is used to access the actual argument used in the
call. It means the changes made to the parameter affect the passed
argument.
Example
Local Variables
Variables that are declared inside a function or block are called local
variables. They can be used only by statements that are inside that
function or block of code. Local variables are not known to functions
outside their own. The following example shows how local variables are
used. Here all the variables a, b, and c are local to main() function.
Example
Global Variables
• Global variables are defined outside a function, usually on top of the
program. Global variables hold their values throughout the lifetime of
your program and they can be accessed inside any of the functions
defined for the program.
• A global variable can be accessed by any function. That is, a global
variable is available for use throughout your entire program after its
declaration.
Example

You might also like