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

Functions C

Uploaded by

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

Functions C

Uploaded by

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

Functions

C
What is A Function?
• Function is a group of statement that together
perform a specific task enclosed within curly
brackets ({})
• We divide a code into separate functions
• Every program have at least one function,
example main() For execution of the program
• Thus,collection of functions creates a program
• Functions usually take in data, process it and
return a result
• It can be used over, over and over again
Advantages of function

• Avoid repetition of code


• Increase program readability
• Divide complex problem into simpler
segments
• Reduce chance of error
• Modifying program becomes easier
Types of functions
• Inbuilt
• Library provide numerous inbuilt functions.
• This means that we do not have to write a definition or the
function’s body to call them. We can simply call them without
defining them as they are already defined.
• However, we need to include the library at the beginning of the
code for calling a library function. We can then use the proper
syntax of the function to call them.
• Example :
• Printf(), scanf(), strcat and floor()
Header Files
• all library functions are included in different
header files saved with a .h extension.
• To use any library functions, weneed to use
the header files at the beginning of the
program.
• Without including the header files, the
program will not be executed as the compiler
will throw errors.
User defined
• These are the functions that a developer or
the user declares, defines, and calls in a
program.
• This increases the scope and functionality, and
reusability of C programming as we can define
and use any function we want.
Syntax
• return_type: Here, we declare the data type of the value
returned by functions. However, not all functions return a
value. In such cases, the keyword void indicates to the
compiler that the function will not return any value.
• function_name: This is the function’s name that helps the
compiler identify it whenever we call it.
• arg1, arg2, ...argn: It is the argument or parameter list that
contains all the parameters to be passed into the function. The
list defines the data type, sequence, and the number of
parameters to be passed to the function. A function may or
may not require parameters. Hence, the parameter list is
optional.
• Body: The function’s body contains all the statements to be
processed and executed whenever the function is called.
Function Declaration
• A function prototype is simply the declaration of a
function that specifies function's name, parameters
and return type. It doesn't contain function body.
• A function prototype gives information to the
compiler that the function may later be used in the
program.
• A function must be declared globally
• Syntax of function prototype
• returnType functionName(type1 argument1, type2
argument2, ...);
Function Call
• Control of the program is transferred to the user-
defined function by calling it.
• Syntax of function call
functionName(argument1, argument2, ...);
 we can call the function at any point in the entire
program.
 The only thing to take care of is that we need to
pass as many arguments of the same data type as
mentioned while declaring the function
Function Defination
• Function definition contains the block of code to
perform a specific task.
• Syntax of function definition
returnType functionName(type1 argument1, type2
argument2, ...) {
//body of the function
}
• When a function is called, the control of the program is
transferred to the function definition. And, the compiler
starts executing the codes inside the body of a function.
A method returns to the code that invoked it
when:
• It completes all the statements in the method
• It reaches a return statement
• Throws an exception
Return Statement

• The return statement terminates the


execution of a function and returns a value to
the calling function. The program control is
transferred to the calling function after the
return statement.
• Make Calculator
• Thanks

You might also like