The document discusses functions in C programming. It defines what a function is, explains function prototypes and declarations, and how to define and call functions. It also covers differences between function definitions and declarations and provides examples of creating simple functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views
Newfunction
The document discusses functions in C programming. It defines what a function is, explains function prototypes and declarations, and how to define and call functions. It also covers differences between function definitions and declarations and provides examples of creating simple functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34
Function
Function
Function is simply a collection of commands that does something or
specific tasks. Function breaks large computing task into smaller ones. It makes a program reusable i.e. enable people to build on what others have done instead of building another one from the scratch. It also hides the detail of operation from parts of the program that do not need to know about them. It may or may not return a value to the calling function. Variables declared in a function are local variables While variables that are not declared in any function are global variable Function Prototype/declaration
The syntax for declaring a function is:
return-type function-name (argument or argument-list); For example: the following function add is an integer and takes in three parameters x, y, and z. int add (int x, int y, int z); int factorial (int n); Note that if the return-type is not there C uses int by default. Also the return value must match the return-type. If a function will not return any value use the following syntax; void function-name (argument or argument lists); Void means no return value (when in the position of return-type), or no return arguments when in the position of argument. Function Prototype/declaration
Arguments: are local variables i.e. values passed from caller.
Return value: single value returned to caller function. First argument: the argument type is integer
Function name Return type main function
main function is a special function which every c program must have.
Execution always start from the main function. It is a function which expects no arguments; this is indicated by an empty list (). Main function always call other functions like printf, scanf, getchar. Syntax: return type main () main function • The function body is enclosed within the left and right braces {…}. • { This opening brace marks the start of the statements that make up the function body. • And must be ended with a closing brace }. • This pair of braces and the portion of the program between the braces is called a block main function
Main function like every other function can communicate with
another function by calling the function. Syntax for function call: function_name(argument); For example: printf(“Hello-World”); printf(“the sum of two integers are %d”, sum(a,b)); Declare the main function 1.Begin with comment Define the library
# indicates That this Line should be interpreted By the preprocessor
This line is the preprocessor
directives Int indicates the return type Void indicates no argument
Use left and
Right braces. They contain The body of System pause The main Is used by Dev fun- C++ for viewing ction Console application Explanation printf(" The first program in C "); instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks This displays the phrase The first C program printf() is part of the standard C library. It's termed a function, and using a function in the program is termed calling a function. Identify the function called by main Identify the functions called by main and the point of call Solution Note these: Note that : return 0; statement in the main function indicates the program ended successfully The keyword return is one of several means we’ll use to exit a function
Every statement must end with a semicolon
(also known as the statement terminator). Each function including the main function must have its function body within the right and left braces i.e.{ function body} Things to Note When Writing C Program
; (semicolon) indicates the end of a statement
<…> e.g. <stdio.h>..the compiler should look for the file in a library. In this case compiler should look for input and output files such as “printf” and “getchar”. All C statements are defined in free format, i.e., with no specified layout or column assignment. Lab work
Write a program without a return statement in the main function.
Function Definition
Definition refers to a place where the variable or function is created or
assigned storage. Syntax: return-type function-name(argument or arguments list) Difference Between Function definition and function declaration Function definition Syntax: return-type function-name(argument or arguments list) Function declaration Syntax: return-type function-name(argument or arguments list); The major difference is that function decalration ends with a semicolon. Another minor difference is that data type of the argument may not be indicated in function definition. Another difference is that function definition is followed by a block of function body. Example: Reading Assignment
Textbook: Computer Science Problem Solving Methods In C and C++
Pages 92-110, 118-128 Function • Function is simply a collection of commands that does something. • Function breaks large computing task into smaller ones. • It makes a program reusable i.e. enable people to build on what others have done instead of building another one from the scratch. • It also hides the detail of operation from parts of the program that do not need to know about them. Creating Functions using Function Prototype • /* Begins with comments about file contents */ • Insert preprocessor definitions • Function prototypes
• Define main() function
• { • Function body • }
• Define other function i.e. function definition
• { • Function body • } Function Prototype • Function prototypes are also known as function declarations. • Declaration refers to stating the nature of a variable or function, but no storage is allocated. • At this stage user-defined function are declared. Syntax of Function Prototype/declaration • The syntax for declaring a function is: • return-type function-name (argument or argument-list); • For example: the following function add is an integer and takes in three parameters x, y, and z. • int add (int x, int y, int z); • int factorial (int n); • Note that if the return-type is not there C uses int by default. • Also the return value must match the return-type. • If a function will not return any value use the following syntax: • void function-name (argument or argument lists); • Void means no return value (when in the position of return-type), or no return arguments when in the position of argument. • Note that function prototypes end with a semicolon Function Call • A function can be called from any other function where its result is needed. • Function for calling a function is as follows: • To call a function called add which has two arguments: • add(x,y) Function Definition • Function definitions instruct the computer on how the function will perform its operation. • The syntax is as follows: • returntype functionname(parameters) •{ • Function body i.e. instructions on functions operations •} Example: a simple calculator program A simple calculator continued… Practical • Develop a simple application that can compute • area of a cylinder • area of a cube • area of a cone • You are to develop functions for each of the shapes • Program can be broken down into segments called functions. • Each function performs specific task • Each function can be independent of each other. • The interface with which function receives or passes information or relates with other functions is the function name e.g. add(). • There are two ways in which arguments or parameters can be passed to the called function. • Call by value The values of the variables are passed by the calling function to the called function. • Call by reference The addresses of the variables are passed by the calling function to the called • function. Call-by-value • Following are the points to remember while passing arguments to a function using the call-byvalue • method: • Σ When arguments are passed by value, the called function creates new variables of the same • data type as the arguments passed to it. • Σ The values of the arguments passed by the calling function are copied into the newly created • variables. • Σ Values of the variables in the calling functions remain unaffected when the arguments are • passed using the call-by-value technique. • Pros and cons • The biggest advantage of using the call-by-value technique is that arguments can be passed as • variables, literals, or expressions, while its main drawback is that copying data consumes additional • storage space. In addition, it can take a lot of time to copy, thereby resulting in performance penalty, • especially if the function is called many times. Call-by-reference