What is a function in a program?
Before jumping to the function call, we need to understand the function in the C programming language. A function is a group of tasks used to execute the predefined operations and returns a value. A large program can be divided into small blocks of code that help to understand the logic, debug, and modified it. Show
In the C programming language, the function is divided into two parts: the built-in/ library function and the user-defined function. Library/ Built-in FunctionA library function is predefined functions, and its tasks are also defined in the C header files. So, it does not require writing the code of the particular function; instead, it can be called directly in a program whenever it is required. Example: printf(), scanf(), getch(), etc., are the predefined function in the C library, and the meaning of these functions cannot be changed. Let's write a program to define the library function in C. Predefined.c Output: Welcome to the JavaTpoint.com It is the library function or built-in function in C User-defined FunctionIt is a user-defined function in the C programming language to execute some specific actions according to the programmer's requirement. A user-defined function is divided into three types such as function declaration, function definition, and function call. Function DeclarationA function declaration defines the name and return type of a function in a program. Before using the function, we need to declare it outside of a main() function in a program. Syntax: Example of function declaration: In the above example, int is a return data type of the function name add function that contains two integer parameters as num1 and num2. Furthermore, we can write the above function declaration is as follows: Function DefinitionIt defines the actual body of a function inside a program for executing their tasks in C. Syntax: In the above syntax, a function definition contains the three parts as follow:
Consider an example to demonstrate the function definition: Function Calling:A function call is an important part of the C programming language. It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function. Syntax: Let's consider a program to call a function in C programming languages. Add.c Output: Enter the first and second number 5 6 The sum of the two number is 11 Call by Value:When single or multiple values of an actual argument are copied into the formal parameter of a function, the method is called the Call by Value. Hence, it does not alter the function's actual parameter using the formal parameter. Consider a program to demonstrate the Call by Value in C programming. Call_Value.c Output: x = 10, y = 20 from main before calling the function x = 15, y = 25 from modular function x = 10, y = 20 from main after calling the function Call by Reference:In this method, the address of the actual argument is copied into the function call's formal parameter; the method is known as Call by Reference. If we make some changes in the formal parameters, it shows the effect in the value of the actual parameter. Consider a program to demonstrate the Call by Reference in C programming. Call_Ref.c Output: x = 10, y = 20 from main before calling the function x = 15, y = 25 from modular function x = 15, y = 25 from main after calling the function next → ← prev In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages. Advantage of functions in CThere are the following advantages of C functions.
Function AspectsThere are three aspects of a C function.
The syntax of creating function in c language is given below: Types of FunctionsThere are two types of functions in C programming:
Return ValueA C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return type. Let's see a simple example of C function that doesn't return any value from the function. Example without return value: If you want to return any value from the function, you need to use any data type such as int, long, char, etc. The return type depends on the value to be returned from the function. Let's see a simple example of C function that returns int value from the function. Example with return value: In the above example, we have to return 10 as a value, so the return type is int. If you want to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method. Now, you need to call the function, to get the value of the function. Different aspects of function callingA function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects of function calls.
Example for Function without argument and return valueExample 1 Output Hello Javatpoint Example 2 Output Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34 Example for Function without argument and with return valueExample 1 Output Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34 Example 2: program to calculate the area of the square Output Going to calculate the area of the square Enter the length of the side in meters: 10 The area of the square: 100.000000 Example for Function with argument and without return valueExample 1 Output Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34 Example 2: program to calculate the average of five numbers. Output Going to calculate the average of five numbers: Enter five numbers:10 20 30 40 50 The average of given five numbers : 30.000000 Example for Function with argument and with return valueExample 1 Output Going to calculate the sum of two numbers: Enter two numbers:10 20 The sum is : 30 Example 2: Program to check whether a number is even or odd Output Going to check whether a number is even or odd Enter the number: 100 The number is even C Library FunctionsLibrary functions are the inbuilt function in C that are grouped and placed at a common place called the library. Such functions are used to perform some specific operations. For example, printf is a library function used to print on the console. The library functions are created by the designers of compilers. All C standard library functions are defined inside the different header files saved with the extension .h. We need to include these header files in our program to make use of the library functions defined in such header files. For example, To use the library functions such as printf/scanf we need to include stdio.h in our program which is a header file that contains all the library functions regarding standard input/output. The list of mostly used header files is given in the following table.
Next TopicCall by value and call by reference in C ← prev next → What is a function in programming example?Every programming language lets you create blocks of code that, when called, perform tasks. Imagine a dog that does the same trick only when asked. Except you do not need dog treats to make your code perform. In programming, these code blocks are called functions.
What is function in C programming with examples?There are two types of functions in C programming: Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times.
What do you mean by function?A function is defined as a relation between a set of inputs having one output each. In simple words, a function is a relationship between inputs where each input is related to exactly one output. Every function has a domain and codomain or range.
How do you know if a program is a function?To determine whether a relation is a function, you need to check whether one input value leads to two different output values. If one input value does lead to two different output values, you will be able to tell visually because the two points will line up vertically.
|