How To Call A Function

Table of contents:

How To Call A Function
How To Call A Function

Video: How To Call A Function

Video: How To Call A Function
Video: Calling a Function in Python 2024, December
Anonim

Functions of the C programming language are used to perform a series of repetitive actions within a single program. Sometimes a large block of some auxiliary calculations is also separated into a separate function. As a rule, the function is called with the passing of the set arguments. A function can be either returning a value or simply performing a number of specific actions. You can call a function only after its description or its prototype has been declared.

How to call a function
How to call a function

It is necessary

C programming environment

Instructions

Step 1

A function declaration can be made in a header file with the.h extension. In this case, you can use a function call anywhere in the program code without worrying about the visibility of its declaration. Header files are included in files with the.срр extension at the very beginning with a line like: #include “My_sag.h”.

Step 2

Declare the function as follows: bool My_fanc (char p1, int p2). Here My_fanc is the unique function name for your program. The following description of the function can be performed anywhere in the program code. To do this, specify the return type, function name, and any arguments passed. After that, write the actions performed by the function in curly braces that enclose the function body.

Step 3

In the place in the code where you want to perform the actions of this function, write its name and pass all the required arguments to it. The type of the passed values must be the same as the declared type. Assign the returned value to a variable of the same type: bool Res = My_fanc (“H”, 24). Passing arguments to a function can be carried out both through variables of the specified type, and using constant values.

Step 4

When calling an overloaded function, the number of its arguments can be different for the same header. It is important to specify their values correctly here, since the compiler may not detect your error in the function call.

Step 5

The function can be called using a pointer. To do this, declare this pointer and assign it the address of the function: int (* p_F) (const char *, const char *); p_F = My_fanc. In this case, the call to the My_fanc function can be written as a reference to a pointer. For example, this is how you can pass the address of a function as an argument to another function: copy (n, p_F). Thus, already in the copy function, the call to My_fanc will look like this: (* p_F) (a, b), where a, b are the arguments of the called function. The result of work for any implemented call will correspond to the programmed actions of the function.

Recommended: