Home> Common Problem> body text

Can function calls be nested?

藏色散人
Release: 2023-02-07 15:35:24
Original
22316 people have browsed it

Function calls can be nested, but function definitions cannot be nested, because in C language, function definitions are parallel and independent of each other, which means that when a function is defined, the function body cannot Contains the definition of another function, that is, functions cannot be nested definitions, but they can be nested calls.

Can function calls be nested?

The operating environment of this tutorial: Windows 7 system, C18 version, Dell G3 computer.

In C language, function calls can be nested, but function definitions cannot be nested.

You cannot nest the definition, that is,

function a(){ function b(){ } }
Copy after login

It is wrong to define function b inside function a. You can only define function b outside function a like this

function a(){} function b(){}
Copy after login

Can be nested Set call, that is,

function a (){ b(); }
Copy after login

can call the defined b function in the a function.

The reason why functions cannot be nested is because the syntax is not supported and the function cannot be defined inside the function definition. The definitions of functions in C language are all parallel and independent of each other. That is to say, when a function is defined, the function body cannot contain the definition of another function. That is, functions cannot be nested definitions, but they can be nested calls.

What are nested calls of functions?

In the process of calling a function, the process of calling another function

Function In C language, the definition of each function exists relatively independently, inside the function Other functions can be called (this does not include the main() function). This calling process is called function nesting (the definition part of the function cannot be nested).

Example:

Find the greatest common divisor and least common multiple of two integers.

Problem Analysis

The least common multiple of two numbers = the product of the two numbers/the greatest common divisor of the two numbers.

So the key is the greatest common divisor.

Thinking map for finding the greatest common divisor:

Can function calls be nested?

Code implementation

#define _CRT_SECURE_NO_WARNINGS 1 #include"stdio.h" #include"math.h" int gcd(int a, int b) { int c; if (a
        
Copy after login

Implementation of the function: Find the least common multiple and greatest common factor of the two integers 45 and 56.

Can function calls be nested?

Recommended:c video tutorial

The above is the detailed content of Can function calls be nested?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!