GESP C++ Level 4 | Functions: Definition, Invocation, Declaration

πŸ‘† ClickKnowledge and Action Integration GESP> Click the top right corner β€œΒ·Β·Β·β€ > Mark as favorite 🌟

Hello everyone, I am Teacher Huang. I have worked in several large state-owned technology companies and have many years of practical experience in C/C++. I have developed electronic terminal products with millions of users. Currently, I am an amateur lecturer for children’s C++ programming, after all, CSP also claims to be non-professional.😁

I have completed the column β€œGESP Level 1 Survival Series”, the column β€œGESP Level 2 Programming Survival – Character Graphics”, and the column β€œGESP Level 2 Programming Survival – Brute Force Enumeration” for everyone’s reference.

Now I am launching the column β€œGESP Level 3 Programming Series” and will share relevant practical experiences, learning and progressing together with the students.πŸš€

GESP C++ Level 4 | Functions: Definition, Invocation, DeclarationGESP C++ Level 4 | Functions: Definition, Invocation, Declaration

Selected Past Articles

GESP Certification Level | Programming Problem Submission Status AC, WA, TLE… All Black Language Bleached!

GESP Level 3 C++ Programming Problem Summary: 2025.11.07

GESP Certification Level C++ Level 2 Survival Notes – Nested Enumeration

GESP Certification Level C++ Level 2 Survival Notes – Character Graphics

GESP Certification Level C++ Level 1 Survival Problem Series Notes

Welcome to follow the public account, to learn and progress together like πŸ‘ | share🌏 | recommend β™₯️ | forward πŸ”₯ | comment 🐳

The GESP Level 4 Programming Exam builds on the GESP Level 3 Programming, adding applications of two-dimensional arrays, functions and exceptions, structures and pointers, file operations, bubble, selection, and insertion sorting algorithms (sort), and recursive algorithms. The content in parentheses may be beyond the syllabus, but learning it early is beneficial!

In the previous article GESP C++ Level 4 Knowledge Points: Custom Function Forwarding, I introduced some basics of functions. This article mainly discusses the definition, invocation, and declaration (prototype) of functions!

GESP C++ Level 4 | Functions: Definition, Invocation, Declaration

↑ Function definition syntax explanation ↑

01

Function Definition

1. Function with no return value and no parameters:

1. Return value type is void

2. No parameters can be(), or(void)

3. The final return can be omitted

void PrintYes() {    cout<<"Yes\n";}void PrintNo() {    cout<<"No\n";}

The code implements two custom functions: no parameters, no return value. From the function names, we know one outputs Yes, and the other outputs No.

2. Function with no return value and with parameters:

1. Return value type is void

2. Parameter list is between(), multiple parameters are separated by commas ‘,’

3. The final return can be omitted

void PrintResult(bool f) {    if(f) cout<<"Yes\n";    else cout<<"No\n";}

The above code implements a custom function: with parameters, no return value. Based on the function parameters, it determines whether to output Yes or No.

3. Function with return value and with parameters:

1. Return value type is int

2. Parameter list is between(), β€œtype name” is one parameter, multiple parameters are separated by commas ‘,’

3. return cannot be omitted

int add(int a, int b) {    int res = a+b;    return res;}

The above code implements a custom function: with parameters, with return value; through the function name, parameters, and code, we can see that the function add calculates the sum of parameters a and b, and returns it to the caller through return.

From the above three examples, do you understand the key points of function definition?

GESP C++ Level 4 | Functions: Definition, Invocation, Declaration

1. Return value type: when there is no return value, it is void, when there is a return value, it is based on the return value type;

2. Parameter list: is between(), with β€œtype name” as one parameter, multiple parameters are separated by commas ‘,’ , when there are no parameters, nothing can be written, or in() you can write void;

3. Return value: when there is no return value, the final return statement can be omitted, when there is a return value, there must be a definite return statement at the end.

02

Function Invocation and Declaration

1. Define first, then invoke

#include <bits/stdc++.h>using namespace std;// 1 Function definitionint add(int a, int b) {    return a + b;}int main() {    // 2 Function invocation    int ans = add(12,13);    cout<<ans<<endl;    return 0;}

2. Declare first, then invoke, then define

#include <bits/stdc++.h>using namespace std;// 1 Function declaration (function prototype)int add(int a, int b);int main() {    // 2 Function invocation    int ans = add(12,13);    cout<<ans<<endl;    return 0;}// 3 Function definitionint add(int a, int b) {    return a + b;}

The general principle of function invocation: the caller must know what the function looks like before invoking it, otherwise it is like a blind person touching an elephant!!! We can compare it to the principle of defining variables before using them.

03

Custom Function Examples

1. Prime Number Check

bool isprime(long long x){    for(long long i=2; i<=sqrt(x);i++){        if(x%i == 0) return false;    }    return x>=2;}

2. Reverse Number

long long dds(long long x){    long long y=0,t=x;    while(t){        y = y*10 + t%10;        t = t/10;    }    return y;}

3. Palindrome Check

bool ishw(long long x){    long long y=0,t=x;    while(t){        y = y*10 + t%10;        t = t/10;    }    return x==y;}

4. Palindrome String Check

bool ishw(string s){    int len=s.size();    for(int i=0,j=len-1;i<len;i++,j--){        if(s[i]!=s[j]) return false;    }    return true;}

5. Count Digits

int ssw(long long x){    int res = 0;    do{        res++;        x=x/10;        cout<<x<<endl;    }while(x);    return res;}

6. Sum of Digits

int swh(long long x){    int res = 0;    while(x){        res += x%10;        x /= 10;    }    return res;}

7. Sum of Factors

int yzh(long long x){    int res = 0;    for(long long i=1; i<=sqrt(x); i++){        if(x%i==0) res += i;    }    return res;}

Additionally, I recommend parents read the article What is Programming to understand programming, which includes the teacher’s WeChat contact method! Providing systematic learning services and examination guidance and evaluation services for levels one to four.

Leave a Comment