The document discusses functions in C programming, including what functions are, different types of functions, function declaration, definition, and call. It covers topics like passing parameters to functions, local and global variables, and functions that return values or accept arguments.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
33 views30 pages
Chap13 (ICS 12)
The document discusses functions in C programming, including what functions are, different types of functions, function declaration, definition, and call. It covers topics like passing parameters to functions, local and global variables, and functions that return values or accept arguments.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30
COMPUTER SCIENCE
12 (MS Access and C)
CHAPTER 13: Functions in C
Copyright @ IT Series www.itseries.com.pk
Topics • Function • Types of Functions • Function Declaration • Function Definition • Difference between Function declaration and Definition • Function Call • Passing Parameters to Functions • Difference between Formal and Actual Parameters • Returning Value from Function • Local Variable • Global Variable • Difference between Local and Global Variable Copyright @ IT Series www.itseries.com.pk Topics(continued) • Functions without Arguments • Functions that Return a Value and Accept Arguments
Copyright @ IT Series www.itseries.com.pk
A function is named block of code that performs a specific task or action Functions are like building blocks A program may contain many functions Each function has a unique name The statements written in a function are executed when it is called by its name possible to perform mathematical operation on character values Functions provide structured programming approach Breaking a large program into smaller pieces or modules Modules in C are called functions Pieces are more manageable than one large program Pieces can be independently implemented and tested
Copyright @ IT Series www.itseries.com.pk
Easier to Code Easier to Modify Easier to Maintain & Debug Reusability possible to perform mathematical operation on character values Less Programming Time
Copyright @ IT Series www.itseries.com.pk
User-defined Function Function written by the programmer User-defined function has a unique name A program may contain many user-defined functions possible to perform mathematical operation on character values These functions are written according to the exact need of the user
Copyright @ IT Series www.itseries.com.pk
Built-in or Predefined Functions • Available as a part of language. • Also known as Library functions. Remember! • These functions are ready-made programs. puts() , gets() , printf() , scanf() etc are standard • Make programming faster and easier. library functions
• These functions are defined in different header files.
• printf() and scanf() possible to perform functions mathematical are defined in stdio.hoperation on character values • getch(), getche(), and clrscr() functions are defined in in conio.h • The sqrt() function is defined in the math.h header file. • C provides many built-in functions to perform mathematical calculations, input /output and many other useful operations. • printf() is used to send formatted output to the screen (display output on the screen). • The sqrt() function calculates the square root of a number.
Copyright @ IT Series www.itseries.com.pk
Function Declaration or Function Prototype • Function declaration or Function prototype is a model of a function. • It provides information to the compiler about the structure of the function to be used in program. • The compiler must know the following about a function before it is called: • Name - the name of the function. Function names follow same rules as variable names possible • Return type – data type oftothe perform value mathematical operation the function returns onpart to the character values that called it of the program • Parameters- Values that are provided to a function when the function is called. • Function prototype usually placed at the beginning of source file just before the main() function.
Copyright @ IT Series www.itseries.com.pk
Syntax Remember! Return-type Function-name (parameters); Forgetting the semicolon at the end of function The parameters are given in two ways: prototype is a syntax error • Only data types of the parameters are written in prototype as follows: int add(int, int); • Both data types and names of parameters are written in prototype as follows: Remember! int add(int a, int b); possible to perform mathematical operation on character values Specifying a function parameters of the same Examples type as double a, b instead of double a, double b results in a compilation error
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
Function definition A set of statements that explains what a function does is called function definition. The function definition can be written at the following places: Remember! • Before main() function Placing a semicolon after right parenthesis • After main() function enclosing the parameter • In a separate file list of function definition is a syntax error The function definition consists of two parts: 1. Function Header possible to perform mathematical operationSyntax on character values • The first line of function definition is known as function header. • It is similar to function prototype • The only difference is that it is not terminated with semicolon. • The number of parameters and sequence parameters in function header and function prototype must be same. 2. Function Body • The set of statements which are executed inside the function is known as function body. • The body of function appears after function declaration and the statements are written in curly braces { }
Copyright @ IT Series www.itseries.com.pk
Difference between Function Definition & Declaration
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
Function Call The statement that activates a function is known as function call. To call a function, use the function name followed by () and ; fun(); The following steps take place when a function is called: • The control moves to the function that is called possible to perform mathematical operation on character values • All statements in the function body are executed • The control returns back to the calling function When the control returns back to the calling function, the remaining statements in the calling function are executed main is automatically called when the program starts main can call any number of functions Functions can call other function
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
• Parameters are the values that are provided to a function when the function is called. • Parameters are given in the parentheses • If there are many parameters, these are separated by commas. If there is no parameter, empty parentheses are used possible • The sequence and types to perform mathematical of parameters in function operation call muston becharacter values similar to the sequence and types of parameters in function declaration • Parameters in function call are called actual parameters • Parameters in function declaration are called formal parameters • The number of actually parameter must be same as number of formal parameters. • The values of actual parameters are copied to formal parameters when a function call is executed
Copyright @ IT Series www.itseries.com.pk
Example
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
Difference between Formal & Actual Parameters
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
• A function can return a single value. • The return type in function declaration indicates the type of value returned by a function e.g. int is used as return type if the function returns integer value • If the function returns no value, the keyword void is used as return type • The keyword return ispossible to perform used to return mathematical the value operation back to the on character values calling function • When the return statement is executed in a function, the controls moves back to the calling function along with the returned value Syntax The syntax for returning a value is as follows: return expression;
Copyright @ IT Series www.itseries.com.pk
• The calling function can use the returned value in the following ways: • Assignment statement • Arithmetic expression • Output statement
1. Assignment Statement
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
2. Arithmetic Expression
possible to perform mathematical operation on character values
3. Output Statement
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
• A variable declared inside a function • Local variables are also called automatic variables Syntax auto data_type identifier; auto It is a keyword that indicates that the variable is automatic. The use of auto is optional. possible to perform mathematical operation on character values data_type It indicates the data type of variable identifier It indicates the name of variable Scope of Local Variable • The area where a variable can be accessed is known as scope of variable • Local variable can be used only in the function in which it is declared • If a statement accesses a local variable that is not in scope, the compiler generates a syntax error
Copyright @ IT Series www.itseries.com.pk
Lifetime of Local Variable • The time period for which a variable exists in the memory • The lifetime of local variable starts when the control enters the function in which it is declared • Local variable is automatically destroyed when the control exits from the function and its lifetime ends possible to perform mathematical operation on character values • When the lifetime of a local variable ends, the value stored in this variable also becomes inaccessible
Copyright @ IT Series www.itseries.com.pk
• A variable declared outside any function • Global variables can be used by all functions in the program • The values of these variables are shared among different functions • If one function changes the value of a global variable, this change is also available to other functions Scope of Global Variable • Global variable can bepossible used by to allperform functions in the program mathematical operation on character values • It means that these variables are globally accessed from any part of the program • Normally, global variables are declared before main function Lifetime of Global Variable • Global variables exist in the memory as long as the program is running • These variables are destroyed from the memory when the program terminates • These variables occupy memory longer than local variables
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
• The simplest type of function is a function that accepts no argument and returns no value • The return type of such function is void • The parameter list may be empty or the keyword void can be written in the parenthesis • The keyword void instead of parameter list indicates that the function accepts no argument when it is executed Example possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
• A function may require certain values to calculate the result • The required values are passed to the function as arguments • The arguments are written in parenthesis • Each argument is separated by semicolon • The function can process the arguments possible to perform and mathematical operation on character values return the result back to main function. • A function can only return single value. • The keyword return is used to return a value.
Copyright @ IT Series www.itseries.com.pk
Example
possible to perform mathematical operation on character values